Chapter 10. Object-Oriented Programming
At its heart, R is a functional programming language. But the
R system includes some support for object-oriented programming (OOP). OOP
has become the most popular paradigm for organizing computer software; it’s
used in most modern programming languages (Java, C#, Ruby, and Objective C,
among others) and in quite a few old ones (Smalltalk, C++). Many R packages
are written using R Objects, including the core statistics package, lattice
, and ggplot2
.
You don’t need to use object-oriented programming techniques to work with R. Writing functional programs is a fine practice. Many people believe that it is easier to write programs using functional techniques, particularly when multiple parts of a program run concurrently; this has led to a resurgence of new functional languages like Scala and Clojure. I like functional programs but find object-oriented techniques convenient for representing complicated objects (such as statistical models or charts).
R includes two different mechanisms for object-oriented programming. As you may recall, the R language is derived from the S language. S’s object-oriented programming system evolved over time. Around 1990, S version 3 (S3) introduced class attributes that allowed single-argument methods. Many R functions (such as the statistical modeling software) were implemented using S3 methods, so S3 methods are still around today. In S version 4 (S4), formal classes and methods were introduced that allowed multiple ...
Get R in a Nutshell, 2nd Edition now with the O’Reilly learning platform.
O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.