This is an automated email from the ASF dual-hosted git repository.
thisisnic pushed a commit to branch main
in repository https://gitbox.apache.org/repos/asf/arrow.git
The following commit(s) were added to refs/heads/main by this push:
new 46b6f9d69fa GH-34860: [R] New column name wrongly set when using
mutate with if_any (#51314)
46b6f9d69fa is described below
commit 46b6f9d69facbe461367c416a60d40ea70679f9a
Author: Nic Crane <[email protected]>
AuthorDate: Thu Sep 17 15:11:10 2026 -0500
GH-34860: [R] New column name wrongly set when using mutate with if_any
(#51314)
### Rationale for this change
We got errors with new column names when using `mutate()` with `if_any()`
due to not having access to column name
### What changes are included in this PR?
Set the names
### Are these changes tested?
Yeah
### Are there any user-facing changes?
Yeah
* GitHub Issue: #34860
Authored-by: Nic Crane <[email protected]>
Signed-off-by: Nic Crane <[email protected]>
---
r/R/dplyr-across.R | 16 ++++++++--------
r/tests/testthat/test-dplyr-across.R | 15 +++++++++++++++
r/tests/testthat/test-dplyr-mutate.R | 31 +++++++++++++++++++++++++++++++
3 files changed, 54 insertions(+), 8 deletions(-)
diff --git a/r/R/dplyr-across.R b/r/R/dplyr-across.R
index 0568ee8c400..d020c84edff 100644
--- a/r/R/dplyr-across.R
+++ b/r/R/dplyr-across.R
@@ -59,18 +59,18 @@ expand_across <- function(.data, quos_in, exclude_cols =
NULL) {
new_quos <- quosures_from_setup(setup, quo_env)
+ # if_any()/if_all() collapse the expanded quosures into a single
+ # expression, keeping whatever name the user gave the call
+ if (is_call(quo_expr, c("if_any", "if_all"))) {
+ op <- switch(call_name(quo_expr), if_any = "|", if_all = "&")
+ combined <- reduce(new_quos, combine_if, op = op, envir = quo_env)
+ new_quos <- set_names(list(combined), names(quo_in))
+ }
+
quos_out <- append(quos_out, new_quos)
} else {
quos_out <- append(quos_out, quo_in)
}
-
- if (is_call(quo_expr, "if_any")) {
- quos_out <- append(list(), purrr::reduce(quos_out, combine_if, op = "|",
envir = quo_get_env(quos_out[[1]])))
- }
-
- if (is_call(quo_expr, "if_all")) {
- quos_out <- append(list(), purrr::reduce(quos_out, combine_if, op = "&",
envir = quo_get_env(quos_out[[1]])))
- }
}
new_quosures(quos_out)
diff --git a/r/tests/testthat/test-dplyr-across.R
b/r/tests/testthat/test-dplyr-across.R
index bd93bac8852..b0f40ad0ab2 100644
--- a/r/tests/testthat/test-dplyr-across.R
+++ b/r/tests/testthat/test-dplyr-across.R
@@ -330,3 +330,18 @@ test_that("if_all() and if_any() are supported", {
example_data
)
})
+
+test_that("if_any() and if_all() keep their name and leave other quosures
alone", {
+ # GH-34860
+ expect_across_equal(
+ quos(new_var = if_any(c(dbl, dbl2), ~ .x > 4)),
+ quos(new_var = dbl > 4 | dbl2 > 4),
+ example_data
+ )
+
+ expect_across_equal(
+ quos(int_plus = int + 1L, new_var = if_all(c(dbl, dbl2), ~ .x > 4)),
+ quos(int_plus = int + 1L, new_var = dbl > 4 & dbl2 > 4),
+ example_data
+ )
+})
diff --git a/r/tests/testthat/test-dplyr-mutate.R
b/r/tests/testthat/test-dplyr-mutate.R
index d8f5579c36e..7c4be302ffb 100644
--- a/r/tests/testthat/test-dplyr-mutate.R
+++ b/r/tests/testthat/test-dplyr-mutate.R
@@ -796,3 +796,34 @@ test_that("mutate() with aggregations after arrange()
(GH-45373)", {
tbl
)
})
+
+test_that("mutate() keeps the name given to if_any() and if_all()", {
+ # GH-34860
+ compare_dplyr_binding(
+ .input |>
+ mutate(new_var = if_any(starts_with("dbl"), ~ . > 4)) |>
+ select(new_var) |>
+ collect(),
+ example_data
+ )
+
+ compare_dplyr_binding(
+ .input |>
+ mutate(new_var = if_all(starts_with("dbl"), ~ . > 4)) |>
+ select(new_var) |>
+ collect(),
+ example_data
+ )
+
+ # other expressions in the same mutate() must not be folded into the if_any()
+ compare_dplyr_binding(
+ .input |>
+ mutate(
+ int_plus = int + 1L,
+ new_var = if_any(starts_with("dbl"), ~ . > 4)
+ ) |>
+ select(int_plus, new_var) |>
+ collect(),
+ example_data
+ )
+})