Hi, I probably misunderstand the entire concept of environments in R. I now have a list of environments in which each has a number of variables. What I want to do is to apply a function on each of those environments which adds additional variables to that particular environment. Is there a way to set the environment of the function so that also the variables newly created in the function are added to the environment? Right now I do environment(FN) <- oldEnvironment before I call the function and the function operates on the old environments variables but the newly created ones are not in the old environment. Thank you for considering my question! Werner
Here L is a list of environments and the lapply creates z in each of them: L <- list(e1 = new.env(), e2 = new.env()) junk <- lapply(L, function(e) e$z <- 1) ls(L$e1) # "z" You might also want to look at the proto package: http://hhbio.wasser.tu-dresden.de/projects/proto/ On 4/3/06, Werner Wernersen <pensterfuzzer at yahoo.de> wrote:> Hi, > > I probably misunderstand the entire concept of > environments in R. I now have a list of environments > in which each has a number of variables. What I want > to do is to apply a function on each of those > environments which adds additional variables to that > particular environment. Is there a way to set the > environment of the function so that also the variables > newly created in the function are added to the > environment? > > Right now I do > environment(FN) <- oldEnvironment > before I call the function and the function operates > on the old environments variables but the newly > created ones are not in the old environment. > > Thank you for considering my question! > Werner > > ______________________________________________ > R-help at stat.math.ethz.ch mailing list > https://stat.ethz.ch/mailman/listinfo/r-help > PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html >
On 4/3/2006 1:44 PM, Werner Wernersen wrote:> Hi, > > I probably misunderstand the entire concept of > environments in R. I now have a list of environments > in which each has a number of variables. What I want > to do is to apply a function on each of those > environments which adds additional variables to that > particular environment. Is there a way to set the > environment of the function so that also the variables > newly created in the function are added to the > environment? > > Right now I do > environment(FN) <- oldEnvironment > before I call the function and the function operates > on the old environments variables but the newly > created ones are not in the old environment.The line environment(FN) <- oldEnvironment sets the closure of the function, i.e. the parent of the evaluation environment. Assignments within the function are made within the evaluation environment, which usually disappears when the function returns. (An exception to this: if you create and return a function within a function, the closure of the new one is by default the evaluation environment of the old one, so it will outlast the outer function call.) Generally there is no way to set the evaluation environment of a function, but you could write a function that copied things from it somewhere else, e.g. copyObjects <- function(fromEnvironment, toEnvironment) { names <- ls(fromEnvironment) for (n in names) assign(n, get(n, env=fromEnvironment), env=toEnvironment) } and then putting copyObjects(environment(), parent.env(environment())) in a function will copy all the locals into the parent. More simply, you can do assignments directly to the parent environment using assign("name", env=parent.env(environment()))