The R programming language uses object-oriented programming (OOP) concepts. OOP in R deals mainly with the objects and classes, which are the main tools used to manage the complex nature of the language.
We can think of a class as a computer system and an object as the components that make up the computer system, including a visual display unit, a mouse, and a keyboard.
These components (the objects) make up a computer system (the class). An object such as the mouse has certain attributes such as weight, size, and color. It also has certain operating methods, such as clicking or scrolling. In summary, an object is an instance of a class that has attributes (object attributes) and can also be used to perform certain operations (methods
). This is what OOP is all about.
Objects in R are simply a term used for the data types in R for various operations and functionalities, just like the mouse is used for clicking and scrolling. In R programming language, there are six basic types of objects. They include:
In the code below, we'll create six object types in R:
# A code to illustrate how R objects are created# creating a Matrix# using the matrix() function to create a matrixmy_matrix <- matrix(c(1,2,3,4,5,6), #datanrow = 2, # number of rowsncol = 2, # number of columnsbyrow = FALSE, # matrix fillingdimname = list(c("row1", "row2"),c("Col1", "Col2")) # names of rows and columns)my_matrix# creating a listmy_list <- list("Apple", "Mango", FALSE, 10.5)my_list# creating a Vectormy_vector <- c(1, 2, 3, 4, 5)my_vector# creating an Arraymy_array <- array(1:6, dim = c(2, 3, 2))my_array# creating a DataFramemy_dataframe <- data.frame(col1 = c("A", "B"), col2 = c(1, 2))my_dataframe# creating a factormy_factor <- factor(c("Tall", "Average", "Short"))my_factor
There are basically two types of classes in R programming language:
S3
classS4
classS3
classesThe S3
classes are the most popular classes in R programming language. The implementation of this class does not require much knowledge of programming.
S3
classTo create an object in the S3
class:
class()
function. The value is assigned to a class name.# creating a list of attributes for the objectmy_object <- list(attribute_1, attribute_2)# defining a class name for the object using the class() functionclass(my_object) <- "class_name"# creating the objectmy_object
my_object
.class()
function.In the code below, we create an object, computer_mouse
, which we’ll assign to a list containing its attributes. To print this object we created, we pass it as an argument to the class()
function.
# A code illustrating how an object of S3 class is being created# creating a list of attributes for the object, computer_mousecomputer_mouse <- list(name = "Dell", size = "small", color = "black", weight =119)# defining a class for the objectclass(computer_mouse) <- "computer_system"# creating the objectcomputer_mouse
computer_mouse
.class()
function. S3
classA generic function contains one or more functions that implement a particular operation depending on the input type.
generic_function.class_name <- function(object{operation(object$object_attributes )})
generic_function
: This represents the name for the generic function..class_name
: This takes the name of the class you created.operation
: This is the particular method or operation you wish to perform.object$
: This is used to access the attributes of the object created.object_attributes
: This represents a specified name of the attributes of the objects created.In continuation with the previous code where we created an S3
class named "computer_system"
, we will create a generic function, print()
, which will help us print any specified attribute of the object, computer_mouse
, by using the operation or method cat()
.
# A code to illustrate how to create a generic function# below is the syntax we will replicate# generic_function.class_name <- function(my_object{# operation(my_object$object_attributes )# })# creating the generic functionprint.Computer_system <- function(object) {cat("The color of the computer mouse is: ", object$color, "\n")cat("The weight of the computer mouse is: ", object$weight, "\n")}# calling the generic functionprint(computer_mouse)
From the code above, the generic function was able to return the object's attributes, computer_mouse
.
S4
classesThe S4
classes are not as popular as the S3
classes. The implementation or how the class works requires much more knowledge of programming. S4
classes are conventional and contain functions used to define the methods and generics. They contain multiple inheritance and multiple dispatch.
S4
classTo create an object in the S4
class:
setClass()
command to define a class.new()
function is called to construct an object of the class.Creating an object from an S4
class takes the syntax below:
# create a class using the setClass() functionsetClass("class_name", slots=list(value_1, value_2))# construct an object using the new() functionobject_name <- new("class_name", value_1, value_2)
setClass()
command to define a class named "class_name"
and as well a list of slot values (value_1
and value_2
) of the desired object."object_name"
, using the new()
function. We will illustrate this syntax using a suitable example in the code example section below:
# A code to illustrate how to create an object of S4 class# to create S4 class containing the class name and list of slots.my_class <- setClass("computer_system", slots=list(name="character",weight="numeric"))# constructing an objectcomputer_mouse <- new("computer_system", name="DELL", weight=119)# Calling objectcomputer_mouse
"computer_system"
and a list of sort for the desired object of the class.computer_mouse
, using the new()
function. computer_mouse
.An object of class "Computer_system"Slot "name":[1] "Adam"Slot "weight":[1] 119
S4
classesThe generic function can help us perform a particular operation based on the input type.
The syntax to use when creating a generic function for the S4
class is shown below:
setMethod("show", "class_name", function(object){operation(object@value_1)})
setMethod
: This is used to define a generic function.show
: This is a generic function.class_name
: This is the name of the class.operation
: This is the operation we wish to perform using the function.object@vlaue_1
: object@
is used to access the value of slots value_1
.# A code to illustrate how a generic function is created for an S4 class# creating the S4 classmy_class <- setClass("computer_system", slots=list(name="character",weight="numeric"))# constructing an object of the classcomputer_mouse <- new("computer_system", name="DELL", weight=119)# creating a generic function for the classsetMethod("show", "computer_System",function(object){cat(obj@name, "\n")cat(obj@age, "\n")})# calling the functionshow(computer_mouse)
"computer_system"
and a list of sort for the desired object of the class.computer_mouse
, using the new()
function. show()
, for the class using setMethod()
. This function will perform the cat()
operation while the slot values of the object are accessed using @
.show()
function and pass the object computer_mouse
as the parameter value.An object of class "computer_system"Slot "name":[1] "DELL"Slot "weight":[1] 119
The code output above shows the result printed in a form with the slot names, along with their respective values.