juliasilge / widyr

Widen, process, and re-tidy a dataset
http://juliasilge.github.io/widyr/
Other
327 stars 29 forks source link

{{ embrace }} doesn't work with pairwise_cor() #28

Open sebastian-fox opened 5 years ago

sebastian-fox commented 5 years ago

Hi There,

I love your package. I can't see anything about this in the current issues. I've been trying to create a function that uses widyr::pairwise_cor() using the new tidy eval method embrace, but it doesn't work. I can't see why. Here is a reproducible example:

library(dplyr)
library(gapminder)
library(tidyr)
library(widyr)

# this works
gapminder %>%
  pairwise_cor(country, year, lifeExp)

dummy_function <- function(data, var1, var2, var3) {
  data %>%
    pairwise_cor({{ var1 }}, {{ var2 }}, {{ var3 }}) %>%
    return()
}

# this doesn't work
dummy_function(gapminder, country, year, lifeExp)

This returns:

Error: Invalid column specification 

Are you able to help? Many thanks

Session info:

- Session info ------------------------------------------------------------------------------------------------------
 setting  value                       
 version  R version 3.6.1 (2019-07-05)
 os       Windows 10 x64              
 system   x86_64, mingw32             
 ui       RStudio                     
 language (EN)                        
 collate  English_United Kingdom.1252 
 ctype    English_United Kingdom.1252 
 tz       Europe/London               
 date     2019-09-26                  

- Packages ----------------------------------------------------------------------------------------------------------
 package     * version date       lib source        
 assertthat    0.2.1   2019-03-21 [1] CRAN (R 3.6.0)
 backports     1.1.4   2019-04-10 [1] CRAN (R 3.6.0)
 broom         0.5.2   2019-04-07 [1] CRAN (R 3.6.0)
 cli           1.1.0   2019-03-19 [1] CRAN (R 3.6.0)
 crayon        1.3.4   2017-09-16 [1] CRAN (R 3.6.0)
 dplyr       * 0.8.3   2019-07-04 [1] CRAN (R 3.6.1)
 fansi         0.4.0   2018-10-05 [1] CRAN (R 3.6.0)
 gapminder   * 0.3.0   2017-10-31 [1] CRAN (R 3.6.0)
 generics      0.0.2   2018-11-29 [1] CRAN (R 3.6.1)
 glue          1.3.1   2019-03-12 [1] CRAN (R 3.6.0)
 janeaustenr   0.1.5   2017-06-10 [1] CRAN (R 3.6.0)
 lattice       0.20-38 2018-11-04 [1] CRAN (R 3.6.1)
 lifecycle     0.1.0   2019-08-01 [1] CRAN (R 3.6.1)
 magrittr      1.5     2014-11-22 [1] CRAN (R 3.6.0)
 Matrix        1.2-17  2019-03-22 [1] CRAN (R 3.6.1)
 nlme          3.1-140 2019-05-12 [1] CRAN (R 3.6.1)
 packrat       0.5.0   2018-11-14 [1] CRAN (R 3.6.0)
 pillar        1.4.2   2019-06-29 [1] CRAN (R 3.6.0)
 pkgconfig     2.0.3   2019-09-22 [1] CRAN (R 3.6.1)
 plyr          1.8.4   2016-06-08 [1] CRAN (R 3.6.0)
 purrr         0.3.2   2019-03-15 [1] CRAN (R 3.6.0)
 R6            2.4.0   2019-02-14 [1] CRAN (R 3.6.0)
 Rcpp          1.0.2   2019-07-25 [1] CRAN (R 3.6.1)
 reshape2      1.4.3   2017-12-11 [1] CRAN (R 3.6.0)
 rlang         0.4.0   2019-06-25 [1] CRAN (R 3.6.0)
 rstudioapi    0.10    2019-03-19 [1] CRAN (R 3.6.0)
 sessioninfo   1.1.1   2018-11-05 [1] CRAN (R 3.6.0)
 SnowballC     0.6.0   2019-01-15 [1] CRAN (R 3.6.0)
 stringi       1.4.3   2019-03-12 [1] CRAN (R 3.6.0)
 stringr       1.4.0   2019-02-10 [1] CRAN (R 3.6.0)
 tibble        2.1.3   2019-06-06 [1] CRAN (R 3.6.0)
 tidyr       * 1.0.0   2019-09-11 [1] CRAN (R 3.6.1)
 tidyselect    0.2.5   2018-10-11 [1] CRAN (R 3.6.0)
 tidytext      0.2.2   2019-07-29 [1] CRAN (R 3.6.1)
 tokenizers    0.2.1   2018-03-29 [1] CRAN (R 3.6.0)
 utf8          1.1.4   2018-05-24 [1] CRAN (R 3.6.0)
 vctrs         0.2.0   2019-07-05 [1] CRAN (R 3.6.1)
 widyr       * 0.1.2   2019-09-09 [1] CRAN (R 3.6.1)
 withr         2.1.2   2018-03-15 [1] CRAN (R 3.6.0)
 zeallot       0.1.0   2018-01-28 [1] CRAN (R 3.6.0)
TimBMK commented 1 year ago

Any chance there's a solution to this yet? I'm running into the same problem with pairwise_pmi(); using pairwisepmi() doesn't help either. The calls work outside the function, but not when utilized inside a function. Specifically, when passing the arguments directly to pairwisepmi() via {{ var }} I'm getting:

`Error in (function (cl, name, valueClass)  : 
  assignment of an object of class “numeric” is not valid for @‘Dim’ in an object of class “dgTMatrix”; is(value, "integer") is not TRUE`

The same call outside of my function does work, however. Using the standard pairwise_pmi() call always results in Error: Invalid column specification no matter how I specify the colnames.

Any help appreciated!

TimBMK commented 1 year ago

Okay, I found the error. It was actually on my side, with the function producing erroneous dates to subset on, thus passing an empty dataframe to pairwisepmi.

So for anyone trying to pass columns with {{ embrace }} to functions like pairwise_pmi(), pairwise_cor() etc within a function or loop: Use the pairwisecor(), pairwisepmi() etc. functions. They can handle the {{ embrace }}. The "normal" function without the additional _ are just wrappers around them checking the column specifications, producing errors with {{ embrace }} (or with !!as.name() ).

juliasilge commented 1 year ago

Thanks for the info @TimBMK! I did some refactoring of how these functions use NSE a while back but still need to do more to be aligned with all the available ways to use rlang currently. In the meantime, your workaround looks good. 👍