S4 provides a formal approach to functional OOP. The underlying ideas are similar to S3 (the topic of Chapter 13), but implementation is much stricter and makes use of specialised functions for creating classes (
setClass()), generics (
setGeneric()), and methods (
setMethod()). Additionally, S4 provides both multiple inheritance (i.e. a class can have multiple parents) and multiple dispatch (i.e. method dispatch can use the class of multiple arguments).
An important new component of S4 is the slot, a named component of the object that is accessed using the specialised subsetting operator
@ (pronounced at). The set of slots, and their classes, forms an important part of the definition of an S4 class.
Section 15.2 gives a quick overview of the main components of S4: classes, generics, and methods.
Section 15.3 dives into the details of S4 classes, including prototypes, constructors, helpers, and validators.
Section 15.4 shows you how to create new S4 generics, and how to supply those generics with methods. You’ll also learn about accessor functions which are designed to allow users to safely inspect and modify object slots.
Section 15.5 dives into the full details of method dispatch in S4. The basic idea is simple, then it rapidly gets more complex once multiple inheritance and multiple dispatch are combined.
Section 15.6 discusses the interaction between S4 and S3, showing you how to use them together.
Like the other OO chapters, the focus here will be on how S4 works, not how to deploy it most effectively. If you do want to use it in practice, there are two main challenges:
There is no one reference that will answer all your questions about S4.
R’s built-in documentation sometimes clashes with community best practices.
As you move towards more advanced usage, you will need to piece together needed information by carefully reading the documentation, asking questions on StackOverflow, and performing experiments. Some recommendations:
The Bioconductor community is a long-term user of S4 and has produced much of the best material about its effective use. Start with S4 classes and methods taught by Martin Morgan and Hervé Pagès, or check for a newer version at Bioconductor course materials.
Martin Morgan is a member of R-core and the project lead of Bioconductor. He’s a world expert on the practical use of S4, and I recommend reading anything he has written about it, starting with the questions he has answered on stackoverflow.
John Chambers is the author of the S4 system, and provides an overview
of its motivation and historical context in Object-oriented programming, functional programming and R (Chambers 2014). For a fuller exploration of S4, see his book Software for Data Analysis (Chambers 2008).
All functions related to S4 live in the methods package. This package is always available when you’re running R interactively, but may not be available when running R in batch mode, i.e. from
Rscript54. For this reason, it’s a good idea to call
library(methods) whenever you use S4. This also signals to the reader that you’ll be using the S4 object system.
Chambers, John M. 2008. Software for Data Analysis: Programming with R. Springer.
Chambers, John M. 2014. “Object-Oriented Programming, Functional Programming and R.” Statistical Science 29 (2): 167–80. https://projecteuclid.org/download/pdfview_1/euclid.ss/1408368569.
This is a historical quirk introduced because the methods package used to take a long time to load and
Rscriptis optimised for fast command line invocation.↩︎