Object-Oriented Programming in : S3, S4, and Reference Classes

Object-Oriented Programming in R: S3, S4, and Reference Classes

Fish 512: Super-Advanced R University of Washington Cole Monnahan

May 21, 2014 Object-Oriented Programming in R: S3, S4, and Reference Classes

About this lecture

This lecture covers the basics of classes in R, with the caveats that I am not a computer scientist nor an expert in object-oriented programming. The following slides were distilled from: 1 Hadley Wickham’s “OO field guide” in his Advanced R Programming book: http://adv-r.had.co.nz/OO-essentials.html. 2 Chapter 9 of The Art of R Programming. 3 A lecture by Nicholas Christian, http://www.pitt.edu/ njc23/Lecture4.pdf. 4 Wikipedia (of course!) Simula 67 language (1960s) introduced formal concepts, then extended by the language By the 1990s had developed into the dominant programming methodology Is one of many programming paradigms, and has pros/cons. OOP is built into R so we use it constantly, even if we’re unaware of it

Object-Oriented Programming in R: S3, S4, and Reference Classes

Background and History

Object-oriented programming (OOP) is a programming paradigm for designing modular, reusable software systems. OOP is built into R so we use it constantly, even if we’re unaware of it

Object-Oriented Programming in R: S3, S4, and Reference Classes

Background and History

Object-oriented programming (OOP) is a programming paradigm for designing modular, reusable software systems. Simula 67 language (1960s) introduced formal concepts, then extended by the Smalltalk language By the 1990s had developed into the dominant programming methodology Is one of many programming paradigms, and has pros/cons. Object-Oriented Programming in R: S3, S4, and Reference Classes

Background and History

Object-oriented programming (OOP) is a programming paradigm for designing modular, reusable software systems. Simula 67 language (1960s) introduced formal concepts, then extended by the Smalltalk language By the 1990s had developed into the dominant programming methodology Is one of many programming paradigms, and has pros/cons. OOP is built into R so we use it constantly, even if we’re unaware of it Object-Oriented Programming in R: S3, S4, and Reference Classes

Goals of this lecture

1 Get comfortable with the basic (often convoluted) terminology used in OOP. 2 Understand how R uses classes currently, and be able to recognize them. 3 Know when and how to write your own S3 class. Rapid Development Lessen the semantic gap between code and users. Powerful debugging/testing tools for inspecting system performance. Ease of maintenance Changes can be localized to just an object and its component parts, reducing potential side effects. It is clear what changes to a function will do throughout the code. Ease of evolution OOP makes it easy to reuse and extend components as needed.

Object-Oriented Programming in R: S3, S4, and Reference Classes

Philosophy

These following goals of object-oriented programming were taken from wikipedia, and apply broadly the the OOP paradigm. For R, some of these will not necessarily be true. We’ll discuss how this philosophy applies to R at the end of the lecture. Lessen the semantic gap between code and users. Powerful debugging/testing tools for inspecting system performance. Ease of maintenance Changes can be localized to just an object and its component parts, reducing potential side effects. It is clear what changes to a function will do throughout the code. Ease of evolution OOP makes it easy to reuse and extend components as needed.

Object-Oriented Programming in R: S3, S4, and Reference Classes

Philosophy

These following goals of object-oriented programming were taken from wikipedia, and apply broadly the the OOP paradigm. For R, some of these will not necessarily be true. We’ll discuss how this philosophy applies to R at the end of the lecture. Rapid Development Changes can be localized to just an object and its component parts, reducing potential side effects. It is clear what changes to a function will do throughout the code. Ease of evolution OOP makes it easy to reuse and extend components as needed.

Object-Oriented Programming in R: S3, S4, and Reference Classes

Philosophy

These following goals of object-oriented programming were taken from wikipedia, and apply broadly the the OOP paradigm. For R, some of these will not necessarily be true. We’ll discuss how this philosophy applies to R at the end of the lecture. Rapid Development Lessen the semantic gap between code and users. Powerful debugging/testing tools for inspecting system performance. Ease of maintenance Ease of evolution OOP makes it easy to reuse and extend components as needed.

Object-Oriented Programming in R: S3, S4, and Reference Classes

Philosophy

