Skip to content

"assign" statement in S-Plus

4 messages · kathie, David Winsemius, Douglas Bates +1 more

#
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.


--------------------------------------------------------

  prime <- function(x)
    {
    1*(abs(x) < chuber)
    }
  assign("prime",prime,frame=1)

---------------------------------------------------------


Any comments will be greatly appreciated.

Kathryn Lord
#
On Dec 23, 2008, at 1:41 PM, kathie wrote:

            
?assign   # ????
#
On Tue, Dec 23, 2008 at 1:38 PM, David Winsemius <dwinsemius at comcast.net> wrote:

            
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.
#
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.