Skip to content

FR: if_any(.empty="keep") #7831

Description

@DanChaltiel

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

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions