Object-Oriented Programming in R
Object-oriented programming (OOP) has become a widely used and valu-
able tool for software engineering. Much of its value derives from the fact
that it is often easier to design, write and maintain software when there is
some clear separation of the data representation from the operations that are
to be performed on it. In an OOP system, real physical things (like airline
passengers or the data from a microarray experiment) are generally repre-
sented by classes, and metho ds (functions) are written to handle the diﬀerent
manipulations that need to be performed on the objects.
The views that many people have of OOP have been based largely on expo-
sure to languages like Java, where the system can be described as class-centric.
In a class-centric system, classes deﬁne objects and are repositories for the
metho ds that act on those objects. In contrast, languages such as Dylan
(Shalit, 1996), Common Lisp (Steele, 1990), and R separate the class sp eciﬁ-
cation from the speciﬁcation of generic functions, and could be described as
b eing function-centric systems.
R currently supports two internal OOP systems, and several others are
available as add-on packages. In this chapter we discuss the two internal sys-
tems. The ﬁrst, called S3, is documented in Chambers and Hastie (1992)
while the second, S4, was ﬁrst described in Chambers (1998) and later up-
dated in Chambers (2008). R has become very popular and is now being used
for projects that require substantial software engineering as well as its con-
tinued widespread use as an interactive environment for data analysis. This
essentially means that there are two masters – reliability and ease of use. S3
is indeed easy to use, but can be made unreliable through nothing other than
bad luck, or a poor choice of names, and hence is not a suitable paradigm
for constructing large systems. S4, on the other hand, is better suited for
developing large software projects but has an increased complexity of use.
Freidman et al. (2001) list four general elements that an object-oriented
programming language should support.
objects: encapsulate state information and control behavior.
classes: describe general properties for groups of objects.