These following goals of object-oriented programming were taken from wikipedia, and apply broadly the the OOP paradigm. For R, some of these will not necessarily be true. We’ll discuss how this philosophy applies to R at the end of the lecture. Rapid Development Lessen the semantic gap between code and users. Powerful debugging/testing tools for inspecting system performance. Ease of maintenance Changes can be localized to just an object and its component parts, reducing potential side effects. It is clear what changes to a function will do throughout the code. Object-Oriented Programming in R: S3, S4, and Reference Classes

Philosophy

These following goals of object-oriented programming were taken from wikipedia, and apply broadly the the OOP paradigm. For R, some of these will not necessarily be true. We’ll discuss how this philosophy applies to R at the end of the lecture. Rapid Development Lessen the semantic gap between code and users. Powerful debugging/testing tools for inspecting system performance. Ease of maintenance Changes can be localized to just an object and its component parts, reducing potential side effects. It is clear what changes to a function will do throughout the code. Ease of evolution OOP makes it easy to reuse and extend components as needed. Object-Oriented Programming in R: S3, S4, and Reference Classes

Key definitions

Class: Defines the behavior of objects by describing their attributes and their relationship to other classes. lm is a class in R Object: An instance of a class, containing data and methods.

A fitted model is an object of class lm Method: A function associated with a class. predict.lm is a method acting on lm objects. Object-Oriented Programming in R: S3, S4, and Reference Classes

Key definitions

Class: Defines the behavior of objects by describing their attributes and their relationship to other classes. lm is a class in R Object: An instance of a class, containing data and methods.

A fitted model is an object of class lm Method: A function associated with a class. predict.lm is a method acting on lm objects. Object-Oriented Programming in R: S3, S4, and Reference Classes

Key definitions

Class: Defines the behavior of objects by describing their attributes and their relationship to other classes. lm is a class in R Object: An instance of a class, containing data and methods.

A fitted model is an object of class lm Method: A function associated with a class. predict.lm is a method acting on lm objects. S4: Similar to S3 but with three key differences: 1 Has a formal definition, which provides more control 2 Can use (not covered here) 3 A new “slot” syntax: @ instead of $. Reference Classes: Newest type (since 2.12) Quite different from S3/S4, more like traditional languages. Uses “message-passing” style. Base type: The internal C-level types, built using C code. Not covered in this lecture.

Object-Oriented Programming in R: S3, S4, and Reference Classes

Overview of classes in R

S3: The original and most common type in R. Uses “generic-function” style. Very casual: it has no formal definition. Reference Classes: Newest type (since 2.12) Quite different from S3/S4, more like traditional languages. Uses “message-passing” style. Base type: The internal C-level types, built using C code. Not covered in this lecture.

Object-Oriented Programming in R: S3, S4, and Reference Classes

Overview of classes in R

S3: The original and most common type in R. Uses “generic-function” style. Very casual: it has no formal definition. S4: Similar to S3 but with three key differences: 1 Has a formal definition, which provides more control 2 Can use multiple dispatch (not covered here) 3 A new “slot” syntax: @ instead of $. Base type: The internal C-level types, built using C code. Not covered in this lecture.

Object-Oriented Programming in R: S3, S4, and Reference Classes

Overview of classes in R

S3: The original and most common type in R. Uses “generic-function” style. Very casual: it has no formal definition. S4: Similar to S3 but with three key differences: 1 Has a formal definition, which provides more control 2 Can use multiple dispatch (not covered here) 3 A new “slot” syntax: @ instead of $. Reference Classes: Newest type (since 2.12) Quite different from S3/S4, more like traditional languages. Uses “message-passing” style. Object-Oriented Programming in R: S3, S4, and Reference Classes

Overview of classes in R

S3: The original and most common type in R. Uses “generic-function” style. Very casual: it has no formal definition. S4: Similar to S3 but with three key differences: 1 Has a formal definition, which provides more control 2 Can use multiple dispatch (not covered here) 3 A new “slot” syntax: @ instead of $. Reference Classes: Newest type (since 2.12) Quite different from S3/S4, more like traditional languages. Uses “message-passing” style. Base type: The internal C-level types, built using C code. Not covered in this lecture. Object-Oriented Programming in R: S3, S4, and Reference Classes pryr: A package for prying back the surface of R

