R programming - Object Oriented Programming

R programming - Object Oriented Programming

A program for statistical analysis - Part 8

Hitendra K's photo
Hitendra K
·Aug 4, 2022·

4 min read

Subscribe to our newsletter and never miss any upcoming articles

Play this article

Object-Oriented Programming (OOP) is the most popular programming language. With the help of oops concepts, we can construct the modular pieces of code used to build blocks for large systems. Oops is a great tool to manage the complexity of more extensive programs in the R language. In Object- Oriented Programming, S3 and S4 are the two important systems.

We will discuss the S3 and S4 classes, inheritance in these classes, and the methods provided by these classes.

S3

In oops, the S3 is used to overload any function. So that we can call the functions with different names and it depends on the type of input parameter or the number of parameters.

S4

S4 is the most important characteristic of oops. However, this is a limitation, as it is quite difficult to debug. There is an optional reference class for S4.

Class and Object

In R, everything is an object. Therefore, programmers perform the OOPS concept when they write code in R. An object is a data structure that has some methods that can act upon its attributes. An object is a data structure that contains some methods that act upon its attributes.

In R, classes are the outline or design for the object. Classes encapsulate the data members, along with the functions. In R, there are two most important classes, i.e., S3 and S4, which play an important role in performing OOPs concepts.

1. S3 Class : With S3 class, we can take advantage of the ability to implement the generic function. Furthermore, using only the first argument, S3 is capable of dispatching. S3 differs from traditional programming languages such as Java, C ++, and C #, which implement OO passing messages. This makes S3 easy to implement. In the S3 class, the generic function calls the method. S3 is very casual and has no formal definition of classes.

To create an object for this class we will create a list that will contain all the class members. Then this list is passed to the class() method as an argument.

Syntax:

var_name <- list(member1, member2, member3.........memberN)

Example:

var < - list(name="Hari", Reg_No=20)

// Defining Class

class(var) < - "Student" 

// Object creation

var 

Output
//$name
//[1] "Hari"

//$Reg_No
//[1] 20

//attr(, "class")
//[1] "Student"

Inheritance in S3 Class Inheritance is an important concept in OOP(object-oriented programming) which allows one class to derive the features and functionalities of another class. This feature facilitates code-reusability. Inheritance means extracting the features of one class into another class. In the S3 class of R, inheritance is achieved by applying the class attribute in a vector. S3 class objects inherit only methods from their base class.

Example:

student < - function(k, l) {
    value < - list(name=k, Roll=l)
    attr(value, "class") < - "student"
    value
}

print.student < - function(obj) {


    cat("Name:", obj$name, "\n")
    cat("Roll", obj$roll, "\n")}

s < - list(name="Hari", Roll=21, country="India")


class(s) < - c("Student", "student")
s

Output
//Name: Hari
//Roll: 21

2. S4 Class The S4 class is similar to the S3 but more formal than the latter. It differs from S3 in two different ways. S4 class has a predefined definition. It contains functions for defining methods and generics. It makes multiple dispatches easy. This class contains auxiliary functions for defining methods and generics.

In R, we use setClass() command for creating S4 class. In S4 class, we will specify a function for verifying the data consistency and also specify the default value. In R, member variables are called slots.

Syntax setClass(“myclass”, slots=list(name=”character”, Roll_No=”numeric”))

Example

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

a <- new("Student", name="Hari", Roll_No=20)

a

Output
//Slot "name":
// "Hari"

//Slot "Roll_No":
//20

Inheritance in S4 Class

Like S3 class, we can perform inheritance in the S4 class also. The derived class will inherit both attributes and methods of the parent class. Let's start understanding how we can perform inheritance in the S4 class. There are the following ways to perform inheritance in the S4 class:

Example:

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


setMethod("show", "student",
          function(obj){
              cat(obj@name, "\n")
              cat(obj@age, "\n")
              cat(obj@rno, "\n")
          }
          )


setClass("InternationalStudent",
         slots=list(country="character"),
         contains="student"
         )


s < - new("InternationalStudent", name="Hari",
          age=22, rno=15, country="India")
show(s)

Output
//Hari
//22 
//15

That is it for today, we can look into libraries in R in the next blog. Subscribe to our newsletter for more interesting blogs.

 
Share this