Open In App

Classes in R Programming

Last Updated : 12 Jun, 2025
Comments
Improve
Suggest changes
Like Article
Like
Report

Classes and Objects are core concepts in Object-Oriented Programming (OOP), modeled after real-world entities. In R, everything is treated as an object. An object is a data structure with defined attributes and methods. A class is a blueprint that defines a set of properties and methods shared by all objects of that type.

R has a unique three-class system: S3, S4, and Reference Classes. Each of these class systems has distinct characteristics and is used to define and manage objects and their methods effectively.

1. S3 Class

S3 is the most widely used OOP system in R, but it lacks a formal definition and structure. An object of this type can be created simply by adding an attribute to it.

Example:

R
movieList <- list(name = "Iron man", leadActor = "Robert Downey Jr")

class(movieList) <- "movie"

movieList

Output:

$name
[1] "Iron man"

$leadActor
[1] "Robert Downey Jr"

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

In the S3 system, methods are not tied to a specific class; they are associated with generic functions. This means that, unlike in languages like C++ or Java, we cannot create methods that belong directly to a class. However, we can define the behavior of a generic function (such as print) when it is applied to objects of our class.

R
print(movieList)

Output:

$name
[1] "Iron man"
$leadActor
[1] "Robert Downey Jr"

Example: Creating a user-defined print function

R
print.movie <- function(obj){
    cat("The name of the movie is", obj$name,".\n")
    cat(obj$leadActor, "is the lead actor.\n")
}

print.movie(movieList)

Output:

The name of the movie is Iron man .
Robert Downey Jr is the lead actor.

2. S4 Class

Programmers familiar with languages like C++ or Java may find S3 significantly different from their typical concept of classes, as it lacks the structure usually associated with classes. S4 improves upon S3 by providing a more formal definition for objects and offering a clear structure for managing them.

Example:

As shown in the example, setClass() is used to define a class and new() is used to create the objects.

R
library(methods)

setClass("movies", slots=list(name="character", leadActor = "character"))

movieList <- new("movies", name="Iron man", leadActor = "Robert Downey Jr")

movieList

Output:

An object of class "movies"
Slot "name":
[1] "Iron man"
Slot "leadActor":
[1] "Robert Downey Jr"

R
print(movieList)

Output:

An object of class "movies"
Slot "name":
[1] "Iron man"

Slot "leadActor":
[1] "Robert Downey Jr"

Example: Creating a user-defined print function

R
setMethod("show", "movies",function(object){
    cat("The name of the movie is ", object@name, ".\n")
    cat(object@leadActor, "is the lead actor.\n")
})

show(movieList)

Output:

[1] "show"
The name of the movie is Iron man .
Robert Downey Jr is the lead actor.

3. Reference Class

Reference Classes are an improvement over S4 Classes. In this system, methods are associated with the classes, making them more similar to object-oriented classes in other languages. Defining a Reference Class is similar to defining an S4 class. Instead of setClass(), we use setRefClass(), and instead of "slots," we use "fields."

Example:

R
library(methods)

moviess <- setRefClass("moviess",
                      fields = list(
                        name = "character",
                        leadActor = "character",
                        rating = "numeric"
                      ))

movieList <- new("moviess", name = "Iron Man", leadActor = "Robert Downey Jr", rating = 7)

movieList

Output:

Reference class object of class "moviess"
Field "name":
[1] "Iron Man"
Field "leadActor":
[1] "Robert Downey Jr"
Field "rating":
[1] 7

Now let us see how to add some methods to our class with an example.

R
library(methods)

moviess <- setRefClass("moviess", fields = list(name = "character", 
                       leadActor = "character", rating = "numeric"), methods = list(
                       increment_rating = function()
                       {
                           rating <<- rating + 1
                       },
                       decrement_rating = function()
                       {
                           rating <<- rating - 1
                       }
                     ))

movieList <- new("moviess", name = "Iron Man", leadActor = "Robert Downey Jr", rating = 7)

print(movieList$rating)

movieList$increment_rating()
print(movieList$rating)

movieList$decrement_rating()
print(movieList$rating)

Output:

[1] 7
[1] 8
[1] 7


Next Article

Similar Reads