[R] Equivalent of S> assign( , ,frame=1)
Thomas Lumley
thomas at biostat.washington.edu
Thu Apr 27 19:54:52 CEST 2000
On Thu, 27 Apr 2000, Olivier Renaud wrote:
> Hi,
> I'm new to R, and I certainly miss an obvious point. I looked in the doc
> and the archieve, but could not find what is the R equivalent of the S
> assign( , ,frame=1). I guess the question is like what is the equivalent
> of the S frames and how to access them. As an example:
R doesn't have an equivalent of frame 1. It is possible to store objects
in an environment, which can either be referenced by a variable or stored
in the search path.
So one way to do this is
attach(NULL,name="Frame1")
initial <-function()
{
frame1<-match("Frame1",search())
assign ("x", 2, env=frame1)
assign ("y", 3, env=frame1)
}
YOu can now refer to x anywhere. It does not override a variable called
'x' in the global environment, though.
Alternatively you can store things in an environment off the search path
and explicitly refer to them by location
frame1<-new.env()
initial <-function() {
assign ("x", 2, env=frame1)
assign ("y", 3, env=frame1)
}
subrout<-function(){
get("x",env=frame1)+get("y",env=frame1)
}
Alternatively, you can just pass things explicitly into the
functions. There isn't much efficiency loss, as arguments aren't copied
unless they are modified.
-thomas
Thomas Lumley
Assistant Professor, Biostatistics
University of Washington, Seattle
> oneprog <- function()
> {
> initial()
> res <- subrout()
> return(res)
> }
>
> initial <- function()
> {
> assign ("x", 2, frame=1)
> assign ("y", 3, frame=1)
> }
>
>
> subrout <- function()
> return(x+y)
>
> This works perfectly with S. I know that if I put subrout inside oneprog
> and replace the call to initial by a simple x <- 2;y <-3, it will work
> on R (and not on S). But initial is a routine I use in many different
> programs, and therefore I don't want it inside a given one. Any help?
> More general comments on this way of programming are also welcomed.
>
>
> Olivier
> --
> Olivier Renaud
> Department of Statistics - Sequoia Hall - 390 Serra Mall
> STANFORD, CA, 94305-4065 mailto:renaud at stat.stanford.edu
> http://www.stanford.edu/~renaud
> -.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-
> 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
> _._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._
-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-
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