Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Variable not found with data mask

Tags:

r

tidyeval

library(rlang)
myquo <- quo((Temp - 32) / 1.8)
eval_tidy(myquo, data = as_data_mask(datasets::airquality)) # works
e <- as_env(datasets::airquality, parent = global_env())
eval_tidy(myquo, data = as_data_mask(list(), parent = e))   # error

I expected Temp to be found in e. What am I doing wrong?

PS: I have R version 3.5.0 and tested this with both latest CRAN and GitHub version of {rlang}.

like image 668
F. Privé Avatar asked Aug 13 '18 21:08

F. Privé


1 Answers

I think the documentation might have been updated since the question was asked, but for new visitors, the relevant part of the rlang documentation for as_data_mask is:

parent Soft-deprecated. This argument no longer has any effect. The parent of the data mask is determined from either:

  • The env argument of eval_tidy()
  • Quosure environments when applicable

So in the case of eval_tidy(myquo, data = as_data_mask(list(), parent = e)) the env of eval_tidy and the quosure env on myquo are both the global env, and the data mask itself is empty, hence why Temp is not found.

eval_tidy(myquo, data = as_data_mask(datasets::airquality)) 

works but has an unnecessary call in it, since the data argumnet of eval_tidy will convert a data.frame to a data mask anyway, so the easiest way would be.

eval_tidy(myquo, data = datasets::airquality) 

On the other hand, if you really want to specify the environment explicitly in eval_tidy, you could use expr instead of quo

myexpr <- expr((Temp - 32) / 1.8)
eval_tidy(myexpr, data = as_data_mask(list(), parent = e)) # still fails since parent is overridden 
eval_tidy(myexpr, data = list(), env = e) # works since there's no quosure env to override env
like image 67
Tom Greenwood Avatar answered Oct 21 '22 15:10

Tom Greenwood