When does it pay off to use S4 methods in R programming

Solution 1:

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.

Solution 2:

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.

Solution 3:

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!

Solution 4:

I learnt S4 in order to extend the Spatial (sp) classes for animal track data. It was the best choice (most consistent, general and closely matching to many GIS definitions) from the available options to avoid writing everything required from scratch. I don't find S4 as onerous as many people say, but I'm now used to exploring the underlying structure of objects like this. The performance is good too, I think it can be done well, though when done poorly there are performance traps.

If spatial data is of interest to you, spatstat is a good example of how to do a lot of similar things to sp in S3, though (as with seemingly everything spatial . . .) there's hardly ever clean analogies between data structures in different softwares.