Object Oriented Programming in R [OOPs in R]

R is a very popular language used for data analysis and machine learning. As such it makes use of the concepts of Object-Oriented Programming to perform different operations on the data we have.

 

Classes and Objects

Classes are blueprints or prototypes which are responsible for providing different properties to an object of that class and providing different methods to perform operations on those properties. An object of a class is used to access and perform different operations on the data members of the class.

There are two main types of classes in R:

  1. S3 CLASSES – S3 classes are the easier of the two. They do not have a pre-defined method definition and are very flexible. In order to create a S3 class, a list of attributes is created and assigned to a variable and is then given to the “class ()” function as an argument to create a S3 class. A method for a S3 class is defined using the “useMethod” command. A function name has to be reserved by creating a generic function. 
  2. S4 CLASSES – There is a big difference between S3 and S4 classes. S4 classes have predefined definitions. S4 classes have a more rigid definition and they are less flexible and less forgiving than the S3 classes. They already have a lot of pre-defined functions for a number of operations. The object of a class is created by using the command called “setClass”. This command takes a number of arguments – name of the class; ‘slots’ is used to define the data members of the class; ‘prototype’ is used to assign default values to the ‘slots’ data members. As we discussed earlier, S4 classes have its predefined methods. Therefore, in order to create our very own method for the class, we use the “setGeneric” command to make our function generic and then use “setMethod” command to create a definition for the function. 

 

INHERITANCE

Inheritance is an OOPs concept in which one class called Child Class can inherit the properties and methods of another class called Parent Class. Both S3 and S4 classes have different ways for Inheritance. 

In S3 classes, parent class is created and then another class is created and then inherited from the first class by using “class ()” function and assigning it to a vector of names of parent and child classes.

In S4 classes, parent class is created and then another class is created using “setClass” command. The “setClass” command for the child class has an additional argument class ‘contains’ whose value is equal to the name of the class which is to be inherited.

 

ENCAPSULATION

Encapsulation is a concept in OOPs in which all data members and member functions which operate on those data members are bundled or encapsulated into a single unit. This unit is referred to as a class. Since there are already S3 and S4 classes available in R, R is also capable of encapsulation.

 

POLYMORPHISM

Polymorphism is the ability of an object to be used in multiple forms. A method of an object can have multiple forms in R. This is called parametric polymorphism. This involves creating two or more functions with the same name but with different parameters.

 

ABSTRACTION

Abstraction is yet to be implemented in R. Abstraction in R is currently in experimental stage and there is only a single function called “abstract ()” available.

 

Below is a program in R to demonstrate the concepts of OOPs in a single program.

Write a R program to demonstrate the use of OOP concepts in R

#Write a R program to demonstrate the use of OOP concepts in R
Data <- setClass("Data",slots=c(name="character",
                                enroll="character",mobile="numeric"),
                 prototype=list(name="",enroll="",mobile=0))

setGeneric(name="setSlotValue",
           def=function(obj,value1,value2){
             standardGeneric("setSlotValue")
           })

setMethod(f="setSlotValue",
          signature=c("Data","character","character"),
          definition=function(obj,value1,value2){
            [email protected] <- value1
            [email protected] <- value2
            validObject(obj)
            return(obj)
          })

setMethod(f="setSlotValue",
          signature=c("Data","numeric","numeric"),
          definition=function(obj,value1,value2=1){
            [email protected] <- value1
            validObject(obj)
            return(obj)
          })

s1 <- Data()
print("Before Polymorphism...")
print(s1)

#POLYMORPHISM
s1 <- setSlotValue(s1,"XXXXX","A760521XXXX")
s1 <- setSlotValue(s1,000000000,1)

#INHERITANCE
print("After Polymorphism and Before Inheritance...")
print(s1)

New_Data <- setClass("New_Data",contains="Data")

setGeneric(name="newValues",
           def=function(obj){
             standardGeneric("newValues")
           })

setMethod(f="newValues",
          signature="Data",
          definition=function(obj){
            print("Inside Data Class...")
            obj <- setSlotValue(obj,"Exploring Bits","A7605218066")
            validObject(obj)
            return(obj)
          })

setMethod(f="newValues",
          signature="New_Data",
          definition=function(obj){
            print("Going to Data Class...")
            obj <- callNextMethod(obj)
            print("Inside New_Data Class...")
            cat("\n")
            obj <- setSlotValue(obj,123456789,1)
            validObject(obj)
            return(obj)
          })

s2 <- New_Data()
s2 <- newValues(s2)
print("After Inheritance...")
print(s2)

x1 <- list(1,2,3,4,5)
x2 <- list(6,7,8)
x3 <- c(x1,x2)
print(x3)

 

 

Leave a Comment