How should I use the uq() function inside a package?

695 views Asked by At

I'm puzzled by the behaviour of the uq() function. The behavior is not the same when I use uq() or lazyeval::uq().

Here is my reproducible example :

First, I generate a fake dataset

library(tibble)
library(lazyeval)
fruits <- c("apple", "banana", "peanut")
price <- c(5,6,4)
table_fruits <- tibble(fruits, price)

Then I write a toy function, toy_function_v1, using only uq() :

toy_function_v1 <- function(data, var) {
  lazyeval::f_eval(f = ~ uq(var), data = data)
}

and a second function using lazyeval::uq() :

toy_function_v2 <- function(data, var) {
  lazyeval::f_eval(f = ~ lazyeval::uq(var), data = data)
}

Surprisingly, the output of v1 and v2 is not the same :

> toy_function_v1(data = table_fruits, var = ~ price)
[1] 5 6 4
> toy_function_v2(data = table_fruits, var = ~ price)
price

Is there any explanation ?

I know it's a good practice to use the syntaxe package::function() to use the function inside a new package. So what's the best solution in that case ?

Here is my session_info :

> devtools::session_info()
Session info ----------------------------------------------------------------------------------------------------------------------------------------------------
 setting  value                       
 version  R version 3.3.1 (2016-06-21)
 system   x86_64, linux-gnu           
 ui       RStudio (1.0.35)            
 language (EN)                        
 collate  C                           
 tz       <NA>                        
 date     2016-11-07                  

Packages --------------------------------------------------------------------------------------------------------------------------------------------------------
 package    * version    date       source                          
 Rcpp         0.12.7     2016-09-05 CRAN (R 3.2.3)                  
 assertthat   0.1        2013-12-06 CRAN (R 3.2.2)                  
 devtools     1.12.0     2016-06-24 CRAN (R 3.2.3)                  
 digest       0.6.10     2016-08-02 CRAN (R 3.2.3)                  
 lazyeval   * 0.2.0.9000 2016-10-14 Github (hadley/lazyeval@c155c3d)
 memoise      1.0.0      2016-01-29 CRAN (R 3.2.3)                  
 tibble     * 1.2        2016-08-26 CRAN (R 3.2.3)                  
 withr        1.0.2      2016-06-20 CRAN (R 3.2.3) 
1

There are 1 answers

0
PAC On BEST ANSWER

It's just a bug in the uq() function. The issue is open on Github : https://github.com/hadley/lazyeval/issues/78.