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 ...
Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Read now
Unlock full access