R Objects and Classes

R is a functional language that uses concepts of objects and classes.

An object is simply a collection of data (variables) and methods (functions). Similarly, a class is a blueprint for that object.

Let's take a real life example,

We can think of the class as 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.


Class System in R

While most programming languages have a single class system, R has three class systems:


S3 Class in R

S3 class is the most popular class in the R programming language. Most of the classes that come predefined in R are of this type.

First we create a list with various components then we create a class using the class() function. For example,

# create a list with required components
student1 <- list(name = "John", age = 21, GPA = 3.5)

# name the class appropriately
class(student1) <- "Student_Info"

# create and call an object
student1

Output

$name
[1] "John"

$age
[1] 21

$GPA
[1] 3.5

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

In the above example, we have created a list named student1 with three components. Notice the creation of class,

class(student1) <- "Student_Info"

Here, Student_Info is the name of the class. And to create an object of this class, we have passed the student1 list inside class().

Finally, we have created an object of the Student_Info class and called the object student1.

To learn more in detail about S3 classes, please visit R S3 class.


S4 Class in R

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.

In R, we use the setClass() function to define a class. For example,

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

Here, we have created a class named Student_Info with three slots (member variables): name, age, and GPA.

Now to create an object, we use the new() function. For example,

student1 <- new("Student_Info", name = "John", age = 21, GPA = 3.5)

Here, inside new(), we have provided the name of the class "Student_Info" and value for all three slots.

We have successfully created the object named student1.


Example: S4 Class in R

# create a class "Student_Info" with three member variables
setClass("Student_Info", slots=list(name="character", age="numeric", GPA="numeric"))

# create an object of class 
student1 <- new("Student_Info", name = "John", age = 21, GPA = 3.5)

# call student1 object 
student1

Output

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

Slot "age":
[1] 21

Slot "GPA":
[1] 3.5

Here, we have created an S4 class named Student_Info using the setClass() function and an object named student1 using the new() function.

To learn more in detail about S4 classes, please visit R S4 class.


Reference Class in R

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.

Defining a reference class is similar to defining a S4 class. Instead of setClass() we use the setRefClass() function. For example,

# create a class "Student_Info" with three member variables
Student_Info <- setRefClass("Student_Info",
  fields = list(name = "character", age = "numeric", GPA = "numeric"))

# Student_Info() is our generator function which can be used to create new objects
student1 <- Student_Info(name = "John", age = 21, GPA = 3.5)

# call student1 object
student1

Output

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

In the above example, we have created a reference class named Student_Info using the setRefClass() function.

And we have used our generator function Student_Info() to create a new object student1.


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?

Our premium learning platform, created with over a decade of experience and thousands of feedbacks.

Learn and improve your coding skills like never before.

Try Programiz PRO
  • Interactive Courses
  • Certificates
  • AI Help
  • 2000+ Challenges