{{ embrace }} doesn't work with pairwise_cor()
sebastian-fox opened this issue · 3 comments
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)
Any chance there's a solution to this yet? I'm running into the same problem with pairwise_pmi(); using pairwise_pmi_() doesn't help either. The calls work outside the function, but not when utilized inside a function. Specifically, when passing the arguments directly to pairwise_pmi_() 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!
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 pairwise_pmi_.
So for anyone trying to pass columns with {{ embrace }} to functions like pairwise_pmi(), pairwise_cor() etc within a function or loop: Use the pairwise_cor_(), pairwise_pmi_() 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() ).
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. 👍