Closed jankowtf closed 6 years ago
Could you try with:
left <- enquo(left)
quo_expr <- quo((!! left) >= right)
@lionel- then I end up with this:
my_filter <- function(x, left, right) {
# quo_expr <- quo(quo(!!left) >= right)
# quo_expr <- quo(!!quo(left) >= right)
left <- enquo(left)
quo_expr <- quo((!! left) >= right)
print(quo_expr)
x %>% filter(!!quo_expr)
}
v_id <- 1
my_filter(df, c_id, v_id)
#> <quosure: frame>
#> ~(~c_id) >= right
#> id value
#> 1 1 14
#> 2 2 11
#> 3 1 9
#> 4 2 12
#> 5 1 9
#> 6 2 5
Is the following what you're trying to achieve?
my_filter <- function(x, left, right) {
left <- enquo(left)
right <- enquo(right)
filter(x, (!! left) >= (!! right))
}
my_filter(df, !! c_id, !! c_value)
@lionel- sorry, in the initial run of reprex()
in the original post I overlooked an error, I've updated it
Great, thanks!
I think it's easier to grasp when using ==
instead of >=
in my example (sorry, not my best reprex-day today): I wanted a more flexible version of df %>% filter(id == 1)
and yours works perfectly!
This would be the equivalent call then:
my_filter(df, !! c_id, !! v_id)
Great!
By the way, stackoverflow (where I follow the tidyeval
tag) or https://community.rstudio.com are better venues for this type of question.
Thanks, noted. I'll have a look at the tag.
Seems like it actually "only" boils down to using parentheses!
df %>% filter(!! c_id == v_id) # fails
df %>% filter((!! c_id) == v_id) # works
yes !
has low operator precedence so it basically captures everything to its right.
I would like to make use of
quo()
/enquo()
and!!
in calls todplyr::filter
in order to make the column name referentially transparent much in the same way that is supported bydplyr::mutate
.Ideally, I'd like to end up with something like this (pseudo code):
df %>% filter(!!<reference> :== <value>