This is a trivial example I set up to see if I could pass an environment and use the variables in it (this is for a function that will be called many times and might need to use a lot of variables that won't be changing, so it seemed more sensible to use an environment).
Here's the code: ######################### #The outer function run.internal.env <- function(x) { in.env <- new.env() assign('x', x, envir=in.env) print(eval(x^2, envir=in.env)) print(ls(envir=in.env)) return(use.internal.env(in.env)) } #The inner function use.internal.env <- function(env) { print(ls(envir=env)) return(eval(x^2, envir=env)) } ########################## Now if I type run.internal.env(2), my output looks like [1] 4 [This is the evaluation in the outer routine] [1] "x" [This is the "ls" from the outer routine] [1] "x" [This is the "ls" from the inner routine] Error in eval(x^2, envir=env): object "x" not found [??? It was listed, so why can't it be evaluated???] If there's anything called "x" in the global environment, it will evaluate that x^2 instead, e.g. x <- 1:3 run.internal.env(2) [1] 4 [This is the evaluation in the outer routine] [1] "x" [This is the "ls" from the outer routine] [1] "x" [This is the "ls" from the inner routine] [1] 1 4 9 Is there any way to force it to use the environment I actually want, and keep all the values I assigned in there? [[alternative HTML version deleted]] ______________________________________________ R-help@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code.