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?

6
  • They are two different concepts. What are you trying to do? Jun 23, 2011 at 7:42
  • I want to manipulate objects: create them, extract attributes or slots and eventually define methods for them Jun 23, 2011 at 7:45
  • 1
    I have to say that I prefer the objectName@attributeName of S4 rather than attr(objectName, attributeName) of S3 Jun 23, 2011 at 7: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 stackoverflow.com/questions/4143611/… for a discussion on resources about S4 objects. Jun 23, 2011 at 7:49
  • 9
    @RockScience: You shouldn't be using objectName@attributeName, you should have a getter method. Always try to code to interface, not implementation. Jun 23, 2011 at 14:52

2 Answers 2

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

3
  • 6
    Are they computationally comparable? Is S4 any faster? Dec 15, 2014 at 8: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. Mar 2, 2018 at 9:49
  • R7 -- which takes parts of both s3 and s4 might be of interest: rconsortium.github.io/OOP-WG Jul 24, 2022 at 5: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.

2

Your Answer

Reminder: Answers generated by Artificial Intelligence tools are not allowed on Stack Overflow. Learn more

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.