I just updated to R-3.2.1, and am running into errors with code that ran previously on R-3.1.2 (based on this answer).
For example:
test <- list(bquote(x <- 10))
fenv <- environment()
rapply(test, evalq, envir = fenv)
The above code worked well before, but now gives the error:
Error in eval(substitute(expr), envir, enclos) : object 'X' not found
The expected behavior would be to define x
in fenv
. I am trying to figure out how to proceed, such that the code works on the current and previous version of R (if possible).
Interestingly, eval
gives the expected behavior in R-3.2.1 where it did not in R-3.1.2.
EDIT BASED ON COMMENTS WITH @BondedDust
The suggested rapply(list(test), eval, envir = fenv)
returns 10
in both versions but does not store x
in fenv
in R-3.1.2. The suggested code does, however, return 10
and store x
in fenv
in R-3.2.1, as desired.
The original suggestion by @BondedDust: rapply(list(test), evalq, envir = fenv)
gave the same error in R-3.2.1 as the code above in the original question.
I still hope to get to a solution that stores the x
variable in fenv
in both old and new versions of R.
EDIT TO SHOW COMPLEXITY
I have to use bquote
because I am generating the expressions from another function. This allows me to create complex calls to a data.table object without sending the data.table object to the function. Within the function, I can call a method, and then create the data.table expression and apply it to the data.table object without sending the data.table to an additional function. I have tried to simplify the problem below:
fenv <- environment()
func_list <- list(f1 = function(val) {e1 <- bquote(x <- .(val)); list(e1)})
test <- func_list[["f1"]](10)
rapply(test, evalq, envir = fenv)
The rapply
is necessary, because there are functions in the "func_list
" that return a list of expressions, not necessarily just one as in the above example. What I end up with is a nested list of expressions generated by bquote
that I need to evaluate in a given environment, such that the resulting object(s) (or changes to a data.table) are stored in the given environment.
I had this working in previous versions of R, and I am trying to figure out why the behavior has changed and how to rectify the code such that it works both in old and new versions of R.