1

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  
Diego-MX
  • 2,279
  • 2
  • 20
  • 35

2 Answers2

1

We can use it an expr

a_cond <- rlang::expr(magrittr::not(is.na(value)))
filter(a_table, !!a_cond)
# A tibble: 2 x 2
#  key   value
#   <chr> <dbl>
#1 a     0.225
#2 b     0.519

Or as quosure, but make sure the function not is from magrittr

a_cond <- quo(magrittr::not(is.na(value)))
filter(a_table, !!a_cond)
# A tibble: 2 x 2
#   key   value
#  <chr> <dbl>
#1 a     0.225
#2 b     0.519
akrun
  • 874,273
  • 37
  • 540
  • 662
  • Strange. With `expr` I'm still getting an error with `object 'value' not found`. With quosure, it also says `invalid argument type`. I'm adding my relevant `sessionInfo()` as an edit. – Diego-MX Feb 02 '18 at 17:31
  • @Diego What is your dplyr version? I am using `dplyr_0.7.4` – akrun Feb 02 '18 at 17:31
  • Mmph... that would be `dplyr_0.5.0`. @akrun. I'll do the update. – Diego-MX Feb 02 '18 at 17:34
  • @Diego That would be old version which wouldn't be compatible with the quosures – akrun Feb 02 '18 at 17:36
  • Thanks @akrun. Updating `dplyr` solved the quosure problem. For anyone reading this (note to self), remember to do the update with `install_github`, not `update.packages`. – Diego-MX Feb 02 '18 at 17:56
  • Asking one last thing @akrun since it still doesn't work with `lazyeval` `interp` formulas. Are they in the scope of new quosures? – Diego-MX Feb 02 '18 at 17:59
  • @Diego I am not sure about. May be the `interp` and all those stufl will be deprecated – akrun Feb 03 '18 at 02:43
  • 1
    That makes sense. `quo` and `!!` did it for me this time. There's no need of `interp` anymore. – Diego-MX Feb 06 '18 at 23:04
  • Right. Marking the answer. While @akrun 's suggestions helped me get to the point of my package being outdated, the solution was mainly to just update it, so I typed that up for an answer. Cheers. – Diego-MX Feb 08 '18 at 18:48
0

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.

Diego-MX
  • 2,279
  • 2
  • 20
  • 35