S3 Class

S3 class does not have a predefined definition and is able to dispatch. In this class, the generic function makes a call to the method. Easy implementation of S3 is possible because it differs from the traditional programming language Java, C++, and C# which implements Object Oriented message passing.

Creating S3 Class

To create an object of 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: 

variable_name <- list(attribute1, attribute2, attribute3….attributeN)

Example: 

In the following code, a Student class is defined. An appropriate class name is given having attributes student’s name and roll number. Then the object of the student class is created and invoked. 

R




# List creation with its attributes name
# and roll no.
a <- list(name="Adam", Roll_No=15)
 
# Defining a class "Student"
class(a) <- "Student"
 
# Creation of object
a


Output:

$name
[1] "Adam"

$Roll_No
[1] 15

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

Generic Functions

The generic functions are a good example of polymorphism. To understand the concept of generic functions consider the print() function. The print() function is a collection of various print functions that are created for different data types and data structures in the R Programming Language. It calls the appropriate function depending on the type of object passed as an argument. We can see the various implementation of print functions using the methods() function.

Example: Seeing different types of the print function

R




methods(print)


Output:

Print methods

Now let’s create a generic function of our own. We will create the print function for our class that will print all the members in our specified format. But before creating a print function let’s create what the print function does to our class.

 Example:

R




# List creation with its attributes name
# and roll no.
a = list(name="Adam", Roll_No=15)
 
# Defining a class "Student"
class(a) = "Student"
 
# Creation of object
print(a)


Output:

$name
[1] "Adam"

$Roll_No
[1] 15

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

Now let’s print all the members in our specified format. Consider the below example –

Example:

R




print.Student <- function(obj){
  cat("name: " ,obj$name, "\n")
  cat("Roll No: ", obj$Roll_No, "\n")
}
 
print(a)


Output:

name:  Adam 
Roll No:  15 

Attributes

Attributes of an object do not affect the value of an object, but they are a piece of extra information that is used to handle the objects. The function attributes() can be used to view the attributes of an object.

Examples: An S3 object is created and its attributes are displayed.

R




attributes(a)


Output:

$names
'name''Roll_No'
$class
'Student'

Also, you can add attributes to an object by using attr. 

R




attr(a, "age")<-c(18)
attributes(a)


Output:

$names
'name''Roll_No'
$class
'Student'
$age
18

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.

S3 class in R programming language has no formal and fixed definition. In an S3 object, a list with its class attribute is set to a class name. S3 class objects inherit only methods from their base class.

Example: 

In the following code, inheritance is done using S3 class, firstly the object is created of the class student.

R




# student function with argument
# name(n) and roll_no(r)
student <- function(n, r) {
    value <- list(name=n, Roll=r)
    attr(value, "class") < - "student"
    value
}


Then, the method is defined to print the details of the student. 

R




# 'print.student' method created
print.student <- function(obj) {
 
    # 'cat' function is used to concatenate
    # strings
    cat("Name:", obj$name, "\n")
    cat("Roll", obj$roll, "\n")}


Now, inheritance is done while creating another class by doing class(obj) <- c(child, parent).

R




s <- list(name="Kesha", Roll=21, country="India")
 
# child class 'Student' inherits
# parent class 'student'
class(s) <- c("Student", "student")
s


Output: 

Name: Kesha 
Roll: 21 

The following code overwrites the method for class students.

R




# 'Student' class object is passed
# in the function of class 'student'
 
print.student <- function(obj) {
    cat(obj$name, "is from", obj$country, "\n")
}
s


Output:

Kesha is from India

R – Object Oriented Programming

In this article, we will discuss Object-Oriented Programming (OOPs) in R Programming Language. We will discuss the S3 and S4 classes, the inheritance in these classes, and the methods provided by these classes.

Similar Reads

OOPs in R Programming Language:

In R programming, OOPs in R  provide classes and objects as its key tools to reduce and manage the complexity of the program. R is a functional language that uses concepts of OOPs. We can think of a class as a sketch of a car. It contains all the details about the model_name, model_no, engine, etc. Based on these descriptions we select a car. The car is the object. Each car object has its own characteristics and features. An object is also called an instance of a class and the process of creating this object is called instantiation. In R S3 and S4 classes are the two most important classes for object-oriented programming. But before going discussing these classes let’s see a brief about classes and objects....

Class and Object

Class is the blueprint or a prototype from which objects are made by encapsulating data members and functions. An object is a data structure that contains some methods that act upon its attributes....

S3 Class

S3 class does not have a predefined definition and is able to dispatch. In this class, the generic function makes a call to the method. Easy implementation of S3 is possible because it differs from the traditional programming language Java, C++, and C# which implements Object Oriented message passing....

S4 Class

...

Contact Us