Currently available from github: install_github("pryr"). Provides useful tools for inspecting R’s objects. df <- data.frame(x=1:10,y = letters[1:10]) class(df) # A data frame is a class ## [1] "data.frame" otype(df) # ... of type S3 ## [1] "S3" otype(df$x) # A numeric vector isn't ## [1] "primitive" We want a more practical way to create objects, so we define a constructor function. growth <- function(ID, sex, age, len){ out <- list(ID=ID, sex=sex, data=data.frame(age=age, len=len)) class(out) <- "growth" invisible(out) } g <- growth("ID1", "Male",0:10,(0:10)^2) class(g) ## [1] "growth" otype(g) ## [1] "S3" The constructor function returns a list, which is an object of class growth.

Object-Oriented Programming in R: S3, S4, and Reference Classes

S3 classes: How to create one It’s remarkably simple: x <- "Hello World" class(x) <- "fake class" Object-Oriented Programming in R: S3, S4, and Reference Classes

S3 classes: How to create one It’s remarkably simple: x <- "Hello World" class(x) <- "fake class" We want a more practical way to create objects, so we define a constructor function. growth <- function(ID, sex, age, len){ out <- list(ID=ID, sex=sex, data=data.frame(age=age, len=len)) class(out) <- "growth" invisible(out) } g <- growth("ID1", "Male",0:10,(0:10)^2) class(g) ## [1] "growth" otype(g) ## [1] "S3" The constructor function returns a list, which is an object of class growth. Object-Oriented Programming in R: S3, S4, and Reference Classes

Further definitions

Constructor: A function which generates objects of a class. Generic Function: A function which behaves differently depending on the class of the called object. Dispatch: The process of determining the correct method to call based on an object type Polymorphism: Same function call leads to different operations for objects of different classes. Inheritance: Extending a class to create a more specialized class. The new class inherits existing methods. Encapsulation: Data and methods are packaged in each object. The generic function detects the class type and dispatches (calls) the class-specific method The concept that the same function leads to different operation for different classes is called polymorphism. Example: plot is a generic function and plot.lm is a member function. Others: print, summary, etc.

Object-Oriented Programming in R: S3, S4, and Reference Classes

S3: How to create a method

For S3 classes, methods belong to a generic function, rather than an object. The concept that the same function leads to different operation for different classes is called polymorphism. Example: plot is a generic function and plot.lm is a member function. Others: print, summary, etc.

Object-Oriented Programming in R: S3, S4, and Reference Classes

S3: How to create a method

For S3 classes, methods belong to a generic function, rather than an object. The generic function detects the class type and dispatches (calls) the class-specific method Example: plot is a generic function and plot.lm is a member function. Others: print, summary, etc.

Object-Oriented Programming in R: S3, S4, and Reference Classes

S3: How to create a method

For S3 classes, methods belong to a generic function, rather than an object. The generic function detects the class type and dispatches (calls) the class-specific method The concept that the same function leads to different operation for different classes is called polymorphism. Object-Oriented Programming in R: S3, S4, and Reference Classes

S3: How to create a method

For S3 classes, methods belong to a generic function, rather than an object. The generic function detects the class type and dispatches (calls) the class-specific method The concept that the same function leads to different operation for different classes is called polymorphism. Example: plot is a generic function and plot.lm is a member function. Others: print, summary, etc. Object-Oriented Programming in R: S3, S4, and Reference Classes

Further definitions

Constructor: A function which generates objects of a class. Generic Function: A function which behaves differently depending on the class of the called object. Dispatch: The process of determining the correct method to call based on an object type Polymorphism: Same function call leads to different operations for objects of different classes. Inheritance: Extending a class to create a more specialized class. The new class inherits existing methods. Encapsulation: Data and methods are packaged in each object. Object-Oriented Programming in R: S3, S4, and Reference Classes

Further definitions

Constructor: A function which generates objects of a class. Generic Function: A function which behaves differently depending on the class of the called object. Dispatch: The process of determining the correct method to call based on an object type Polymorphism: Same function call leads to different operations for objects of different classes. Inheritance: Extending a class to create a more specialized class. The new class inherits existing methods. Encapsulation: Data and methods are packaged in each object. Object-Oriented Programming in R: S3, S4, and Reference Classes

Further definitions

Constructor: A function which generates objects of a class. Generic Function: A function which behaves differently depending on the class of the called object. Dispatch: The process of determining the correct method to call based on an object type Polymorphism: Same function call leads to different operations for objects of different classes. Inheritance: Extending a class to create a more specialized class. The new class inherits existing methods. Encapsulation: Data and methods are packaged in each object. Object-Oriented Programming in R: S3, S4, and Reference Classes

