You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
f<-function(df, var) filter(df, {{ var }})
f(mtcars, am)
# Error in `filter()`:# ℹ In argument: `vs`.# Caused by error:# ! `..1` must be a logical vector, not a double vector.
This code works:
f<-function(df, var) filter(df, {{ var }} ==0)
f(mtcars, vs)
The text was updated successfully, but these errors were encountered:
The above code is working as expected, as filter() expects logical vectors.
See the argument documentation for ... (?dplyr::filter):
...: <‘data-masking’> Expressions that return a logical value, and
are defined in terms of the variables in ‘.data’. If multiple
expressions are included, they are combined with the ‘&’
operator. Only rows for which all conditions evaluate to
‘TRUE’ are kept.
filter(df, {{ var }}) therefore works well as an example of indirection, as long as var evaluates to a logical vector:
library(dplyr)
(df<- tibble(a=1:3, b=a!=2))
#> # A tibble: 3 × 2#> a b #> <int> <lgl>#> 1 1 TRUE #> 2 2 FALSE#> 3 3 TRUE f<-function(df, var) filter(df, {{ var }})
f(df, b)
#> # A tibble: 2 × 2#> a b #> <int> <lgl>#> 1 1 TRUE #> 2 3 TRUE
f(df, a==1)
#> # A tibble: 1 × 2#> a b #> <int> <lgl>#> 1 1 TRUE
This code does not work:
This code works:
The text was updated successfully, but these errors were encountered: