Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

case_when() error path isn't working correctly #6261

Closed
DavisVaughan opened this issue May 7, 2022 · 2 comments · Fixed by #6300
Closed

case_when() error path isn't working correctly #6261

DavisVaughan opened this issue May 7, 2022 · 2 comments · Fixed by #6300
Labels
bug an unexpected problem or unintended behavior

Comments

@DavisVaughan
Copy link
Member

library(dplyr)

# check_type
case_when(TRUE ~ 1, TRUE ~ "x")
#> Error in names(message) <- `*vtmp*`: 'names' attribute [1] must be the same length as the vector [0]

# check_class
case_when(TRUE ~ 1, TRUE ~ structure(1, class = "foo"))
#> Error in names(message) <- `*vtmp*`: 'names' attribute [1] must be the same length as the vector [0]

These are supposed to report on invalid types and classes, respectively.

This happens because we are missing an argument here in replace_with(). That NULL is probably intended to be passed on to reason, but it is instead being matched to name and that doesn't work. A character name is required for that error path to run correctly.

out <- replace_with(out, query[[i]] & !replaced, value[[i]], NULL, error_call = error_call)

@DavisVaughan DavisVaughan added the bug an unexpected problem or unintended behavior label May 7, 2022
@eutwt
Copy link
Contributor

eutwt commented May 7, 2022

Causing #6206

@DavisVaughan
Copy link
Member Author

DavisVaughan commented May 9, 2022

Introduced by #6052 when there was an internal switch from glubort() to abort() in check_type() and friends
https://github.com/tidyverse/dplyr/pull/6052/files#diff-84f715c580f8ab258e33d9d80e0eb0f8e24cb2c1a66b9c28c6abe51c52f99f54R54

We now do:

msg <- glue("{header} must be {friendly_type_of(template)}, not {friendly_type_of(x)}.")
abort(msg, call = error_call)

Where header could be NULL, causing the issue.

We used to do:

glubort(header, "must be {friendly_type_of(template)}, not {friendly_type_of(x)}.")

Where glubort() was:

glubort <- function(header, ..., .envir = parent.frame(), .abort = abort) {
   text <- glue(..., .envir = .envir)
   if (!is_null(header)) text <- paste0(header, " ", text)
   .abort(text)
 }

The previous code wouldn't give the best result, but it would at least kind of work (this is dplyr 1.0.0):

> case_when(TRUE ~ 1, TRUE ~ "x")
Error in `glubort()`:
! must be a double vector, not a character vector.
Run `rlang::last_error()` to see where the error occurred.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug an unexpected problem or unintended behavior
Projects
None yet
2 participants