diff --git a/NEWS.md b/NEWS.md index 16cfc2f680..61abb9eb6d 100644 --- a/NEWS.md +++ b/NEWS.md @@ -1,5 +1,8 @@ # dplyr (development version) +* `case_when()` now throws a better error if one of the conditions is an array + (#6862, @ilovemane). + * `between()` gains a new `ptype` argument, allowing users to specify the desired output type. This is particularly useful for ordered factors and other complex types where the default common type behavior might not be ideal diff --git a/R/vec-case-when.R b/R/vec-case-when.R index d0908ca719..c4c27a76a2 100644 --- a/R/vec-case-when.R +++ b/R/vec-case-when.R @@ -52,6 +52,7 @@ vec_case_when <- function(conditions, condition <- conditions[[i]] condition_arg <- condition_args[[i]] check_logical(condition, arg = condition_arg, call = call) + check_no_dim(condition, arg = condition_arg, call = call) } size <- vec_size_common( @@ -207,3 +208,17 @@ vec_paste0 <- function (...) { args <- vec_recycle_common(...) exec(paste0, !!!args) } + +check_no_dim <- function(x, + ..., + arg = caller_arg(x), + call = caller_env()) { + if (is.null(dim(x))) { + return(invisible(NULL)) + } + + cli::cli_abort( + "{.arg {arg}} can't be an array.", + call = call + ) +} diff --git a/tests/testthat/_snaps/vec-case-when.md b/tests/testthat/_snaps/vec-case-when.md index 76b73e9c04..8fa734876d 100644 --- a/tests/testthat/_snaps/vec-case-when.md +++ b/tests/testthat/_snaps/vec-case-when.md @@ -102,6 +102,22 @@ Error in `vec_case_when()`: ! `conditions[[2]]` must be a logical vector, not the number 3.5. +# `conditions` can't be arrays (#6862) + + Code + vec_case_when(list(x), list(y)) + Condition + Error in `vec_case_when()`: + ! `conditions[[1]]` can't be an array. + +--- + + Code + vec_case_when(list(x), list(y)) + Condition + Error in `vec_case_when()`: + ! `conditions[[1]]` can't be an array. + # `size` overrides the `conditions` sizes Code diff --git a/tests/testthat/test-vec-case-when.R b/tests/testthat/test-vec-case-when.R index 0b6582ed43..3560bb6550 100644 --- a/tests/testthat/test-vec-case-when.R +++ b/tests/testthat/test-vec-case-when.R @@ -315,6 +315,22 @@ test_that("`conditions` can be classed logicals", { expect_identical(vec_case_when(list(x), list(1), default = 2), c(2, 1)) }) +test_that("`conditions` can't be arrays (#6862)", { + x <- array(TRUE, dim = c(3, 3)) + y <- c("a", "b", "c") + + expect_snapshot(error = TRUE, { + vec_case_when(list(x), list(y)) + }) + + # Not even 1D arrays + x <- array(TRUE, dim = 3) + + expect_snapshot(error = TRUE, { + vec_case_when(list(x), list(y)) + }) +}) + test_that("`size` overrides the `conditions` sizes", { expect_snapshot(error = TRUE, { vec_case_when(list(TRUE), list(1), size = 5)