OOP in R

Base R provides three OOP systems: S3, S4, and reference classes (RC):

  • S3 is R’s first OOP system, and is described in Statistical Models in S (Chambers and Hastie 1992). S3 is an informal implementation of functional OOP and relies on common conventions rather than ironclad guarantees. This makes it easy to get started with, providing a low cost way of solving many simple problems.

  • S4 is a formal and rigorous rewrite of S3, and was introduced in Programming with Data (Chambers 1998). It requires more upfront work than S3, but in return provides more guarantees and greater encapsulation. S4 is implemented in the base methods package, which is always installed with R.

    (You might wonder if S1 and S2 exist. They don’t: S3 and S4 were named according to the versions of S that they accompanied. The first two versions of S didn’t have any OOP framework.)

  • RC implements encapsulated OO. RC objects are a special type of S4 objects that are also mutable, i.e., instead of using R’s usual copy-on-modify semantics, they can be modified in place. This makes them harder to reason about, but allows them to solve problems that are difficult to solve in the functional OOP style of S3 and S4.

A number of other OOP systems are provided by CRAN packages:

  • R6 (Chang 2017) implements encapsulated OOP like RC, but resolves some important issues. In this book, you’ll learn about R6 instead of RC, for reasons described in Section 14.5.

  • R.oo (Bengtsson 2003) provides some formalism on top of S3, and makes it possible to have mutable S3 objects.

  • proto (Grothendieck, Kates, and Petzoldt 2016) implements another style of OOP based on the idea of prototypes, which blur the distinctions between classes and instances of classes (objects). I was briefly enamoured with prototype based programming (Wickham 2011) and used it in ggplot2, but now think it’s better to stick with the standard forms.

Apart from R6, which is widely used, these systems are primarily of theoretical interest. They do have their strengths, but few R users know and understand them, so it is hard for others to read and contribute to your code.

References

Bengtsson, Henrik. 2003. “The R.oo Package - Object-Oriented Programming with References Using Standard R Code.” In Proceedings of the 3rd International Workshop on Distributed Statistical Computing (Dsc 2003), edited by Kurt Hornik, Friedrich Leisch, and Achim Zeileis. Vienna, Austria: https://www.r-project.org/conferences/DSC-2003/Proceedings/. https://www.r-project.org/conferences/DSC-2003/Proceedings/Bengtsson.pdf.

Chambers, John M. 1998. Programming with Data: A Guide to the S Language. Springer.

Chambers, John M, and Trevor J Hastie. 1992. Statistical Models in S. Wadsworth & Brooks/Cole Advanced Books & Software.

Chang, Winston. 2017. R6: Classes with Reference Semantics. https://r6.r-lib.org.

Grothendieck, Gabor, Louis Kates, and Thomas Petzoldt. 2016. Proto: Prototype Object-Based Programming. https://CRAN.R-project.org/package=proto.

Wickham, Hadley. 2011. “Mutatr: Mutable Objects for R.” Computational Statistics 26 (3): 405–18. https://doi.org/10.1007/s00180-011-0235-7.