[R] How to change the value of a class slot

Thomas Petzoldt thpe at hhbio.wasser.tu-dresden.de
Thu Jun 9 21:37:32 CEST 2005


Ross Boylan wrote:
> 
> I believe your example assumes that foo is updating the outer a by
> "cheating" and directly modifying enclosing environments.  (I figure it
> also needs to get the name of its actual argument to do this, which
> would also involve slightly dirty tricks.) This does seem to be the only
> way to do it in standard R.
> 
> In contrast, I wanted something like
> foo<-function(formalArg){ 
>   formalArg <- new value
>   return something  else
> }
> so that if I call foo(a), a has the new value after the call.
> Unfortunately for me, that doesn't work.  Call-by-value semantics imply
> that it can't.

What about call by reference like this:

library(proto)

arg <- proto(x=1)

foo<-function(formalArg){
   formalArg$x <- formalArg$x + 1.23
   #return something  else
}

foo(arg)

arg$x

[1] 2.23

Best regards

Thomas P.




More information about the R-help mailing list