Object Oriented Programming
R programming integrates object-oriented programming concepts, providing classes and objects as fundamental tools to simplify and manage program complexity. R, though primarily a functional language, also supports OOP principles. A class can be thought of as a blueprint, like the design of a car. It defines attributes such as model name, model number, engine type, etc. Using this design, we can create objects—specific cars with unique features. An object is an instance of a class, and the process of creating this object is called instantiation.
In R, S3 and S4 are two key systems for implementing object-oriented programming. Let’s delve deeper into these classes.
Classes and Objects
A class is a template or blueprint from which objects are created by encapsulating data and methods. An object is a data structure containing attributes and methods that act upon those attributes.
S3 Class
The S3 class is the simplest and most commonly used object system in R. It has no formal definition, and its methods are dispatched using generic functions. S3 is quite flexible and less restrictive compared to traditional OOP languages like Java or C++.
Creating an S3 Class
To create an S3 class, you start by creating a list containing the attributes. Then, assign a class name to the list using the class() function.
Syntax:
variable_name <- list(attribute1, attribute2, ..., attributeN)
Example:
# Create a list with attributes
student <- list(name = "John", Roll_No = 101)
# Define a class
class(student) <- "Student"
# View the object
student
Output:
$name
[1] "John"
$Roll_No
[1] 101
attr(,"class")
[1] "Student"
Generic Functions
Generic functions exhibit polymorphism, meaning the function behavior depends on the type of object passed. For instance, the print() function adapts its output based on the object type.
Example: Viewing Print Methods
methods(print)
Output:
[1] print.default print.data.frame print.factor
...
Custom Generic Function Example:
print(12345)
# Define a custom print method for the Student class
print.Student <- function(obj) {
cat("Name: ", obj$name, "\n")
cat("Roll Number: ", obj$Roll_No, "\n")
}
# Call the custom print method
print(student)
Output:
Name: John
Roll Number: 101
Attributes in S3 Classes
Attributes provide additional information about an object without altering its value. Use the attributes() function to view an object’s attributes, and attr() to add attributes.
Example:
# View attributes
attributes(student)
Output:
$names
[1] "name" "Roll_No"
$class
[1] "Student"
Inheritance in S3 Class
Inheritance allows one class to derive features and functionalities from another class. In S3, this is done by assigning multiple class names to an object.
Example:
# Create a function to define a Student
createStudent <- function(name, roll_no) {
student <- list(name = name, Roll_No = roll_no)
class(student) <- "Student"
return(student)
}
# Define a new class that inherits from Student
internationalStudent <- list(name = "Emily", Roll_No = 202, country = "USA")
class(internationalStudent) <- c("InternationalStudent", "Student")
# View the object
internationalStudent
Output:
$name
[1] "Emily"
$Roll_No
[1] 202
$country
[1] "USA"
attr(,"class")
[1] "InternationalStudent" "Student"
S4 Class
S4 classes are more structured and formally defined than S3 classes. They include explicit declarations for slots and use accessor functions for better data encapsulation.
Creating an S4 Class
Use the setClass() function to define an S4 class and the new() function to create objects.
Syntax:
setClass("ClassName", slots = list(slot1 = "type", slot2 = "type"))
Example:
# Define a base class
setClass("Person", slots = list(name = "character", age = "numeric"))
# Define a derived class
setClass("InternationalStudent", slots = list(country = "character"), contains = "Person")
# Create an object of the derived class
student <- new("InternationalStudent", name = "Sarah", age = 25, country = "Canada")
# Display the object
show(student)
Output:
An object of class "InternationalStudent"
Slot "name":
[1] "Sarah"
Slot "age":
[1] 25
Slot "country":
[1] "Canada"
Leave a Reply