On Tue, 2005-03-08 at 15:36 -0800, Vadim Ogranovich wrote:
> Hi,
>  
> Is it possible to modify an object in the parent.env (as opposed to
> re-bind)? Here is what I tried:
>  
> > x = 1:3
> # try to modify the first element of x from within a new environment
> > local(get("x", parent.env(environment()))[1] <- NA)
> Error in eval(expr, envir, enclos) : Target of assignment expands to
> non-language object
> 
> # On the other hand retrieval works just fine
> > local(get("x", parent.env(environment()))[1])
> [1] 1


You could try this:

> x <- 1:3

mod.x <- function(x)
{
  eval.parent(substitute(x[1] <- NA))
}

> x
[1] 1 2 3

> mod.x(x)

> x
[1] NA  2  3


See ?eval.parent for more information and variations on this.

HTH,

Marc Schwartz

______________________________________________
[email protected] mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html

Reply via email to