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

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


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.


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.


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!