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

Conversation

jrwinget
Copy link
Contributor

Fixes #7059

This commit addresses the unexpected behavior of if_any() and if_all() when no columns are selected. Specifically, if_any() now returns FALSE, and if_all() returns TRUE in cases where no columns match the selection criteria.

Additionally, tests have been added to ensure the correct behavior of these functions in both filter() and mutate() contexts, including the scenarios highlighted by the reprex example. This ensures that empty selections behave consistently with the logical expectations.

Fixes tidyverse#7059

This commit addresses the unexpected behavior of `if_any()` and `if_all()` when no columns are selected. Specifically, `if_any()` now returns `FALSE`, and `if_all()` returns `TRUE` in cases where no columns match the selection criteria.

Additionally, tests have been added to ensure the correct behavior of these functions in both `filter()` and `mutate()` contexts, including the scenarios highlighted by the `reprex` example. This ensures that empty selections behave consistently with the logical expectations.
Copy link
Member

@krlmlr krlmlr left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks, looks great! My proposals are non-authoritative.


expect_equal(
filter(tbl, if_any(c(), ~ is.na(.x))),
filter(tbl, FALSE)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Expected values seem slightly better without too much logic, not sure if it's easier to read though:

Suggested change
filter(tbl, FALSE)
tbl[0, ]

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for the feedback!


expect_equal(
filter(tbl, if_all(c(), ~ is.na(.x))),
filter(tbl, TRUE)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
filter(tbl, TRUE)
tbl

R/across.R Outdated Show resolved Hide resolved
Copy link
Member

@DavisVaughan DavisVaughan left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looking good! Here are a few comments

R/across.R Outdated
Comment on lines 15 to 20
#' **Important Behavior:** 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.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would create a new @details section and move this down there, without the Important Behavior header

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sounds good!

R/across.R Outdated
#' `any()` when called without inputs.
#' - `if_all()` will return `TRUE`, consistent with the behavior of
#' `all()` when called without inputs.
#'
#' If you just need to select columns without applying a transformation to each
#' of them, then you probably want to use [pick()] instead.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@jrwinget since you are touching documentation, can you please run devtools::document() as well and commit the results?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, absolutely!

R/across.R Show resolved Hide resolved
R/across.R Show resolved Hide resolved
R/across.R Outdated
if (!length(quos)) {
return(list(quo(TRUE)))
return(list(quo(empty)))
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
return(list(quo(empty)))
return(list(quo(!!empty)))

@krlmlr is right but i still think that having the empty variable is a little clearer, so here is the change you can make

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That makes sense. Thanks!

@@ -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 as expected in filter", {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
test_that("if_any() on zero-column selection behaves as expected in filter", {
test_that("if_any() on zero-column selection behaves like any() (#7059)", {

We often like to link to the issue when we add a test specifically for it

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No problem - will update that!

)
})

test_that("if_all() on zero-column selection behaves as expected in filter", {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
test_that("if_all() on zero-column selection behaves as expected in filter", {
test_that("if_all() on zero-column selection behaves like all() (#7059)", {

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@DavisVaughan I've made the requested changes and pushed the updates. Please take a look when you have a moment. Thanks!!

jrwinget and others added 4 commits August 15, 2024 12:54
- Moved specific details into the `@details` section of the documentation.
- Ran `devtools::document()` to update Rd files.
- Added a bullet in `NEWS.md` to highlight the consistent behavior of `if_any()` and `if_all()` with `any()` and `all()` when called with empty inputs.
@DavisVaughan DavisVaughan merged commit b2c7e04 into tidyverse:main Aug 15, 2024
11 checks passed
@DavisVaughan
Copy link
Member

Thanks @jrwinget!

@jrwinget
Copy link
Contributor Author

Of course - this was fun! Thanks again for your feedback!!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

if_any on zero-column selection yields TRUE
3 participants