[R] "assign" statement in S-Plus

Greg Snow Greg.Snow at imail.org
Tue Dec 23 22:11:32 CET 2008


If I remember correctly, frame 1 is the evaluation frame that comes into existence with each evaluation, then goes away at the end of the evaluation.  The main use of it is to get past the fact that S-PLUS searches the current functions variables, but not the ones the current function is nested in, so a person could assign something to frame 1, then call another function and that function could look for the variable in frame 1.

To do the same thing in R depends on what you are trying to accomplish.  In some cases the lexical scoping of R makes this completely unneeded.  If function g is defined inside of function f and function f assigns a value to 'prime' before function g is called, then function g will be able to see 'prime' in function f without any use of assign or frame 1.  If function g needs to change the value of 'prime', then <<- will work.

If function g is not defined inside of function f and they both need to see the same variable (and it cannot be passed as an argument), then one way to do this is to just insure that both functions inherit from the same environment.

-- 
Gregory (Greg) L. Snow Ph.D.
Statistical Data Center
Intermountain Healthcare
greg.snow at imail.org
801.408.8111

> -----Original Message-----
> From: r-help-bounces at r-project.org [mailto:r-help-bounces at r-
> project.org] On Behalf Of Douglas Bates
> Sent: Tuesday, December 23, 2008 1:22 PM
> To: David Winsemius
> Cc: r-help at r-project.org
> Subject: Re: [R] "assign" statement in S-Plus
> 
> On Tue, Dec 23, 2008 at 1:38 PM, David Winsemius
> <dwinsemius at comcast.net> wrote:
> 
> > On Dec 23, 2008, at 1:41 PM, kathie wrote:
> 
> >>
> >> Dear R users...
> >>
> >> I need to change the S+ code below to R code.
> >>
> >> I am wondering if there is a R statement equivalent for "assign"
> statement
> >> in S-plus.
> >>
> >
> > ?assign   # ????
> 
> The problem is not with the assign function per se but with the use of
> frame = 1 as an argument to assign.  R uses evaluation environments
> and S used evaluation frames.  frame = 1 was special, although I have
> forgotten which one it was.
> 
> Kathie, I would try removing the call to assign altogether and seeing
> if the rest of your S-PLUS script works as intended.
> 
> >
> >
> >>
> >> --------------------------------------------------------
> >>
> >>  prime <- function(x)
> >>   {
> >>   1*(abs(x) < chuber)
> >>   }
> >>  assign("prime",prime,frame=1)
> >>
> >> ---------------------------------------------------------
> >>
> >>
> >> Any comments will be greatly appreciated.
> >>
> >> Kathryn Lord
> >> --
> >> View this message in context:
> >> http://www.nabble.com/%22assign%22-statement-in-S-Plus-
> tp21149319p21149319.html
> >> Sent from the R help mailing list archive at Nabble.com.
> >>
> >> ______________________________________________
> >> R-help at r-project.org mailing list
> >> https://stat.ethz.ch/mailman/listinfo/r-help
> >> PLEASE do read the posting guide
> >> http://www.R-project.org/posting-guide.html
> >> and provide commented, minimal, self-contained, reproducible code.
> >
> > ______________________________________________
> > R-help at r-project.org mailing list
> > https://stat.ethz.ch/mailman/listinfo/r-help
> > PLEASE do read the posting guide http://www.R-project.org/posting-
> guide.html
> > and provide commented, minimal, self-contained, reproducible code.
> >
> 
> ______________________________________________
> R-help at r-project.org mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide http://www.R-project.org/posting-
> guide.html
> and provide commented, minimal, self-contained, reproducible code.



More information about the R-help mailing list