Object Oriented Programming


May 9, 2022, Learn eTutorial
1246

In this tutorial you are going to explore the OOPS or object-oriented concepts in R.We will begin with understanding the functional programming concept in R to know a clear idea to differentiate the object-oriented style from the functional programming style. Everything in R is an object with different attributes and behavior. Classes and objects are the primary tools related to the OOPs concept that manage and maintain the complexity of the R program. In order to understand the program execution, the R program is divided into two classes: S3 classes & S4 classes. So in this tutorial, you will learn about objects, classes, s3 classes, and s4 classes which make OOPs a better or strong data analysis tool.

What is functional programming in R?

A functional programming style is mostly used with the R language. That is a program is initiated with some data manipulation by applying some function (f(x)) over the data which returns modified data or new data as a result of the function applied over the input data. Again applying some other function like (g(x)) to modify data and so on repeats till the required output is generated.

What is functional programming in R?

The image represents objects(argument x) passed to the function f() to return a value after the function execution.
Generally, the programming begins with a functional mindset, suppose you need to carry out some calculations. You will create a function for it with a set of instructions to execute. Together with that, you need to consider the objects to be passed to the functions that is the arguments inside function parentheses like function(a,b) where a,b forms the arguments.
 


Some calculations <-function(a,b){
//do something
return(result)
}

What is object-oriented programming in R?

OOP or object-oriented programming is a  very important programming style or concept associated with classes and objects. Along with these main concepts, there are some more core concepts such as inheritance, polymorphism, abstraction, encapsulation, etc. The OOPs concept is used in languages like C++, JAVA, PYTHON, etc.

In object-oriented programming, the entire program is subdivided into various classes for the simplicity of understanding the execution of the whole program. Consider a simple example for understanding the OOPs concept. Consider human beings are categorized into MALE and FEMALE.

Object-oriented programming is a technique for building a clear programming structure to manage, maintain, debug and reuse the code. In object-oriented programming (OOP) concepts the functions are known as methods. In R methods are just functions used in an object-oriented context. R language supports many available object types such as numeric, logical, character, vectors, list, factors, and data frames. We can summarize that there are about 20 or more object types available in R.  These are all the basic building blocks needed for data analysis in R.

For object-oriented programming, two variable types are most important: lists and environments. These two variable types (list and environment) are important because these variable types can contain other variables and are useful in creating many more other complex types.

Object-oriented programming is not necessary or even desirable for data analysis. Most of the time data is stored in a data frame and uses functions to manipulate the data frame and finally returns another data frame.

Note: OOP is used with a limited number of complex objects.

Difference between functional and Object-Oriented programming

key Functional Programming Object-Oriented Programming
Definition Functional programming is a programming technique that focuses on evaluating functions and developing the structure as well as elements of computer programs. Object-oriented programming is a programming paradigm that uses abstraction to create models based on the real world.
Data  Immutable data is used. Mutable data is used.
Model The declarative programming model is followed. The imperative programming model is followed.
Support Functional programming supports parallel programming Does not support functional programming.
Execution Executed in any order Executed in a particular order
Iteration Recursion is used Loops are used
Primary units Functions and variables Object and method

What are objects and classes in R?

An object is a data structure with some attributes and methods which act upon its attributes’ is an object-oriented language. Everything in R is considered an object. Each object has one or more attributes. Most of the R objects have an attribute which is the “class” of the object. R provides a bundle of classes for statistical programming. Everything in R is an object list, vector, data frame, factor, array, etc. An object is a collection of data members associated with functions. Objects are the Basic Building of a program. An object is also called an Instance of class. The process of creating an object from a class is called instantiation. Objects have their attributes like class, dimnames,names,dim,comments etc. An object has a real Existence.

Classes in R

A class is a description of a thing and an object is an instance of a class. A class describes an object type by mentioning the properties, behavior, and relationship of the object with other objects. The term methods can be described as functions in association with the specified object types. 

A class is a blueprint for the object. For example consider a sketch of a house with all details regarding its floor, windows, doors, bedrooms, kitchen, etc based on the model in which the house is constructed.

