[Rd] [R] Re: [S] Old style and new style classes

Frank E Harrell Jr fharrell at virginia.edu
Tue Apr 1 10:31:31 MEST 2003

On Tue, 1 Apr 2003 14:33:39 +0100
Jonathan Swinton <jswinton at proteom.com> wrote:

> I have been writing quick and dirty S-plus code for years, but for a recent project I took the plunge, bought Venables and Ripley's S Programming, and implemented a class library using new style classes (in S-Plus 6.0). It worked quite nicely and I am wondering about making more routine use of them. Before I make much more of an investment, I have a few questions, both factual and subjective, that I'd be grateful for opinions on. 
> 1) I don't use R at the moment, but I might, and might want to target users who do. The documentation at http://cran.r-project.org/doc/manuals/R-lang.pdf seems to suggest that R 1.6.2 supports only old style classes, but http://developer.r-project.org/methodsPackage.html implies that new style classes can be used with the aid of a methods package from John Chambers. Venables and Ripley only describe old-style classes for R and add in their online supplement that there are 'small changes' between S-Plus and R for new style classes. Can the new style class mechanism be used in practice transparently and portably between S-Plus and R?
> 2) The s-news archives hold a few complaints about the design of the new style classes scattered over the last few years. On mature reflection, do people have opinions about the practical workability of old or new style classes for implementing reusable, object oriented code?
> 3) There are also some hints (eg top line of http://developer.r-project.org/classIssues.html) that new style classes have not been committed to by some active developers. Is this true? Do new style classes have an active future? Is R committed to them?   Insightful?   
> I use old-style (S version3 engine) and new-style (S version4) classes in the sense of Venables and Ripley. 
>  -------------------------------------
>  Dr Jonathan Swinton
>  Proteom Ltd 
>  Babraham Hall
>  Babraham
>  Cambridge CB2 4AT
>   www.proteom.com

I think you are asking the right questions.  SV4 has caused severe problems in S-Plus mainly due to lack of multiple inheritance.  I have been burned MANY times when running an object through a method (e.g., imputation) that sticks on an additional class.  Right now I am teaching a class using S-Plus 6 with my impute function in the Hmisc library and strange error messages are driving the students crazy.  So far all my tests in the R 1.7 development version indicate that R, unlike S-Plus, is backward compatible with old-style classes and methods.  But if you want to be compatible with S-Plus it would be nice if we could just avoid new classes and methods.  Unfortunately S-Plus does not let us do that either, because multiple old-style classes is not allowed any more.  

In the bigger picture,  I am still not really convinced that new classes and methods offer advantages to data analysts.  The new methods are not as flexible and take longer to program.  I frequently stick on a new element of a fit object list for debugging or for enhancing functions that others wrote.  You can't really do this with SV4.  Also, new methods and classes have made some code non-transportable across systems (primarily to older versions of S-Plus).  There are now packages on CRAN that use slots to address elements while other versions of these same packages use list elements.  Hence some code retrieves details of fit objects using @ and some needs to use $.  I know that retrieving pieces of fit objects is inelegant (extractor methods should be written) but it is very common that I don't want to take the time to write an extractor method for every application, and I sure hate to waste time sensing whether the object was created using old vs. new style classes.

I know that new classes and methods are more elegant from a computer science perspective, but I analyze data and like to use what works (e.g., I really like both Perl and Python).  New methods provide much more safety.  I have found though that I don't need this kind of protection from myself.  I have plenty of other problems to worry about.

That's my $.02 worth.

Frank E Harrell Jr              Prof. of Biostatistics & Statistics
Div. of Biostatistics & Epidem. Dept. of Health Evaluation Sciences
U. Virginia School of Medicine  http://hesweb1.med.virginia.edu/biostat

R-help at stat.math.ethz.ch mailing list

More information about the R-devel mailing list