[R] Change value of a slot of an S4 object within a method.

Joris Meys jorismeys at gmail.com
Wed Aug 25 17:56:32 CEST 2010


Dear all,

I have an S4 class with a slot "extra" which is a list. I want to be
able to add an element called "name" to that list, containing the
object "value" (which can be a vector, a dataframe or another S4
object)

Obviously

setMethod("add.extra",signature=c("PM10Data","character","vector"),
  function(object,name,value){
             object at extra[[name]] <- value
  }
)

Contrary to what I would expect, the line :
eval(eval(substitute(expression(object at extra[[name]] <<- value))))

gives the error :
Error in object at extra[[name]] <<- value : object 'object' not found

Substitute apparently doesn't work any more in S4 methods...

 I found a work-around by calling the initializer every time, but this
influences the performance. Returning the object is also not an
option, as I'd have to remember to assign that one each time to the
original name.

Basically I'm trying to do some call by reference with S4, but don't
see how I should do that. How would I be able to tackle this problem
in an efficient and elegant way?

Thank you in advance
Cheers
Joris

-- 
Joris Meys
Statistical consultant

Ghent University
Faculty of Bioscience Engineering
Department of Applied mathematics, biometrics and process control

tel : +32 9 264 59 87
Joris.Meys at Ugent.be
-------------------------------
Disclaimer : http://helpdesk.ugent.be/e-maildisclaimer.php



More information about the R-help mailing list