[R] Class & Method, S3 / S4

Martin Morgan mtmorgan at fhcrc.org
Thu Mar 2 14:23:17 CET 2006

Again, R has a different paradigm from what you're used to. R is a
'pass by value' language. So 'obj' inside the method is a *copy* of
testObj, and your assignment changes the 'value' slot of the copy. An
R way of doing this might be

setMethod("test", signature=c("connect"),
  function( obj ) {
    obj at value <- obj at value / 2
    ## and other manipulations...

testObj <- test( testObj )

I 'know' these things by reading Chambers' Programming with Data, and
by looking at exsiting code (ok, and maybe some other ways, too
;). There are some example packages suggested in this thread


If you have an R source distribution, look in, for instance,


At the R command prompt, do something like


A warning, I guess, is that looking at complicated code (and the help
pages for the methods package) can be confusing without the kind of
foundation that Chambers' book provides. Maybe others on the list will
provide some hints for introductory documentation on S4 classes and

Hope that's enough to get you going!


"Dominik Locher" <dominik.locher at bondsearch.ch> writes:

> Hi Martin
> Thanks a lot for your short example. If you input
> test(testObj)
> it will return
> 22
> However, how is it possible that the value will be saved in the object
> i.e. (does not work currently!!??)
> setMethod("test", signature=c("connect"),
>   function( obj ) { obj at value<-obj at value / 2 })
> so that test(testObj) will save a new value to obj at value or 
> testObj at value will return 22.
> Thanks.
> Nik
> ______________________________________________
> R-help at stat.math.ethz.ch mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html

More information about the R-help mailing list