Hi,
Since #7059, zero-length selection in if_any() yields an empty dataframe:
library(dplyr)
selected_cols = "mpg"
mtcars |>
filter(if_any(any_of(selected_cols), ~ .x>20)) |>
nrow()
#> [1] 14
selected_cols = character(0)
mtcars |>
filter(if_any(any_of(selected_cols), ~ .x>20)) |>
nrow()
#> [1] 0
Created on 2026-04-10 with reprex v2.1.1
This behaviour is consistent with the current definition of if_any(), but in some programmatic workflows it would be useful to have an option to keep all rows when the selection is empty.
A typical use case is:
- define a predicate
- define a set of columns on which to apply it
- filter rows failing that predicate on any selected column
In that context, when no columns are selected, it can be natural to interpret this as "there is nothing to check", so all rows should be kept rather than discarded.
For example, something like .empty= "keep" could be useful, with the current behaviour remaining the default (.empty= "discard").
There is some kind of workaround, but I don't find it very satisfying:
mtcars |>
filter(
length(selected_cols) == 0 ||
if_any(any_of(selected_cols), ~ .x > 20)
) |>
nrow()
#> [1] 32
Hi,
Since #7059, zero-length selection in
if_any()yields an empty dataframe:Created on 2026-04-10 with reprex v2.1.1
This behaviour is consistent with the current definition of
if_any(), but in some programmatic workflows it would be useful to have an option to keep all rows when the selection is empty.A typical use case is:
In that context, when no columns are selected, it can be natural to interpret this as "there is nothing to check", so all rows should be kept rather than discarded.
For example, something like
.empty= "keep"could be useful, with the current behaviour remaining the default (.empty= "discard").There is some kind of workaround, but I don't find it very satisfying: