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

More informative error for case_when() RHS type and class mismatches #6225

Closed
wants to merge 3 commits into from

Conversation

eutwt
Copy link
Contributor

@eutwt eutwt commented Apr 1, 2022

closes #6206

Current

dplyr::case_when(c(TRUE, FALSE) ~ c(3, 4), TRUE ~ c('a', 'b'))
#> Error in names(message) <- `*vtmp*`: 'names' attribute [1] must be the same length as the vector [0]

Created on 2022-03-31 by the reprex package (v2.0.1)

New

dplyr::case_when(c(TRUE, FALSE) ~ c(3, 4), TRUE ~ c('a', 'b'))
#> Error in `dplyr::case_when()`:
#> ! `c("a", "b")` must be a double vector, not a character vector.

Created on 2022-03-31 by the reprex package (v2.0.1)

I didn't update snapshots because the line-wrapping in my error messages happens earlier than in the snapshot, and I'm not sure why.

@ericnewkirk
Copy link

I was just looking into this too, sorry for butting in. I think you need fmt_calls(quos_pairs[[i]]$rhs[2]) instead of fmt_calls(pair$rhs[i]) here. pair is just the last formula to be processed in the loop above, not necessarily the one triggering the error.

Try your version like this and you'll see what I mean:

case_when(
  1:10 > 8 ~ "a",
  1:10 > 6 ~ "b",
  1:10 > 4 ~ FALSE,
  1:10 > 2 ~ "c",
  TRUE ~ "d"
)

Co-Authored-By: Eric Newkirk <[email protected]>
@eutwt
Copy link
Contributor Author

eutwt commented Apr 15, 2022

Thanks @ericnewkirk, you are correct

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

More informative error for case_when() failing due to not matching NA
2 participants