Object Oriented Programming In R language

This tutorial will share object-oriented programming in the R programming language together. R also offers object-oriented programming capabilities such as classes, objects, inheritance, overloading, and others, in addition to function-oriented programming.

What is object-oriented programming in R?

Programming languages that use object-oriented techniques are shared. Using its notions, we can create modular bits of code that serve as building blocks for massive systems. The language R is practical. Additionally, there is assistance for writing OOP-style programs. Object Oriented Programming is an excellent tool for managing complexity in more sophisticated applications. For GUI development, it is especially suitable.

What types of OOP are in systems?

OOP systems in R come in six main types. But in object-oriented programming, the two most essential systems are S3 and S4:

  • S3 is a simple method to invoke a distinct function name depending on the kind or quantity of input parameters.
  • S4 is a completely functional OOP system, but it is cumbersome and difficult to debug. It is only applied to legacy code.

Class and Object in R?

R supports OOP programming for programmers. In R, everything is an object.

  • Data structures are objects. It has specific techniques that can influence its qualities.
  • Classes serve as the object’s framework or design. It includes both the functions and the data members.

Classes in R?

S3 Class in R

The Syntax

varName <- list(att1, att2, ... )


  • att1, att2: The attributes which you want to add
  • varName: The variable name

The example

Here, we will create a student Student class. It is decided on a class name that includes the student’s name and Math number. The student class object is then created and used.

# Create a list
teacher <- list(name = "Jean C. Bolin",
          Age = 30)

# Define a class "Student"
class(teacher) <- "Teacher"

# Create an object


[1] "Jean C. Bolin"

[1] 30

[1] "Teacher"

Now we will print out student information through the class as follows:

# Create a list
teacher <- list(name = "Jean C. Bolin",
                age = 15)

# Define a class "Teacher"
class(teacher) <- "Teacher"

# Create function to print student
print.teacher <- function(teacher){
  cat('name teacher:',teacher$name, '\n')
  cat('age:',teacher$age, '\n')

# print student information


name teacher: Jean C. Bolin 
age: 15

S4 Class in R

The Syntax

setClass("myclass", slots=list(name="character", Roll_No="numeric"))  


  • myclass: The name class is used

The Example

Here, we will make an S4 class object. The class name and the slot values will be passed to this function.

# Create a S4 class
setClass("Teacher", slots = list(name = "character",
                              Roll_No = "numeric"))

# The class 'Teacher'
teacher <- new("Teacher", name = "Jean C. Bolin",
        Roll_No = 20)

# Calling object


An object of class "Teacher"
Slot "name":
[1] "Jean C. Bolin"

Slot "Roll_No":
[1] 20

In addition, we can make an object using the generator function.

teacher<- setClass("teacher", slots =
                      list(name = "character",
                      Roll_No = "numeric"))

# Calling object


class generator function for class “teacher” from package ‘.GlobalEnv’
function (...) 
new("teacher", ...)


This article demonstrates object-oriented programming in the R programming language. If you have any questions, please leave a comment below.

Have a great day!

Posted in R

Leave a Reply

Your email address will not be published. Required fields are marked *