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

filter should warn or prevent users from using named logical inputs #7105

Open
conig opened this issue Nov 21, 2024 · 0 comments
Open

filter should warn or prevent users from using named logical inputs #7105

conig opened this issue Nov 21, 2024 · 0 comments

Comments

@conig
Copy link

conig commented Nov 21, 2024

Currently dplyr warns users who accidentally use = instead of ==.
However, this does not occur if a logical is passed as the named variable.

Demonstration

library(dplyr)

mtcars$big_cyl <- mtcars$cyl > 4
# Mistaking = for == silently fails, returning the whole dataset
filter(mtcars, big_cyl = TRUE) |>
  nrow()
#> [1] 32

Correctly using == for comparison

# Correctly using ==.
filter(mtcars, big_cyl == TRUE) |>
  nrow()
#> [1] 21

I think while doing x == TRUE is bad practice, this is bound to trip up some users and an error should be thrown.

Additional context

Example of the error working correctly:

# version 1.1.4
dplyr::filter(mtcars, cyl = "4")
#> Error in `dplyr::filter()`:
#> ! We detected a named input.
#> ℹ This usually means that you've used `=` instead of `==`.
#> ℹ Did you mean `cyl == "4"`?

Interestingly if the TRUE is in a vector the error is thrown.

dplyr::filter(mtcars, big_cyl = c(TRUE))
#> Error in `dplyr::filter()`:
#> ! We detected a named input.
#> ℹ This usually means that you've used `=` instead of `==`.
#> ℹ Did you mean `big_cyl == c(TRUE)`?
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

No branches or pull requests

1 participant