Skip to content

"<<-" assignment no long work in class methods

2 messages · Gang Liang, Wolski

#
Hi-

I used to use "<<-" to do assignment inside a class method, and
just found that now it is broken in R 2.0. For example, the
following code

-----------------------------------------------------------------------
setClass( "myclass", representation(x="numeric") )
setGeneric("incrXByOne", function(obj) standardGeneric("incrXByOne"))
setMethod( "incrXByOne", "myclass", function(obj) obj at x <<- obj at x + 1 )

incrXByOne( new("myclass") )
-----------------------------------------------------------------------

will give an error message like:

   Error in incrXByOne(new("myclass")) : Object "obj" not found
   ## R failed to trace the object back to the GlobalEnv...

It used to work under R1.7 - 1.9. I don't know whether this is a
bug or a new feature...

Anyone can recommend a workaround?

Thanks, Gang

-----------------------------
debian unstable, kernel 2.6.8
platform i386-pc-linux-gnu
arch     i386
os       linux-gnu
system   i386, linux-gnu
status
major    2
minor    0.0
year     2004
month    10
day      04
language R
#
Gang Liang wrote:

            
Hi,

What (my guess) you want to define is:

setMethod( "incrXByOne", "myclass", function(obj) {obj at x <- obj at x + 1;obj})

You do not need "<<-" to assign to a function argument? Its in my view 
even erroneous.

This S code gives an error too.

test <- function(x)
{
x$bla<<-x$bla + 1
}
test(1)
Error in test(1) : Object "x" not found

/E