[R] What is the most efficient way to assign to PARTS of obje cts in other frames/environments?
Raubertas, Richard
richard_raubertas at merck.com
Fri May 17 20:02:35 CEST 2002
For nested function calls it appears that a combination of environment()
and '<<-' will also work:
f1 <- function() {
x <- rep(NA,4) # Object to be modified by function f2
environment(f2) <- sys.frame(1) # Make f1's environment available to f2
f2(2) # Changes 2nd element of x
print(x)
}
f2 <- function(i) {
# Following works because "<<-" searches through all available
# environments for the LHS symbol, and does assignment where the
# symbol is found. "x" is in the environment of f1, which was
# made available to f2 above.
x[i] <<- i^2
NULL
}
f1() # prints NA 4 NA NA
Of course this requires that f1 anticipate that f2 will want to
modify its objects; i.e., f1 and f2 must be written as a pair, to
know about each other.
Richard Raubertas
Biometrics Research
Merck & Co.
> -----Original Message-----
> From: Thomas Lumley [mailto:tlumley at u.washington.edu]
> Sent: Friday, May 17, 2002 11:44 AM
> To: oehl_list at gmx.de
> Cc: r-help at stat.math.ethz.ch; Martin.Schlather at uni-bayreuth.de
> Subject: Re: [R] What is the most efficient way to assign to PARTS of
> objects in other frames/environments?
>
>
> On Fri, 17 May 2002 oehl_list at gmx.de wrote:
>
> > Can please someone familiar with the R internals explain on
> the following:
> >
> > PR#1434 from r-bugs clarifies that
> >
> > assign("a[1]", x, SomeOtherFrame)
> >
> > or
> >
> > assign("a$a", x, SomeOtherFrame)
> >
> > will NOT assign to an object 'a' in the other frame BUT
> create a new object
> > called 'a[1]' resp. 'a$a'.
> >
> > This leads to the following question: what is the most
> efficient way to
> > assign to PARTS of objects in other frames/environments?
> >
> > Any way I know appears to have some problems associated:
> >
> > # doing it locally involves several calls and probably
> copies the whole
> > object
> > local.copy <- get("a", SomeOtherFrame)
> > local.copy$a <- x
> > assign("a", local.copy, SomeOtherFrame)
> >
> > # using eval fails because x might be unknown in the other frame
> > > t1 <- function(){
> > + # x is locally defined
> > + x <- 9
> > + eval(parse(text="a$a <- x"), envir=globalenv())
> > + }
> > > t1()
> > Error in eval(expr, envir, enclos) : Object "x" not found
> >
>
> Instead of
>
> assign("a$a",x,envir=SomeOtherFrame)
> you can do
> eval(substitute(a$a<-localx,localx=x),envir=SomeOtherFrame)
>
>
> -thomas
>
> Thomas Lumley Asst. Professor, Biostatistics
> tlumley at u.washington.edu University of Washington, Seattle
>
> -.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.
> -.-.-.-.-.-.-.-.-
> r-help mailing list -- Read
> http://www.ci.tuwien.ac.at/~hornik/R/R-FAQ.html
> Send "info", "help", or "[un]subscribe"
> (in the "body", not the subject !) To:
> r-help-request at stat.math.ethz.ch
> _._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._.
> _._._._._._._._._
>
------------------------------------------------------------------------------
Notice: This e-mail message, together with any attachments, contains information of Merck & Co., Inc. (Whitehouse Station, New Jersey, USA) that may be confidential, proprietary copyrighted and/or legally privileged, and is intended solely for the use of the individual or entity named on this message. If you are not the intended recipient, and have received this message in error, please immediately return this by e-mail and then delete it.
==============================================================================
-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-
r-help mailing list -- Read http://www.ci.tuwien.ac.at/~hornik/R/R-FAQ.html
Send "info", "help", or "[un]subscribe"
(in the "body", not the subject !) To: r-help-request at stat.math.ethz.ch
_._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._
More information about the R-help
mailing list