Object Oriented Programming in R

Scott Chamberlain and Ted Laderas

2019-05-01

Context for Object Oriented Programming

What are Objects?

Why Use Object Oriented Programming?

Any of these is an opportunity to encapsulate data together into an object!

Object Oriented Design

Use Case (S4 and S6)

Results object for your package

We want people to do something with this object!

What do you want people to do with your results object?

Chambers

From Chambers:

The main ideas of object-oriented programming are also quite simple and intuitive:

  1. Everything we compute with is an object, and objects should be structured to suit the goals of our computations.
  2. For this, the key programming tool is a class definition saying that objects belonging to this class share structure defined by properties they all have, with the properties being themselves objects of some specified class.
  3. A class can inherit from (contain) a simpler superclass, such that an object of this class is also an object of the superclass.
  4. In order to compute with objects, we can define methods that are only used when objects are of certain classes.

https://rstudio-pubs-static.s3.amazonaws.com/150296_904158e070594471864e89c10c0d14f9.html

Classes versus Objects

Basic OOP Concepts

WTH

There are three object systems in R:

S3

Lightweight, good enough, all purpose!

Use S3 if:

S4

Stricter than S3; enforces data types in slots and methods

Use S4 if:

R6

A lot like Java/Python Objects.

Use R6 if:

Functionality of R’s Object Systems

Portions of this tutorial are derived from: