diff --git a/r/NEWS.md b/r/NEWS.md index 9e5a694fc23..0be0bc8e0d5 100644 --- a/r/NEWS.md +++ b/r/NEWS.md @@ -24,6 +24,8 @@ - Added bindings for atan, sinh, cosh, tanh, asinh, acosh, and tanh, and expm1 (#44953) - Expose an option `check_directory_existence_before_creation` in `S3FileSystem` to reduce I/O calls on cloud storage (@HaochengLIU, #41998) +- `case_when()` now correctly detects objects that are not in the global + environment (@etiennebacher, #46667). # arrow 20.0.0.1 diff --git a/r/R/dplyr-eval.R b/r/R/dplyr-eval.R index 2dce24117a3..896b47a7799 100644 --- a/r/R/dplyr-eval.R +++ b/r/R/dplyr-eval.R @@ -30,7 +30,7 @@ arrow_eval <- function(expr, mask) { # regular dplyr may work # * validation_error: the expression is known to be not valid, so don't # recommend retrying with regular dplyr - tryCatch(eval_tidy(expr, mask), error = function(e) { + tryCatch(eval_tidy(expr, mask, env = mask), error = function(e) { # Inspect why the expression failed, and add the expr as the `call` # for better error messages msg <- conditionMessage(e) diff --git a/r/tests/testthat/test-dplyr-funcs-conditional.R b/r/tests/testthat/test-dplyr-funcs-conditional.R index 24ddd342a88..3ff93c0a87f 100644 --- a/r/tests/testthat/test-dplyr-funcs-conditional.R +++ b/r/tests/testthat/test-dplyr-funcs-conditional.R @@ -491,3 +491,20 @@ test_that("coalesce()", { class = "validation_error" ) }) + +test_that("external objects are found when they're not in the global environment, #46636", { + dat <- arrow_table(x = c("a", "b")) + pattern <- "a" + expect_identical( + dat %>% + mutate(x2 = case_when(x == pattern ~ "foo")) %>% + collect(), + tibble(x = c("a", "b"), x2 = c("foo", NA)) + ) + expect_identical( + dat %>% + mutate(x2 = if_else(x == pattern, "foo", NA_character_)) %>% + collect(), + tibble(x = c("a", "b"), x2 = c("foo", NA)) + ) +})