[R] S3 and S4 classes

Laurent Faisnel laurent.faisnel at ariase.com
Wed Jul 23 14:53:56 CEST 2003


Hi helpers,

I've been programming in R for a few months now but I still have doubts 
about my code - I would like it to be completely S4-compatible. The 
current code works fine but is probably 'unclean'.
I read the interesting article in the last R News and it helped me 
understand the difference on the whole between S3 and S4 classes, but I 
need a practical example. Could anyone point me out what's S3-like in 
the following sample and why it is not fully S4-compatible ? (any other 
comment welcome).

Thanks in advance.
Laurent

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

# I define a class
setClass("MyClass", representation(mynumber="numeric"));

# the initilization method
setMethod("initialize","MyClass",
          function(.Object)
          {
            .Object at mynumber <- 10;
            return(.Object);
          }
);

# a function that objects of this class have
perform <- function(.Object) UseMethod("perform", .Object);

# the associated method
setMethod("perform","MyClass",
          function(.Object)
          {
            .Object at mynumber <- .Object at mynumber + 10;
            return(.Object);
           }
);




More information about the R-help mailing list