Skip to content

Frustrating stripping of newlines and whitespace in warning_cnd() and error_cnd() #1833

@DavisVaughan

Description

@DavisVaughan

Consider the following lifecycle message

warn_case_when_scalar_lhs_vector_rhs <- function(
  user_env,
  error_call
) {
  details <- paste(
    sep = "\n",
    "Please use a series of simpler if statements with early returns instead.",
    "# Previously",
    "case_when(scalar_lhs1 ~ rhs1, scalar_lhs2 ~ rhs2)",
    "# Now",
    "if (scalar_lhs1) {",
    "  return(rhs1)",
    "}",
    "",
    "if (scalar_lhs2) {",
    "  return(rhs2)",
    "}"
  )

  lifecycle::deprecate_soft(
    when = "1.2.0",
    what = I("Using `case_when()` with scalar LHS and vector RHS"),
    details = details
  )
}

This shows

Warning message:
Using `case_when()` with scalar LHS and vector RHS was deprecated in <NA> 1.2.0.
ℹ Please use a series of simpler if statements with early returns instead. # Previously
  case_when(scalar_lhs1 ~ rhs1, scalar_lhs2 ~ rhs2) # Now if (scalar_lhs1) { return(rhs1) }

if (scalar_lhs2) { return(rhs2) }

I seem to often be frustrated by rlang/cli stripping out purposeful leading whitespace and newlines from my error messages.

It seems like this rougly boils down to these two annoyances

# With a single string, newlines and whitespace are stripped
rlang::warning_cnd(message = "a\n      b", use_cli_format = TRUE)
#> <warning/rlang_warning>
#> Warning:
#> a b

rlang::warning_cnd(message = "a\n      b", use_cli_format = FALSE)
#> <warning/rlang_warning>
#> Warning:
#> a
#>       b

# With multiple strings, whitespace is stripped
rlang::warning_cnd(message = c("a", "    b"), use_cli_format = TRUE)
#> <warning/rlang_warning>
#> Warning:
#> a
#> b

rlang::warning_cnd(message = c("a", "    b"), use_cli_format = FALSE)
#> <warning/rlang_warning>
#> Warning:
#> a
#>     b

It also seems to have something to do with the fact that dplyr calls local_use_cli() for itself on load, but I don't think that is the real problem.

The main issue is that I don't know how to write this multiline error message

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions