[R] Fwd: Evaluating a function within a pre-defined environment?

David Reiss dreiss at systemsbiology.org
Wed Dec 9 22:54:28 CET 2009

Hi all,

I have a somewhat confusing question that I was wondering if someone
could help with. I have a pre-defined environment with some variables,
and I would like to define a function, such that when it is called, it
actually manipulates the variables in that environment, leaving them
to be examined later. I see from the R language definition that

"When a function is called, a new environment (called the evaluation
environment) is created, whose enclosure (see Environment objects) is
the environment from the function closure. This new environment is
initially populated with the unevaluated arguments to the function; as
evaluation proceeds, local variables are created within it."

So basically, I think I am asking if it is possible to pre-create my
own "evaluation environment" and have it retain the state that it was
in at the end of the function call?


e <- new.env()
e$x <- 3
f <- function(xx) x <- x + xx

can I then call f(2) and have it leave e$x at 5 after the function
returns? I know that

environment(f) <- e

goes part of the way, but I would like to let the function also write
to the environment.

Thanks for any advice.


More information about the R-help mailing list