From 75732e02f5c9410c90f44ba7c3e6c0a6fb7704a9 Mon Sep 17 00:00:00 2001 From: etiennebacher Date: Mon, 2 Jun 2025 10:10:58 +0200 Subject: [PATCH 1/3] init --- r/NEWS.md | 2 ++ r/R/dplyr-eval.R | 2 +- r/tests/testthat/test-dplyr-funcs-conditional.R | 17 +++++++++++++++++ 3 files changed, 20 insertions(+), 1 deletion(-) diff --git a/r/NEWS.md b/r/NEWS.md index 9e5a694fc2308..0ecd8d8c7dd4c 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, #46567). # arrow 20.0.0.1 diff --git a/r/R/dplyr-eval.R b/r/R/dplyr-eval.R index 2dce24117a343..896b47a7799b4 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 24ddd342a882b..f352a9810b8e1 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)) + ) +}) From 18ed0cedde511b20d8eb9971a7e39ff11ac8a7ae Mon Sep 17 00:00:00 2001 From: etiennebacher Date: Mon, 2 Jun 2025 10:25:59 +0200 Subject: [PATCH 2/3] typo in PR number --- r/NEWS.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/r/NEWS.md b/r/NEWS.md index 0ecd8d8c7dd4c..0be0bc8e0d5cd 100644 --- a/r/NEWS.md +++ b/r/NEWS.md @@ -25,7 +25,7 @@ - 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, #46567). + environment (@etiennebacher, #46667). # arrow 20.0.0.1 From 470f5176a828eff49c5fcba0e6c2677d00f7bb66 Mon Sep 17 00:00:00 2001 From: etiennebacher Date: Wed, 4 Jun 2025 08:51:35 +0200 Subject: [PATCH 3/3] use magrittr pipe --- r/tests/testthat/test-dplyr-funcs-conditional.R | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/r/tests/testthat/test-dplyr-funcs-conditional.R b/r/tests/testthat/test-dplyr-funcs-conditional.R index f352a9810b8e1..3ff93c0a87f01 100644 --- a/r/tests/testthat/test-dplyr-funcs-conditional.R +++ b/r/tests/testthat/test-dplyr-funcs-conditional.R @@ -496,14 +496,14 @@ test_that("external objects are found when they're not in the global environment dat <- arrow_table(x = c("a", "b")) pattern <- "a" expect_identical( - dat |> - mutate(x2 = case_when(x == pattern ~ "foo")) |> + 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_)) |> + dat %>% + mutate(x2 = if_else(x == pattern, "foo", NA_character_)) %>% collect(), tibble(x = c("a", "b"), x2 = c("foo", NA)) )