Learn about Object-Oriented Programming(OOP) along with R’s objects, different classes like S3 and S4, along with its construction, creating its generic function with examples and many more.

Object-Oriented Programming(OOP) is a programming paradigm in where different methods are used to design software around data or objects rather than using functions. A method is just a function, talked about in the OOP context. It is object-oriented because all the processing revolves around the objects and fields. Every object has different attributes and behavior.

In Object-Oriented programming, the whole program can be divided into different classes to quickly understand the program execution. R is a functional programming language, and OOP will help manage large system problems. OOP used to manage GUI applications, most likely web-applications. Object-Oriented Programming is good for building tools for data analysis but bad for data analysis itself.

Systems of OOP

There are mainly two major systems of OOP, which are described below:

  • S3 Classes: These let you overload the functions.
  • S4 Classes: These let you limit the data as it is quite difficult to debug the program

Note: These classes will be discussed in detail in the later section.

Objects in R

Objects are the instance of the class. Also, everything in R is an object and to know more look at Data types in R. They also can have their attributes like class, attributes,dimnnames, names, etc.

For example, objects are assigned a value using <-, consider the following piece of code where ‘a1’ and ‘a2’ are the variables where the numeric value is assigned to it.

a1 <- 10
a2 <- 20

An object can be assigned a set of numbers where ‘a3’ is the object containing a vector of different numbers.

a3 <- c(10,20,30)

Here c contains the numbers,10, 20, 30 into a vector.

Let’s take a real-life example like a student, which has attributes of name, age, class, and gender, the teacher, has a name, age, and gender.

st_name <- "andrew"
st_age <- 16
st_class <- 10
st_gender <- "male"

t_name <- "angelina"
t_age <- 26
t_gender <- "female"

student <- c(st_name, st_age, st_class, st_gender)
teacher <- c(t_name, t_age, t_gender)

[1] "andrew" "16"     "10"     "male"  
[1] "angelina" "26"       "female"  

The above code gives the following output:

"andrew" "16" "10" "male"  
"angelina" "26"  "female"

In the above code, you can see the example where there are different types of objects. The ‘student’ and ‘teacher’ object, also called a variable, hold type ‘character’ and ‘double.’

Class in R

Class is the blueprint that helps to create an object and contains its member variable along with the attributes.

As discussed earlier in the previous section, there are two classes of R, S3, and S4.

S3 Class

These classes help in overloading functions by splitting them into generic and methods.

functions = generic + methods

S3 is very different from other programming languages like C#, Java, etc. Because it’s straightforward to implement, it uses only the first argument to dispatch. That’s why it doesn’t require too much knowledge as a programmer’s perspective.

S3 works by defining a strict naming convention of generic dot class.


The arguments of methods should contain arguments of generic, and method should include a …arg.

Constructing along with Example of S3 Class

You will be demonstrated to construct an S3 class in the following example, which shows the creation of the list where components are being passed along with proper class name followed by the result to be printed.

The following example shows ‘studentBio’ contains a list where components are passed as a list where ‘student_name,’ ‘student_age,’ and ‘student_contact’ are being given. The class is named ‘Student Info,’ and ‘studentBio’ is the newly created class whose values are displayed below.

studentBio <- list(studentName = "Harry Potter", studentAge = 19, studentContact="London")
class(studentBio) <- "StudentInfo"
[1] "Harry Potter"

[1] 19

[1] "London"

[1] "StudentInfo"

The above code gives the output as below:

 "Harry Potter"




#r #oop #data-science

Object-Oriented Programming (OOP) in R
2.10 GEEK