[R] Documentation of S3 and S4 classes, inheritance

Ross Boylan ross at biostat.ucsf.edu
Mon May 23 23:41:55 CEST 2005


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 http://www.omegahat.org/RSMethods/.

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          ross at biostat.ucsf.edu
Dept of Epidemiology and Biostatistics           fax: (415) 476-9856
University of California, San Francisco
San Francisco, CA 94143-0840                     hm:  (415) 550-1062




More information about the R-help mailing list