I'm updating an old script using the deprecated dplyr::filter_()
to use dplyr::filter()
. But I can't get it to work for empty filter strings anymore:
Example:
library(dplyr)
my_df <- tibble::tibble(x = sample(c(0:9), 100, replace = TRUE))
deprecated filter_()
works for both string and empty strings
fil1 <- "x == 5"
filter_(mydf, .dots = fil1) # works
fil2 <- NULL
filter_(mydf, .dots = fil2) # works, returns all values
NSE version works only with quoted filter values, but not with empty ones
fil1 = quo(x == 5)
filter(my_df, !!enquo(fil1)) # works
fil2 = NULL
filter(my_df, !!enquo(fil2))
Error: Argument 2 filter condition does not evaluate to a logical vector
fil2 = quo(NULL)
filter(my_df, !!enquo(fil2))
Error: Argument 2 filter condition does not evaluate to a logical vector
I see three possible approaches to this:
- quote
NULL
differently - use another expression instead of
NULL
- use another argument inside
filter()