R Classes and Objects

In this article, you will be introduced to all three classes (S3, S4 and reference class) in R programming with the help of examples.

We can do object oriented programming in R. In fact, everything in R is an object.

An object is a data structure having some attributes and methods which act on its attributes.

Class is a blueprint for the object. We can think of class like a sketch (prototype) of a house. It contains all the details about the floors, doors, windows etc. Based on these descriptions we build the house.

House is the object. As many houses can be made from a description, we can create many objects from a class. An object is also called an instance of a class and the process of creating this object is called instantiation.

While most programming languages have a single class system, R has three class systems. Namely, S3, S4 and more recently Reference class systems.

They have their own features and peculiarities and choosing one over the other is a matter of preference. Below, we give a brief introduction to them.


S3 Class

S3 class is somewhat primitive in nature. It lacks a formal definition and objects of this class can be created simply by adding a class attribute to it.

This simplicity accounts for the fact that it is widely used in the R programming language. In fact most of the R built-in classes are of this type.

See R programming S3 Class section for further details.


Example 1: S3 Class

s <- list(name = "John", age = 21, GPA = 3.5)
class(s) <- "student"

# print the list with the updated class
print(s)

Output

$name
[1] "John"

$age
[1] 21

$GPA
[1] 3.5

attr(,"class")
[1] "student"

Here, the class attribute allows objects to be associated with a specific class. The class attribute provides a way to define behavior and methods for objects of that class.

By assigning the class attribute to the list s, we are essentially indicating that s is an object of the "student" class within the S3 object-oriented system.


S4 Class

S4 class is an improvement over the S3 class. They have a formally defined structure which helps in making objects of the same class look more or less similar.

Class components are properly defined using the setClass() function and objects are created using the new() function.

See R programming S4 Class section for further details.


Example 2: S4 class

setClass("student", slots = list(name = "character", age = "numeric", GPA = "numeric"))

# create an instance of the "student" class
s <- new("student", name = "John", age = 21, GPA = 3.5)

# print the instance
print(s)

Output

An object of class "student"
Slot "name":
[1] "John"

Slot "age":
[1] 21

Slot "GPA":
[1] 3.5

Here, the setClass() function is used to define an S4 class called "student" with three slots: name of type "character", age of type "numeric", and GPA of type "numeric".

After defining the class, we have created an instance of the "student" class using the new() function.

The new() function takes the class name and initializes the object by providing values for each slot.

Finally, the instance s is printed, displaying the object's class and the values stored in each slot.


Reference Class

Reference classes were introduced later, compared to the other two. It is more similar to the object oriented programming we are used to seeing in other major programming languages.

Reference classes are basically S4 classed with an environment added to it.

See R programming Reference Class section for further details.


Example 3: Reference class

# define the reference class "student"
student <- setRefClass("student",
  fields = list(
    name = "character",
    age = "numeric",
    GPA = "numeric"
  )
)

# create an instance of the "student" reference class
s <- student$new(
  name = "John",
  age = 21,
  GPA = 3.5
)

# print the instance
print(s)

Output

Reference class object of class "student"
Field "name":
[1] "John"
Field "age":
[1] 21
Field "GPA":
[1] 3.5

Here, we have defined the reference class "student" using the setRefClass() function, specifying the class name and the fields within the class.

The fields argument is a list that defines the names and types of the fields in the class.

After defining the class, we have created an instance of the "student" class using the $new() method.

The $new() method takes arguments for each field and initializes the object with the provided values.

Finally, the instance s is printed, displaying the object's class and the values stored in each field.


Comparison between S3 vs S4 vs Reference Class

S3 Class S4 Class Reference Class
Lacks formal definition Class defined using setClass() Class defined using setRefClass()
Objects are created by setting the class attribute Objects are created using new() Objects are created using generator functions
Attributes are accessed using $ Attributes are accessed using @ Attributes are accessed using $
Methods belong to generic function Methods belong to generic function Methods belong to the class
Follows copy-on-modify semantics Follows copy-on-modify semantics Does not follow copy-on-modify semantics
Did you find this article helpful?