ashleych
ashleych

Reputation: 1054

tidyeval way to programatically send values to filter

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

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

filter(iris,Sepal.Length > 6)

would be replaced with

string <- 'Sepal.Length > 6'
filter(iris,string)

Upvotes: 3

Views: 262

Answers (1)

Aur&#232;le
Aur&#232;le

Reputation: 12819

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)

Upvotes: 4

Related Questions