S3: How to examine methods

We can tell a method is generic by looking at the source for UseMethod: plot ## function (x, y, ...) ## UseMethod("plot") ## ## And see all of plot’s methods with head(methods(plot)) ## [1] "plot.acf" "plot.ACF" "plot.augPred" ## [4] "plot.coef.mer" "plot.compareFits" "plot.correspondence"

All of these methods are associated with different S3 classes in R. Object-Oriented Programming in R: S3, S4, and Reference Classes

S3: How to create a method

To create a new method for our growth class, we need to associate it with a generic function. plot.growth <- function(object){ d <- object$data plot(d$age, d$len, type="b", xlab="Age", ylab="Length (cm)", main=paste("Growth for", object$ID)) }

Note here that we’ve simply defined a function called plot.growth, and have in no way told R to associate it with our class. Instead, when we call plot it will automatically use dispatch to look for plot.growth (and now find it). Object-Oriented Programming in R: S3, S4, and Reference Classes

Calling a method R automatically looks up our new function (using dispatch) and uses our function. plot(g)

Growth for ID1

● 100

● 80

● 60 ●

40 ●

Length (cm) ●

20 ● ● ● ● ● 0

0 2 4 6 8 10

Age Object-Oriented Programming in R: S3, S4, and Reference Classes

Exercise: Create a print method for growth

What happens if we enter g in the console?

We want to change this behavior of our class (like most common R objects). To do this, create a print method that outputs a summary of a growth object. g ## Growth object w/ 11 rows ## ID = ID1 ## Sex = Male ## age len ## 1 0 0 ## 2 1 1 ## 3 2 4 ## ... Object-Oriented Programming in R: S3, S4, and Reference Classes

S3: Creating a generic function

We can also create a new generic function. f <- function(x) UseMethod("f") f.a <- function(x) "Class a" f(structure(list(), class= "a")) ## [1] "Class a" as well as defining a default method for when the class is unknown.

f.default <- function(x) "Unknown class" f(structure(list(), class= "c")) ## [1] "Unknown class" Object-Oriented Programming in R: S3, S4, and Reference Classes

S3: Subclasses and Inheritance

Another key characteristic of OO-programming is that we can derive a new, more specific class from existing classes. The new subclass inherits properties from the superclass.

f(structure(list(), class=c("b", "a"))) ## [1] "Class a"

The object has two classes: a (the superclass) and b (the subclass). f.b doesn’t exist, so dispatch then looks for f.a and finds it. This is inheritance (in S3). Inheritance provides a transparent way of reusing code. Lots of familiar R functions use inheritance, e.g.

class(glm(log(mpg)~log(disp), data=mtcars)) ## [1] "glm" "lm" Object-Oriented Programming in R: S3, S4, and Reference Classes

Further definitions

Constructor: A function which generates objects of a class. Generic Function: A function which behaves differently depending on the class of the called object. Dispatch: The process of determining the correct method to call based on an object type Polymorphism: Same function call leads to different operations for objects of different classes. Inheritance: Extending a class to create a more specialized class. The new class inherits existing methods. Encapsulation: Data and methods are packaged in each object. Object-Oriented Programming in R: S3, S4, and Reference Classes

S3: Advantages?

We could have just defined plot.growth and not used a class. Where is the advantage in using a class? Users need to only remember the familiar plot instead of plot.growth. The programmer can control how the user interacts with data (via the object’s methods). The concept that a class keeps its data and methods isolated from the user is called encapsulation. (Do you know what is stored in a lm object? Do you care?) Encapsulation is a key characteristic of OOP. As we’ll see shortly, it can also provide extra safety with S4 classes. Object-Oriented Programming in R: S3, S4, and Reference Classes

Further definitions

Constructor: A function which generates objects of a class. Generic Function: A function which behaves differently depending on the class of the called object. Dispatch: The process of determining the correct method to call based on an object type Polymorphism: Same function call leads to different operations for objects of different classes. Inheritance: Extending a class to create a more specialized class. The new class inherits existing methods. Encapsulation: Data and methods are packaged in each object. Object-Oriented Programming in R: S3, S4, and Reference Classes

S4 basics

S3 was the original class in R. S4 was developed later with the goal of safety (more on this later), and is much too rich to cover in depth here1. S4 works very similarly to S3 except it has formal definitions of classes. These definitions describe their fields and inheritance structures. Method dispatch can be based on multiple arguments (just one is used in S3). Fields are extracted using the special operator @ and are called “slots”.

