Is there a way to force the evaluation of multiple variables using a character vector?
for example:
x = 1
y = 2
instead of doing this:
force( x )
force( y )
do something like this:
force( ls() )
Is there a way to force the evaluation of multiple variables using a character vector?
for example:
x = 1
y = 2
instead of doing this:
force( x )
force( y )
do something like this:
force( ls() )
Replacing force()
with eval(as.symbol())
will do the trick:
## Modified from an example in ?force (h.t. @flodel)
g <- function(x,y) {
lapply(ls(), function(X) eval(as.symbol(X)))
function() x+y
}
lg <- vector("list", 4)
for (i in 1:2) for (j in 1:2) lg[[i+j-1]] <- g(i,j)
lg[[1]]()
# [1] 2
This works because, as noted in ?force
:
[force] is semantic sugar: just evaluating the symbol will do the same thing