S4 Class
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.
Creating S4 class and object
setClass() command is used to create S4 class. Following is the syntax for setclass command which denotes myclass with slots containing name and rollno.
Syntax:
setClass(“myclass”, slots=list(name=”character”, Roll_No=”numeric”))
The new() function is used to create an object of the S4 class. In this function, we will pass the class name as well as the value for the slots.
Example:
R
# Function setClass() command used # to create S4 class containing list of slots. setClass ( "Student" , slots= list (name= "character" , Roll_No= "numeric" )) # 'new' keyword used to create object of # class 'Student' a <- new ( "Student" , name= "Adam" , Roll_No=20) # Calling object a |
Output:
Slot "name": [1] "Adam" Slot "Roll_No": [1] 20
Create S4 objects from the generator function
setClass() returns a generator function that helps in creating objects and it acts as a constructor.
Example:
R
stud <- setClass ( "Student" , slots= list (name= "character" , Roll_No= "numeric" )) # Calling object stud |
Output:
class generator function for class “Student” from package ‘.GlobalEnv’ function (...) new("Student", ...)
Now the above-created stud function will act as the constructor for the Student class. It will behave as the new() function.
Example:
R
stud (name= "Adam" , Roll_No=15) |
Output:
An object of class "Student" Slot "name": [1] "Adam" Slot "Roll_No": [1] 15
Inheritance in S4 class
S4 class in R programming have proper definition and derived classes will be able to inherit both attributes and methods from its base class. For this, we will first create a base class with appropriate slots and will create a generic function for that class. Then we will create a derived class that will inherit using the contains parameter. The derived class will inherit the members as well as functions from the base class.
Example:
R
# Define S4 class setClass ( "student" , slots= list (name= "character" , age= "numeric" , rno= "numeric" ) ) # Defining a function to display object details setMethod ( "show" , "student" , function (obj){ cat (obj@name, "\n" ) cat (obj@age, "\n" ) cat (obj@rno, "\n" ) } ) # Inherit from student setClass ( "InternationalStudent" , slots= list (country= "character" ), contains= "student" ) # Rest of the attributes will be inherited from student s <- new ( "InternationalStudent" , name= "Adam" , age=22, rno=15, country= "India" ) show (s) |
Output:
Adam 22 15
The reasons for defining both S3 and S4 classes are as follows:
- S4 class alone will not be seen if the S3 generic function is called directly. This will be the case, for example, if some function calls unique() from a package that does not make that function an S4 generic.
- However, primitive functions and operators are exceptions: The internal C code will look for S4 methods if and only if the object is an S4 object. S4 method dispatch would be used to dispatch any binary operator calls where either of the operands was an S4 object.
- S3 class alone will not be called if there is any eligible non-default S4 method.
So if a package defined an S3 method for unique for an S4 class but another package defined an S4 method for a superclass of that class, the superclass method would be chosen, probably not what was intended.
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.
Contact Us