Skip to content

Commit

Permalink
Add error message when passing an array in conditions (#7069)
Browse files Browse the repository at this point in the history
* add error message when passing a matrix

tidyverse day

* Update test-vec-case-when.R

* fixes

* Tweaks

* NEWS bullet

* Update snap

---------

Co-authored-by: Davis Vaughan <[email protected]>
  • Loading branch information
ilovemane and DavisVaughan authored Aug 27, 2024
1 parent cfb25a0 commit 1d17672
Show file tree
Hide file tree
Showing 4 changed files with 50 additions and 0 deletions.
3 changes: 3 additions & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
@@ -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
Expand Down
15 changes: 15 additions & 0 deletions R/vec-case-when.R
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down Expand Up @@ -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
)
}
16 changes: 16 additions & 0 deletions tests/testthat/_snaps/vec-case-when.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
16 changes: 16 additions & 0 deletions tests/testthat/test-vec-case-when.R
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down

0 comments on commit 1d17672

Please sign in to comment.