Chapter 3
Object-Oriented Programming in R
3.1 Introduction
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 dierent
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 define 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 ecifi-
cation from the specification 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 first, called S3, is documented in Chambers and Hastie (1992)
while the second, S4, was first 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.

Get R Programming for Bioinformatics now with O’Reilly online learning.

O’Reilly members experience live online training, plus books, videos, and digital content from 200+ publishers.