SV: [R] Documentation of S3 and S4 classes, inheritance

From: Søren Højsgaard <>
Date: Tue 24 May 2005 - 08:31:11 EST

I am sure that I do not have the answer to your question - but I would like to add to your views regarding the S4-issue. I've been involved in the gRbase package for graphical modelling in R. The first version was implemented in S4 but now we have made a roll-back to using S3 methods.  

While, I can see that S3 is not really comprehensive enough for 'proper' object oriented programming it certainly has other virtues. One such is the documentation: There are 1-2 pages to read, thats it - and you are up and running!! Plain, easy and simple. One of the things that S3 is lacking is clearly multiple inheritance, and that is, as I understand it, a feature of S4. Another feature of S4 is, again as I understand things (apologies if I am wrong) that objects are protected from being altered (in terms of adding new slots etc). Again a very nice feature...  

There are, however, several problems with S4. The first is 'how to find documentation about it'? Sure, the green book by Chambers is a reference, but really what I am missing is a 5-page document saying 'this is how you should do it'. I claim that most package developers are statisticians who want to share their work with others - not computer scientists with strong views about developments of formal languages. Another issue we came across when working with the gRbase package was that it was too 'painful' to integrate existing S3 code into an S4 package. (Surely, some people could be tempted to reply to this as 'yes, but you should write everything in S4 anyway - sorry, but you'll have to start all over again...' However, such views could be dangerous to the whole 'R-open-source-half-way-anarchistic-philosophy' as I see it). I would expect that it would be possible to improve S3 in a simpler way - perhaps S3.5 could be an option..  

My first experience with statistical computing was with xlisp-stat which has a very nice (as I remember it) object system which is well explained in Luke Tierneys book. I wonder if that could serve as an inspiration for anyone who is interested in writing an 'S4 for dummies'...  

Best regards
Søren Højsgaard            

Fra: på vegne af Ross Boylan Sendt: ma 23-05-2005 23:41
Til: r-help
Emne: [R] Documentation of S3 and S4 classes, inheritance

I'd like to have a class A that computes a likelihood, and a subclass B that computes the same likelihood by sometimes throws in an additional term (B includes measurement error).

So B's likelihood needs to call A's, and then (sometimes) multiply by an additional term.

It sounds as if, in the S3 scheme, NextMethod is supposed to do this:

like.A <- function(stuff) compute value
like.B <- function(stuff) extraFactor*NextMethod() ?

but, after some study of both the Language Manual (2.1) and the online help of NextMethod I can't tell exactly what it does. In particular, I'm not sure if it returns to the calling function, or how it decides which flavor to call. The language manual says the method choice depends on the values of .Generic and .Class, but doesn't say how those get filled in. I would expect .Class to be the current class, in which case the call is recursive. The online help says "'NextMethod' invokes the next method (determined by the class)" but it doesn't say how it is determined. One ambiguity is whether "class" refers to the class (B) or the class attribute ("B", "A").

I think the documentation could be clearer.

Now, probably none of this matters to me, since several sources (including the online help for S3)indicate that S4 classes are preferred.

I found the documentation for S4 initially elusive. As far as I can tell, it isn't even mentioned in the "R Language Definition." While the fact that S4 is defined in a package makes clear it is not formally part of the base language, this is not a very good way to get people to use S4.

I think by now I've tracked down docs on S4, including the intro/overview at

Finally, I'm a bit concerned that one article mentioned that S4 inheritance, in practice, is used mostly for data, not methods (Thomas Lumley, R News 4(1), June 2004: p. 36). Am I going down a road I shouldn't travel?

Ross Boylan                                      wk:  (415) 502-4031
530 Parnassus Avenue (Library) rm 115-4
Dept of Epidemiology and Biostatistics           fax: (415) 476-9856
University of California, San Francisco
San Francisco, CA 94143-0840                     hm:  (415) 550-1062

______________________________________________ mailing list PLEASE do read the posting guide!
______________________________________________ mailing list PLEASE do read the posting guide!
Received on Tue May 24 08:36:22 2005

This archive was generated by hypermail 2.1.8 : Fri 03 Mar 2006 - 03:32:01 EST