I'd like to use dplyr
's new NSE notations (version >= 0.6) for a dynamic filter
on my data. Let's say I have the following dummy dataset:
df = data_frame(x = 1:10, y = 10:1, z = 10 * runif(10))
If now I want to filter column tofilter = "x"
for values greater than 5 I know I can do:
df %>%
filter((!!rlang::sym(tofilter)) >= 5)
Question 1
What if I want to dynamically change the operator of the filtering too (let's say I have a Shiny App in which the user can dynamically selectInput
if to filter the data for values greater than 5, equal to 5 or lower than 5?
What I'd like to do is something on the line of:
op = ">="
val = 5
filt_expr = paste("x", op, val)
df %>%
filter(filt_expr)
Obviously, this does not work and I have played a bit with the rlang
quosore/symbols, etc but didn't quite find the right way to "quote" my inputs.
Question 2
Bonus question is, what if I want to apply multiple filters? Do I need to loop or I can create a list of filtering expressions and apply them all in one go?
An example of this is a Shiny App where the user can type multiple conditions he/she wants to apply to the data so that we have a dynamically changing list of the format:
filt_expr_list = list("x >= 5", "y <= 10", "z >= 2")
and we want to dynamically apply them all, so that the output is equivalent to:
df %>%
filter(x >= 5, y <= 10, z >= 2)
I guess this is in a certain sense a subset of question 1 since when I know how to correctly quote the arguments I think I could do something like:
filt_expr = paste0(unlist(filt_expr_list), collapse = ", ")
df %>%
filter(filt_expr)
but would be nice to see if there is any nicer cleaner way