Forcing multiple variables using a character vector

Is there a way to force the evaluation of several variables to use a character vector?

eg:

x = 1 y = 2 

instead of this:

 force( x ) force( y ) 

do something like this:

 force( ls() ) 
+4
source share
1 answer

Replacing force() with eval(as.symbol()) will do the trick:

 ## Modified from an example in ?force (ht @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] - semantic sugar: just character evaluation will do the same

+5
source

All Articles