Let us understand a simple example to differentiate an object and a class. Consider a class  Person with attributes and functionalities.

What are objects and classes in R?

The different values of the attributes such as Name, Age, Profession, and Place create different objects like Irina, John, and Adam but they belong to the same class Person. The common behavior of a person like walk(),eat(),,sleep() forms methods to implement or the actions of the objects. So we can say the class is a blueprint that is responsible for providing Different Properties to objects and providing different methods to perform operations on those properties.

There are three separate systems of object orientation in R.The three systems are the following:-

  1. S3 class
  2. S4 class
  3. Reference class

Each system is used independently of the other. Developers of new projects are encouraged to use s4 style classes. They are extensively used in Bioconductor projects. Many developers use S3 classes because they are quick and easy.

What is functional programming in R?

1. S3 Classes in R

S3 carries out a style of object-oriented programming known as generic function OO. This is different from most programming dialects, similar to Java, C++, and C#, which execute message-passing OO.S3 is primitive in nature.The objects of the S3 class can be created simply by adding a class attribute to it.S3 classes support the overloading functions by dividing them into methods.

How to define and create an S3 object?

In S3 objects you can simply create a list.Consider the below example where a list is created with list elements such as name, class, location, etc.

How to define and create an S3 object?


#Object Orientation in R

#How to define S3 class and create S3 objects ?

