tags:

views:

289

answers:

5

Hi all,

I program regularly in R in a professional context, and I write packages for clients or co-workers as well. Some of the programmers here have a Java background and insist on doing everything the object-oriented way, using S4 methods. My experience on the other hand is that S4 implementations often perform worse and cause a lot more headache when trying to get the code do what you want it to do.

I definitely agree that in some cases, you have to be able to construct complex objects or append existing objects in a controlled manner. But most of the time, S4 implementations can easily be done using classic lists as well, without all the hassle like defining standardGeneric, methods, constructors, initializers and the likes.

When do you consider writing S4 implementations for R?

EDIT : For clarity, I do appreciate the answers and the discussion about OO in general in R. OOP can be done in numerous ways in R, but my question is really aimed at the added value of using S4 methods specifically.

+4  A: 

My experience is in line with yours, so I use S3 exclusively.

To clarify: S4 has some slick features (e.g. dispatch on multiple arguments and slot type-checking), but I have not encountered a situation where the features outweighed the costs. Examples of the costs include: any slot change requires a full object copy and (potentially worse) the on-going changes to S4 Methods.

In short, I like the idea behind S4 but I would wait for it to mature before using it in my own code.

Joshua Ulrich
My experience as well.
Maiasaura
+3  A: 

Great question! and I hope it generates some thoughtful discussion...

I've never used it, nor do I intend to for the following reasons:

  1. Performance
  2. I don't have the patience to completely understand S4 and it's relationship to S3.
  3. Syntactic suguar: I'd rather have object.method() than method(object).

I like suguar, what can I say!

Jeff
I too don't use S4 because I like `object.method()`. And Google's R style says: "Use S3 objects and methods unless there is a strong reason to use S4 objects or methods. A primary justification for an S4 object would be to use objects directly in C++ code. A primary justification for an S4 generic/method would be to dispatch on two arguments"
Vince
FWIW, why is Google the authority on R style? Shouldn't R-core be a higher authority on the matter? (not that R-core seems unified in their viewpoint on the matter, but ...). The fandom of the Google R style guide annoys me a bit for just this reason.
geoffjentry
@geoffjentry there's a part of me that feels exactly the say way... however, I'm happy people are thinking about style just a little bit. And if having GOOG logo on the PDF makes some economist (or statistician, etc) read it then I'm all for it. I'm tired of trying to read code that is so hard to parse because of the formating and style.
JD Long
I hear you. I don't even think it's bad or anything - I don't agree w/ everything but you know what they say about opinions. And yes, having *something* is better than nothing, but I've never really been comfortable with Google being the arbiters here considering it directly conflicts w/ the personal styles of various prominent R-core members. Heck, I was going to point to BioC's style guidelines on the argument that they should at least be considered "more authoritative" but they have the disclaimer that they shouldn't be viewed as such ;)
geoffjentry
geoffjentry
+1  A: 

S4 classes play a strong part in spatial statistics (sensu package sp), where converting from one type of data to the other seems seamless. The pitfall of this is debugging, which has been, in my experience, tedious at best. So far, I have managed with S3 but may consider using S4 in the future.

With time, as things get played around a lot, I believe they will play a strong role in at least core features of various fields of R (may that be spatial analysis, econometrics, environmetrics...)

Roman Luštrik
In fact, an ever growing part of R is recoded in S4 classes, but I experience more and more problems when using those packages. Documentation is directed at immediate use, but lacks for use in programming. As you know, I also experienced trouble with the evaluation of function arguments using S4 coded methods of a number of packages. So I tend to stay away from them, and I was hoping somebody could show me a good use.
Joris Meys
+1  A: 

Don't forget there's also R.oo (on CRAN) which provides a third way of doing OO in R. In my mind this provides an OO system that might be more familiar to programmers migrating from other systems - in particular instead of having generic functions (so that print(foo) then has to dispatch on the class of foo) the methods are tied to the object, so you'd do foo$print() - just as in python or C++ you'd do foo.print().

Spacedman
I've seen this before, but I always wondered what the extra added value was. Apart from semantics, I couldn't find any difference with S3 programming. But honestly, I haven't been digging deeply into it.
Joris Meys
+4  A: 

I'm assuming this doesn't directly apply to you, but if you're developing packages for Bioconductor there's an incentive to use S4 as they actively encourage it's use and have for the better part of a decade now - so all of the core packages make heavy use of S4.

I find all of the extra overhead to be a pain - the setGeneric, setMethod, dealing with NAMESPACE, etc. That being said, I find that the structure that it imposes, potential for extensibility and other such things can be worth it. As with everything, there are tradeoffs involved. I think it can be a lot cleaner - I dislike how S3 methods are simply disguised by naming convention (foo.class). All that being said, I tend to avoid using S4 heavily in my own code unless I'm being told to do so.

geoffjentry