Search code examples
rdplyrlazy-evaluationnselazyeval

How to pass formulas or quosures in dplyr verbs R as arguments


I am not able to use formulas or quosures in filter expressions in dplyr.

a_table <- data_frame(key = rep(letters[1:2], each = 2), 
  value = replace(runif(4), mod(1:4, 2) == 1, NA))

a_cond <- quo(not(is.na(value)))

filter(a_table, !!a_cond)

Yields the error

Error in filter_impl(.data, dots) : invalid argument type

I have read the vignettes on NSE and dplyr programming, and tried different calls to this:

a_cond <- interp(~not(is.na(value_)), value_ = as.name("value"))

But haven't really nailed it.
Thank you for your help.

The Session Info is:

> sessionInfo()
R version 3.4.2 (2017-09-28)
Platform: x86_64-pc-linux-gnu (64-bit)
Running under: Ubuntu 17.10

other attached packages:
 [1] rlang_0.1.1       readxl_1.0.0      aws.s3_0.3.3      magrittr_1.5     
 [5] dplyr_0.5.0       ggplot2_2.2.1     tidyr_0.6.1       dtplyr_0.0.2     
 [9] lazyeval_0.2.0    purrr_0.2.2.2     data.table_1.10.4 feather_0.3.1    
[13] readr_1.1.0       lubridate_1.6.0   stringr_1.2.0  

Solution

  • To solve the quosures issue, I had to update with install_github instead of update.packages.

    This didn't solve the issue with formulas based on lazyeval::interp(), though.

    For this to work, I figured out how to make the equivalent with quosures as well. Instead of

    interp(~!is.na(value_), value_ = as.name("value"))
    

    I did

    quo(!is.na( !!sym("value") ))
    

    And now all works as it should.
    Cheers.