Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

tidyeval way to programatically send values to filter

Tags:

r

dplyr

tidyeval

Picking up on an earlier thread, (Use string as filter in dplyr?), what would the new tidyeval answer for this be, as filter_ is being deprecated.

Is there a way to use a string variable as the filter argument in dplyr? For example:

filter(iris,Sepal.Length > 6)

would be replaced with

string <- 'Sepal.Length > 6'
filter(iris,string)
like image 443
ashleych Avatar asked Sep 15 '26 06:09

ashleych


1 Answers

Maybe:

filter(iris, !! rlang::parse_expr(string))

But as far as I understand the tidyeval philosophy, code as string is frowned upon and there shouldn't be string <- 'Sepal.Length > 6' in the first place.

Maybe instead:

condition <- expr(Sepal.Length > 6)
filter(iris, !! condition)
like image 160
Aurèle Avatar answered Sep 17 '26 20:09

Aurèle



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!