65

I want to create a class in R, should I use S3 or S4 class?

I read a lot of different things about them, is there one superior to the other one?

RockScience
  • 17,932
  • 26
  • 89
  • 125
  • They are two different concepts. What are you trying to do? – Roman Luštrik Jun 23 '11 at 07:42
  • I want to manipulate objects: create them, extract attributes or slots and eventually define methods for them – RockScience Jun 23 '11 at 07:45
  • 1
    I have to say that I prefer the objectName@attributeName of S4 rather than attr(objectName, attributeName) of S3 – RockScience Jun 23 '11 at 07:46
  • Like I said, S4 is a whole other beast and to think that it will make your programming easier, than be my guest. :) See http://stackoverflow.com/questions/4143611/sources-on-s4-objects-methods-and-programming-in-r for a discussion on resources about S4 objects. – Roman Luštrik Jun 23 '11 at 07:49
  • @Roman Luštrik: you don't think to be convinced? Thanks for the link – RockScience Jun 23 '11 at 07:51
  • 9
    @RockScience: You shouldn't be using objectName@attributeName, you should have a getter method. Always try to code to interface, not implementation. – geoffjentry Jun 23 '11 at 14:52

2 Answers2

70

S3 can only dispatch on it's first argument, whereas S4 can dispatch on multiple arguments. If you want to be able to write methods for function foo that should do different things if given an object of class "bar" or given objects of class "bar" and "foobar", or given objects of class "barfoo" and "foobar", then S4 provides a far better way to handle such complexities.

S3 is very simple and easy to implement, but isn't really a formal object oriented system. That simplicity comes at the cost of enforcing that objects belonging to a class have the correct components/slots etc. With S3 I can do things like class(obj) <- "lm" and method dispatch will use methods for the "lm" class when passed obj, but there is no guarantee that obj really is an object of class "lm".

S3 is easy to implement, document and requires less extra knowledge on the part of the programmer.

Which to use can only be something you can decide. Doug Bates (2003) has said, for example, that for new projects he would use S4 over S3. I haven't yet had a need to use anything other than S3 methods.

So I would sit down and think about the sorts of classes and methods you want to operate on those classes. Think about the functionality you want, and that will probably point towards one system or the other.

Douglas Bates. Converting packages to S4. R News, 3(1):6-8, June 2003

Gavin Simpson
  • 170,508
  • 25
  • 396
  • 453
  • 6
    Are they computationally comparable? Is S4 any faster? – timothy.s.lau Dec 15 '14 at 08:00
  • 3
    I think S3 can be much faster - I've been achieving 50x speed-ups by porting time-critical code to S3. Two major reasons: a) creating new class members has little overhead in S3 (can just change the 'class' attribute) and b) if necessary you can choose to avoid method dispatch overhead by writing e.g. mean.Date(x) instead of mean(x). But would be interested to hear if anyone has any advice speeding up S4 code. – Peter Harrison Mar 02 '18 at 09:49
  • R7 -- which takes parts of both s3 and s4 might be of interest: https://rconsortium.github.io/OOP-WG/ – Bryan Shalloway Jul 24 '22 at 05:23
10

What about Reference Classes? it's not S3 and some consider it syntactical sugar on top of S4, but it's a lot of sugar.

For a short example, check this answers to a question I asked here a few months ago.

I still have to find out how to write Rd files for such classes.

Community
  • 1
  • 1
mariotomo
  • 9,438
  • 8
  • 47
  • 66
  • 3
    @RockScience: If you want reference classes, you can read more about their potential uses here http://stackoverflow.com/questions/5137199/what-is-the-significance-of-the-new-reference-classes – Ari B. Friedman Jul 20 '11 at 07:40
  • Cool. Thanks for your link to the question you asked a few months ago :-) – Ari B. Friedman Jul 20 '11 at 16:17