[Rd] S3 vs S4 classes
jmc at r-project.org
Sun Feb 18 04:59:32 CET 2007
Rossi, Peter E. wrote:
> I have developed a package, bayesm, which uses existing classes of
> objects. I would like
> to add a new class corresponding to objects from this package.
> I have been reading about classes and all sources tell me that I should
> so-called "new" or S4 classes.
> However, a major purpose of defining a class for my package would be to
> add methods to the existing generic functions: print, plot, and
> summary. My understanding
> is that these functions work with the "old" or S3 classes.
> If I want to use S4 classes, do I need to write new generic functions or
> is there
> as way to "extend" the existing generics to work with S4 classes and
If you define a method for any function that is currently not a generic,
the function is automatically turned into a generic with the existing
function becoming the default method. So you can define methods for any
In particular, defining a method for an S3 generic makes that generic
the default method for the S4 generic function.
Should you do this? The main advantages are that you can then know
something about the methods (i.e., there is some metadata that allows
one to list the methods, ask what method would be selected for
particular arguments, etc.), and also, methods can be defined for more
than one argument. The advantages increase if you want to define new
classes of objects, since these also have a formal definition and some
guarantee that the objects really do behave as the class says they do.
Basically, the S3 methods were a "quick and dirty" implementation that
came out with the "Statistical Methods in S" project and book. They
have been intensively worked up for those applications, and the easy way
out is still to use them for that purpose.
Over the (15) years since that project got going, a more serious effort
produced a more extensive view of classes and methods.
You should not feel pressed to choose one or another. Some of the older
generation are still using Fortran after all.
But the newer version was developed with the goal of having better
software. Some people don't like them, which is their privilege.
People have their own goals, and their own prior investments.
> I may
> be using the wrong word here "extend" but I hope that everyone
> understands the point.
> If I have to write new generic functions, it is a simply matter of
> registering the pre-existing
> methods based on S3 objects? Or do you basicaly have to start from
> scratch -- i.e.
> the two types of classes, methods and generic functions are distinct?
> peter r
> Peter E. Rossi
> Joseph T. and Bernice S. Lewis Professor of Marketing and Statistics
> Editor, Quantitative Marketing and Economics
> Rm 353, Graduate School of Business, U of Chicago
> 5807 S. Woodlawn Ave, Chicago IL 60637
> Tel: (773) 702-7513 | Fax: (773) 834-2081
> R-devel at r-project.org mailing list
More information about the R-devel