Skip to content
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

Fix if_any() and if_all() behavior with zero-column selections #7072

Merged
merged 5 commits into from
Aug 15, 2024
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
4 changes: 4 additions & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,10 @@

* R >=3.6.0 is now explicitly required (#7026).

* `if_any()` and `if_all()` are now fully consistent with `any()` and `all()`.
In particular, when called with empty inputs `if_any()` returns `FALSE` and
`if_all()` returns `TRUE` (#7059, @jrwinget).

# dplyr 1.1.4

* `join_by()` now allows its helper functions to be namespaced with `dplyr::`,
Expand Down
34 changes: 28 additions & 6 deletions R/across.R
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
#' `across()` makes it easy to apply the same transformation to multiple
#' columns, allowing you to use [select()] semantics inside in "data-masking"
#' functions like [summarise()] and [mutate()]. See `vignette("colwise")` for
#' more details.
#' more details.
#'
#' `if_any()` and `if_all()` apply the same
DavisVaughan marked this conversation as resolved.
Show resolved Hide resolved
#' predicate function to a selection of columns and combine the
Expand All @@ -18,6 +18,14 @@
#' `across()` supersedes the family of "scoped variants" like
#' `summarise_at()`, `summarise_if()`, and `summarise_all()`.
#'
#' @details
#' When there are no selected columns:
#'
#' - `if_any()` will return `FALSE`, consistent with the behavior of
#' `any()` when called without inputs.
#' - `if_all()` will return `TRUE`, consistent with the behavior of
#' `all()` when called without inputs.
#'
#' @param .cols <[`tidy-select`][dplyr_tidy_select]> Columns to transform.
#' You can't select grouping columns because they are already automatically
#' handled by the verb (i.e. [summarise()] or [mutate()]).
Expand Down Expand Up @@ -133,9 +141,16 @@
#' iris %>%
#' group_by(Species) %>%
#' summarise(across(starts_with("Sepal"), mean, .names = "mean_{.col}"))
#'
#' iris %>%
#' group_by(Species) %>%
#' summarise(across(starts_with("Sepal"), list(mean = mean, sd = sd), .names = "{.col}.{.fn}"))
#' summarise(
#' across(
#' starts_with("Sepal"),
#' list(mean = mean, sd = sd),
#' .names = "{.col}.{.fn}"
#' )
#' )
DavisVaughan marked this conversation as resolved.
Show resolved Hide resolved
#'
#' # If a named external vector is used for column selection, .names will use
#' # those names when constructing the output names
Expand All @@ -146,7 +161,9 @@
#' # When the list is not named, .fn is replaced by the function's position
#' iris %>%
#' group_by(Species) %>%
#' summarise(across(starts_with("Sepal"), list(mean, sd), .names = "{.col}.fn{.fn}"))
#' summarise(
#' across(starts_with("Sepal"), list(mean, sd), .names = "{.col}.fn{.fn}")
#' )
#'
#' # When the functions in .fns return a data frame, you typically get a
#' # "packed" data frame back
Expand All @@ -164,7 +181,9 @@
#'
#' # .unpack can utilize a glue specification if you don't like the defaults
#' iris %>%
#' reframe(across(starts_with("Sepal"), quantile_df, .unpack = "{outer}.{inner}"))
#' reframe(
#' across(starts_with("Sepal"), quantile_df, .unpack = "{outer}.{inner}")
#' )
#'
#' # This is also useful inside mutate(), for example, with a multi-lag helper
#' multilag <- function(x, lags = 1:3) {
Expand Down Expand Up @@ -618,9 +637,11 @@ expand_if_across <- function(quo) {
if (is_call(call, "if_any")) {
op <- "|"
if_fn <- "if_any"
empty <- FALSE
} else {
op <- "&"
if_fn <- "if_all"
empty <- TRUE
}

context_local("across_if_fn", if_fn)
Expand All @@ -634,9 +655,10 @@ expand_if_across <- function(quo) {
call[[1]] <- quote(across)
quos <- expand_across(quo_set_expr(quo, call))

# Select all rows if there are no inputs
# Select all rows if there are no inputs for if_all(),
# but select no rows if there are no inputs for if_any().
if (!length(quos)) {
return(list(quo(TRUE)))
return(list(quo(!!empty)))
}

combine <- function(x, y) {
Expand Down
26 changes: 23 additions & 3 deletions man/across.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

28 changes: 27 additions & 1 deletion tests/testthat/test-across.R
Original file line number Diff line number Diff line change
Expand Up @@ -870,7 +870,7 @@ test_that("if_any() and if_all() expansions deal with no inputs or single inputs
# No inputs
expect_equal(
filter(d, if_any(starts_with("c"), ~ FALSE)),
filter(d)
filter(d, FALSE)
)
expect_equal(
filter(d, if_all(starts_with("c"), ~ FALSE)),
Expand All @@ -888,6 +888,32 @@ test_that("if_any() and if_all() expansions deal with no inputs or single inputs
)
})

test_that("if_any() on zero-column selection behaves like any() (#7059)", {
tbl <- tibble(
x1 = 1:5,
x2 = c(-1, 4, 5, 4, 1),
y = c(1, 4, 2, 4, 9),
)

expect_equal(
filter(tbl, if_any(c(), ~ is.na(.x))),
tbl[0, ]
)
})

test_that("if_all() on zero-column selection behaves like all() (#7059)", {
tbl <- tibble(
x1 = 1:5,
x2 = c(-1, 4, 5, 4, 1),
y = c(1, 4, 2, 4, 9),
)

expect_equal(
filter(tbl, if_all(c(), ~ is.na(.x))),
tbl
)
})

test_that("if_any() and if_all() wrapped deal with no inputs or single inputs", {
d <- data.frame(x = 1)

Expand Down
Loading