#Create a list with needed elements
 s3 = list(name = "Stephen", Standard = "x", age = 16, locati Canada")

print(s3)
 

Output:


$name
[1] "Stephen"

$Standard
[1] "x"

A list s3 is created with name as “Stephen”, Standard as "x",age as 16 and location as " Canada”. These are the attributes of the list s3 .The screenshot below helps you to understand about the attributes better, which shows one user data object

S3 class in R

Once after creating the S3 list ie the list created with attributes name, standard, age, location you can convert the list using a class function into an s3 object.

How to define and create an S3 object?


#Creates an object of class "student"
class(s3) = "student"

print(class(s3))
 

Output:


[1] "student"

2. S4 Classes

S4 classes are an improvement over S3 classes.S4 classes have a formally defined structure which helps in making objects of the same class which look more or less similar.S4 class is more standardized and structured and is easier to manipulate.

How to create and manipulate an S4 object?

An s4 class is defined using the setClass() function. In the below example the code defines s4 class with class name “student” and slots are defined as a list where attributes name, standard, location are character, and age is numeric.


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

After creating or defining a S4 class a specific instance of object is created using new().In the below code s4 is an object created using new() where class name is student with slots like name,standard,age,location filled with their corresponding values.


s  = new("student",name="Sen",standard="XI",age=17,locati)
 

The whole program when executed returns the below output that shows an object created of class student and slots with their values like “name” is “Sen “ , Slot "standard" is “XI”etc


#Object Orientation in R

#How to define S4 class and create S4 objects ?

    #Create a list with needed elements
 setClass("student", slots = list(name = "character", standard = "character", age = "numeric", locati))
 s4 = new("student", name = "Sen", standard = "XI", age = 17, locati)
s4
 

Output:


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

Slot "standard":
[1] "XI"

Slot "age":
[1] 17

Slot "location":
[1] "UK"

How to check an object is s4?

To check an object is s4 you can use iss4() function.The iss4() functions returns a boolean value either TRUE if the object is s4 else returns a FALSE which indicates the objects created is not s4 class.Let us check the object s4 created in our previous section ia an s4 an object of class student.


IsS4(s4)
 

The code once executed returns a TRUE as result because it belongs to s4 class.

What happens when we check it is a s3 class object?


isS4(s3)
[1] FALSE
 

How to access and modify S4 slot?

The @ symbol can be used to access the components or contents within s4 .The object type s4 followed by @ symbol and name of the component to be accessed.In below example “name” is one of the attribute created based on our previous example.


s4@name
 

This returns the name “Sen” as output.

The @ symbol is also useful to set or reset or assign a value to the object.Let us reset the name “Sen” to “Peter” using the assigning symbol(<-/=)


s4@name <- "Peter"
 

An object of class student with new name instead of “Sen” replaced with “Peter” is created as shown below.


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

Slot "standard":
[1] "XI"

Slot "age":
[1] 17

Slot "location":
[1] "UK"

How do modify slots are S4 objects?

The slot() function allows modifying the slots. The slot() function consists of the name of the object and attributes as parameters inside the parentheses.


slot(s4,"name")
 

The code returns “Peter ” as output.You can also modify the value of object using slot() function.


slot(s4,"name")="john"
 

Output:


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

Slot "standard":
[1] "XI"

Slot "age":
[1] 17

Reference class in OOPs

The reference class definition is similar to defining an s4 class. In the s4 class setClass() is used for defining while reference class is defined using setRefClass() The variable members of a class need to be included in the class definition. In the Reference class, the member variables are called as fields like slots in the s4 class.

Defining a Reference Class

The below example defines the Reference class student with fields


setRefClass "student",fields=list(name="character",standard="character",age="numeric",locati
 

Creating a Reference object

 A generator function is returned by setRefclass() which is used to create new objects. The definition is stored to a variable Student. The Student variable which has the definition forms the generator function for the class student.


Student = setRefClass("student",fields=list(name="character",standard="character",age="numeric",locati

print(Student)
 

forms a generator function for class student. It provides details such as class fields,class Methods.


Generator for class "student":

Class fields:
                                              
Name:       name  standard       age  location
Class: character character   numeric character

Class Methods: 
     "field", "trace", "getRefClass", "initFields", "copy", "callSuper", ".objectPackage", "export", "untrace", 
     "getClass", "show", "usingMethods", ".objectParent", "import"

Reference Superclasses: 
     "envRefClass"

 

The generator function is student() which creates new object with values to the fields.


# now student() is our generator
function which can be used to create new objects
 s = student(name = "john", standard = "X", age = 15, locati
print(s)
 

The code after execution gives the below output


Output:-
Reference class object of class "student"
Field "name":
[1] "John"

Field " standard ":
[1] "X"
Field " age ":
[1] 15
Field " location ":
[1] "UK"

How to access and modify fields?

Fields of the object can be accessed using the $ operator.


> s$name
[1] "John"
> s$age
[1] 15
> s$ location 
[1] "UK"

 

Similarly, it is modified by reassignment.


> s$name <- "Paul"
> s

 

The output after reassigning is


Reference class object of class "student"
Field "name":
[1] "Paul"
Field " standard ":
[1] "X"
Field " age ":
[1] 15
Field " location ":
[1] "UK"

S3 class S4 class Reference class
Lacks format 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 a generator function that stores class defined.
Attributes are accessed using $ Attributes are accessed using @ Attributes are accessed using $
Methods belong to generic functions. Methods belong to generic functions. Methods belong to generic functions.
Follows copy-on-modify semantics Follows copy-on-modify semantics Follows copy-on-modify semantics

When should use object-oriented programming in R?

OOPS (object-oriented programming) works well with a limited number of objects where the complete behavior of objects can be analyzed. Mostly applicable with industry-based specific data analysis. Consider an example there are thousands of biological analysis packages available with Bioconductor projects particularly to analyze the genomic sections of biological data. The Bioconductor packages make use of genomic range objects. The reuse of these objects throughout Bioconductor the behavior is predictable across many packages.

Another example is accessing data through web application programming interfaces (APIs). When considering this example there are only a limited number of responses that can be provided by the website. The objects can be defined to store these responses.

Consider a third example a GUI or Graphical User Interface. The languages used in building GUI such as JAVA, and C sharp are object-oriented. There are only a few objects that need to be considered such as buttons, checkboxes,text boxes, etc.

From the above-discussed examples, it is very clear none of the examples involves any data analysis. Object-oriented programming is brilliant for building tools for data analysis. We can summarise this as with functional programming think of functions first then how to use them in data whereas with object-oriented programming you need to think about data structure first and then their functionality.