Skip to content

GH-46636: [R] Fix evaluation of external objects not in global environment in case_when() #46667

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

Merged
merged 3 commits into from
Jun 5, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions r/NEWS.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
2 changes: 1 addition & 1 deletion r/R/dplyr-eval.R
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
17 changes: 17 additions & 0 deletions r/tests/testthat/test-dplyr-funcs-conditional.R
Original file line number Diff line number Diff line change
Expand Up @@ -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))
)
})
Loading