What is a Class and its Types?
In this lesson, we will learn what Classes and their types are and how to create them in the R language.
We'll cover the following
Introduction
Functional languages like R make use of the concepts of objects and classes. An object is comprised of a set of variables and functions. A class is a blueprint for an object. It refers to the set of attributes or functions common to all objects of a specific type.
Class Types
R includes 3 class systems, compared to other programming languages that have only one class system.
S3 class
S4 class
Reference class (RC)
Each has unique features and characteristics, so selecting one over the other comes down to personal preference. We provide a concise overview of them below.
S3 Class
The nature of an S3 class is relatively primitive. The object of this class can be generated by simply adding a class attribute to it, and it has no formal definition. Following is an example to make things more clear:
# creating a student liststudentList <- list(name = "John", gender = "Male", semester = 4)# assigning a class name.class(studentList) <- "Student"# creating and calling an objectstudentList
In the above example, we have created a list named studentList
with three components name
, gender
, and semester
. Here, Student
is the name of the class. And to create an object of this class, we have passed the studentList
list inside class()
.
S4 class
S3 class type is a very different type of class that most programmers have encountered in other OOP languages as there is no structure that we usually see in classes from C++ and Java.
'OOP' refers to object-oriented programming which is a programming approach centered on "objects" that can contain data. Learn more about object-oriented programming in R here.
We have another type of class, which is a little improvement from what is provided by S3: the S4 class type.
# The package "methods" implement functional computations when# one or more of the arguments is an object from these classes.# We've added it here for setClass().library(methods)# definition of S4 classsetClass("person", slots=list(name="character", age = "numeric"))# Creating an object using new() by passing class name and slot valuespersonList <- new("person", name="Adam", age = 25)personList
The method setClass()
is used to define the class and the new()
the method defines a new object of that class.
The concept of functions within classes in S4 is generally the same as in S3. That is, they are generic. Here is an example:
# using setMethod to set a methodsetMethod("show", "person",function(object){cat("The name of the person is ", object@name, " and age:",object@age)})personList
Reference class (RC)
The third type of class in R is known as Reference classes. These are the most similar to other OOP classes. In a reference class, the methods belong to the class unlike in S3 and S4 classes, where the methods are generic.
Defining a Reference class is very similar to how we defined classes in the S4 class. There are differences though. Instead of using the setClass()
method, we use the setRefClass()
, and instead of “slots”, we have “fields”. Let’s look at an example:
# The package "methods" implement functional computations when# one or more of the arguments is an object from these classes.# We've added it here for setRefClass().library(methods)# setRefClass returns a generatorperson <- setRefClass("person", fields = list(name = "character",age = "numeric"))# now to create objects, we use the generatorpersonList <- person(name = "Adam", age= 72)personList
Now, let’s have an example of having a method in our class:
# The package "methods" implement functional computations when# one or more of the arguments is an object from these classes.# We've added it here for setRefClass().library(methods)person <- setRefClass("movies", fields = list(name = "character",age= "numeric"), methods = list(update_age = function(){# We're using global assignment# operator (<<-) because the variable age is# outside the scope of this function.age <<- age + 1}))personList <- person(name = "Adam", age= 72)# print the value of ratingpersonList$age# update age and then print the ratingpersonList$update_age()personList$age
Click here to learn more about the global assignment operator (<<-
) used in Line 13 in the above code block. Essentially, global assignment means that we can access the value of the variable anywhere in the entire program.
Comparison of S3, S4, and RC
Let us summarise the differences between the three different types of classes in R in the table given below:
S3 Class | S4 Class | Reference Class |
---|---|---|
No formal definition | Class defined by setClass() method |
Class defined by setRefClass() method |
By setting the class attribute, objects are created | Objects are created by the new() method |
Generator functions are used to create objects |
$ is used to access attributes |
@ is used to access attributes |
$ is used to access attributes |
Methods are part of the generic function | Methods are part of the generic function | Methods are members of the class |