1See John Chambers’ book Software for Data Analysis for more info Object-Oriented Programming in R: S3, S4, and Reference Classes

S4: Recognizing them The same tools (pryr functions and class) are used for S4 classes. mod <- lme4::lmer(Yield~1|Batch, data=lme4::Dyestuff, REML= FALSE) otype(mod) ## [1] "S4" head(methods(class="lme")) ## [1] "anova.lme" "coef.lme" "extractAIC.lme" "fitted.lme" ## [5] "formula.lme" "logLik.lme" The is function lists all classes an object inherits from. (Remember a an object is both classes) is(mod) ## [1] "lmerMod" "merMod"

The authors of lme4 have used inheritance from two classes. The getGenerics and getClasses() return the S4 generic functions and classes. Object-Oriented Programming in R: S3, S4, and Reference Classes

S4: Defining a class

S4 classes have some key properties:

1 Name. A class identifier, which should be UpperCamelCase (by convention). 2 Slots. A named list of names and permitted classes for slots. For instance with growth we might have list(ID=''character'', age=''numeric''). 3 Contains. A string explicitly giving the classes it inherits from (i.e. contains). 4 Validity. A method that tests if an object is valid. 5 Prototype. A method that defines default slot values. The inflexibility of S4 classes reduces errors because the information is known, and valid. Object-Oriented Programming in R: S3, S4, and Reference Classes

S4: Creating a class

S4 comes with functions (in the methods package) for working explicitly with S4 objects. Consider the following example. setClass("Person", slots= list(name= "character", age= "numeric")) setClass("Employee", slots= list(boss= "Person"), contains= "Person") setClass is used to define a new class. The new function creates new objects, but an explicit constructor function is preferred. alice <- new("Person", name= "Alice", age= 40) john <- new("Employee", name= "John", age= 20, boss = alice) And we can use @ or slot to extract fields. According to Hadley, @ is equivalent to $ and slot to [[. alice@age ## [1] 40 slot(alice, "age") ## [1] 40 Object-Oriented Programming in R: S3, S4, and Reference Classes

S4: Defining a method

The function setMethod is used to create a method for a class. Note that show is the equivalent of print for S4 objects. setMethod(f="show", signature="Person", definition= function(object){ cat(object@name, "is a person of age", object@age," \n") }) ## [1] "show" alice ## Alice is a person of age 40

While setGeneric is used to create new S4 generic functions. Object-Oriented Programming in R: S3, S4, and Reference Classes

S4: Added safety

S3 does not provide safety as do most OO languages. For instance, there are no checks for consistency of names or data types. g$ID <- plot g$fakename <- 'test'

A function can be assigned to ID and a nonsensical list element can be created and R will not complain. For most OO systems, the class is a way of protecting the object data and methods (via encapsulation).

One of the major design goals of S4 was to provide more “safety” than S3. Object-Oriented Programming in R: S3, S4, and Reference Classes

S4: Added safety

Let’s repeat the same operations on an S4 object... alice@name <- plot ## Error: assignment of an object of class "function" is not valid for @’name’ in an object of class "Person"; is(value, "character") is not TRUE alice@fakename <- 'test' ## Error: ’fakename’ is not a slot in class "Person" and R rejects these as invalid automatically, protecting the integrity of the object. S4 also provides machinery for validating S4 objects, see ?validObject for more information.

Bottom line: The strict definitions and checking of S4 give the programmer direct control of how/when objects can be changed, reducing potential bugs. Object-Oriented Programming in R: S3, S4, and Reference Classes

Reference Classes Reference classes are the newest type in R (as of 2.12) and are fundamentally different from S3 and S4 classes.

1 Methods belong to objects, not generic functions 2 Objects are mutable: changing a copy can change the original (!!!). 3 Instead of S4 slots, RC has fields that are referenced with $. RC is much closer to what we see in other languages (C++, Java, etc.). Creating them is very similar to S4 classes: Account <- setRefClass("Account", fields= list(balance= "numeric"), methods= list( withdraw= function(x) {balance <<- balance-x }, deposit= function(x) {balance <<- balance+x }))

Notice that our constructor contains the methods, instead of defining them as cases of a generic function. Object-Oriented Programming in R: S3, S4, and Reference Classes

Using RC classes

The new function (built into the class constructor) creates objects: a <- Account$new(balance= 100) otype(a) ## [1] "RC" Since the methods belong to the object, we get the new syntax: a$deposit(100) a$balance ## [1] 200 Object-Oriented Programming in R: S3, S4, and Reference Classes

Mutability in reference classes

Normally if a copy of an object is made, we would expect the copy to be untouched from changes to the original. If we try this with a RC object... b <-a b$balance ## [1] 200 a$balance <-0 b$balance ## [1] 0 ... the copy changed with the original! This property is called mutability and applies only for RC classes. You can get around mutability by using the copy function: c <- a$copy(). Mutability is not a bad or good thing, it is just another tool. Object-Oriented Programming in R: S3, S4, and Reference Classes

Big picture review2

2Image taken from: http://skat.ihmc.us Object-Oriented Programming in R: S3, S4, and Reference Classes

Which class system to use? S3: Common, simple and convenient to use, and suffices for most purposes. Most of Hadley’s OO code is written in S3, and google recommends it, going so far as to say “avoid S4 objects and methods when possible”.

S4: J. Chambers (R developer), thinks S4 is needed for “clear and reliable software”. Hadley also notes that S4 may be more appropriate for “complicated systems of interrelated objects,” citing the Matrix package’s 110 classes and 18 generic functions.

RC: Currently uncommon in base R, but when mutability is required, then RC is the only option. For experienced programmers, RC will be more familiar behavior to other OO languages. (I’m unsure when you should use RC – consult further documentation before trying it...) Object-Oriented Programming in R: S3, S4, and Reference Classes

Which class system to use? S3: Common, simple and convenient to use, and suffices for most purposes. Most of Hadley’s OO code is written in S3, and google recommends it, going so far as to say “avoid S4 objects and methods when possible”.

S4: J. Chambers (R developer), thinks S4 is needed for “clear and reliable software”. Hadley also notes that S4 may be more appropriate for “complicated systems of interrelated objects,” citing the Matrix package’s 110 classes and 18 generic functions.

RC: Currently uncommon in base R, but when mutability is required, then RC is the only option. For experienced programmers, RC will be more familiar behavior to other OO languages. (I’m unsure when you should use RC – consult further documentation before trying it...) Object-Oriented Programming in R: S3, S4, and Reference Classes

Which class system to use? S3: Common, simple and convenient to use, and suffices for most purposes. Most of Hadley’s OO code is written in S3, and google recommends it, going so far as to say “avoid S4 objects and methods when possible”.

S4: J. Chambers (R developer), thinks S4 is needed for “clear and reliable software”. Hadley also notes that S4 may be more appropriate for “complicated systems of interrelated objects,” citing the Matrix package’s 110 classes and 18 generic functions.

RC: Currently uncommon in base R, but when mutability is required, then RC is the only option. For experienced programmers, RC will be more familiar behavior to other OO languages. (I’m unsure when you should use RC – consult further documentation before trying it...) When to use it? Package development seems like the most common use for classes in R. I suspect typical R users do not need classes for routine tasks like data analysis, plotting, etc. (Thoughts?)

Which class to use? S3 seems like a logical starting point, especially if you’re new to OO concepts. (Thoughts?)

... the end.

Object-Oriented Programming in R: S3, S4, and Reference Classes

Discussion points

How does understanding classes improve our use of R?: Everything in R is an object of a class. But as users, we aren’t forced to use it... so why learn it? (Thoughts)? Which class to use? S3 seems like a logical starting point, especially if you’re new to OO concepts. (Thoughts?)

... the end.

Object-Oriented Programming in R: S3, S4, and Reference Classes

Discussion points

How does understanding classes improve our use of R?: Everything in R is an object of a class. But as users, we aren’t forced to use it... so why learn it? (Thoughts)?

When to use it? Package development seems like the most common use for classes in R. I suspect typical R users do not need classes for routine tasks like data analysis, plotting, etc. (Thoughts?) Object-Oriented Programming in R: S3, S4, and Reference Classes

Discussion points

How does understanding classes improve our use of R?: Everything in R is an object of a class. But as users, we aren’t forced to use it... so why learn it? (Thoughts)?

When to use it? Package development seems like the most common use for classes in R. I suspect typical R users do not need classes for routine tasks like data analysis, plotting, etc. (Thoughts?)

Which class to use? S3 seems like a logical starting point, especially if you’re new to OO concepts. (Thoughts?)

... the end.