Objects and Classes in R - DataCamp PDF
Objects and Classes in R - DataCamp PDF
Olivia Smith
June 22nd, 2020
R PROGRAMMING
In Object-Oriented programming, the whole program can be divided into different classes
to quickly understand the program execution. R is a functional programming language,
and OOP will help manage large system problems. OOP used to manage GUI applications,
most likely web-applications. Object-Oriented Programming is good for building tools for
data analysis but bad for data analysis itself.
Systems of OOP
There are mainly two major systems of OOP, which are described below:
S4 Classes: These let you limit the data as it is quite dif cult to debug the program
https://www.datacamp.com/community/tutorials/r-objects-and-classes 1/11
1/1/2021 Objects and Classes in R - DataCamp
Objects in R
Objects are the instance of the class. Also, everything in R is an object and to know more
look at Data types in R. They also can have their attributes like class,
attributes,dimnnames, names, etc.
For example, objects are assigned a value using <-, consider the following piece of code
where 'a1' and 'a2' are the variables where the numeric value is assigned to it.
a1 <- 10
a2 <- 20
An object can be assigned a set of numbers where 'a3' is the object containing a vector of
different numbers.
a3 <- c(10,20,30)
Let's take a real-life example like a student, which has attributes of name, age, class, and
gender, the teacher, has a name, age, and gender.
https://www.datacamp.com/community/tutorials/r-objects-and-classes 2/11
1/1/2021 Objects and Classes in R - DataCamp
print(student)
print(teacher)
In the above code, you can see the example where there are different types of objects.
The 'student' and 'teacher' object, also called a variable, hold type 'character' and 'double.'
Class in R
Class is the blueprint that helps to create an object and contains its member variable
along with the attributes.
As discussed earlier in the previous section, there are two classes of R, S3, and S4.
S3 Class
These classes help in overloading functions by splitting them into generic and methods.
S3 is very different from other programming languages like C#, Java, etc. Because it's
straightforward to implement, it uses only the rst argument to dispatch. That's why it
doesn't require too much knowledge as a programmer's perspective.
generic.class
The arguments of methods should contain arguments of generic, and method should
include a ...arg.
https://www.datacamp.com/community/tutorials/r-objects-and-classes 3/11
1/1/2021 Objects and Classes in R - DataCamp
You will be demonstrated to construct an S3 class in the following example, which shows
the creation of the list where components are being passed along with proper class name
followed by the result to be printed.
The following example shows 'studentBio' contains a list where components are passed as
a list where 'student_name,' 'student_age,' and 'student_contact' are being given. The
class is named 'Student Info,' and 'studentBio' is the newly created class whose values are
displayed below.
$studentName
[1] "Harry Potter"
$studentAge
[1] 19
$studentContact
[1] "London"
attr(,"class")
[1] "StudentInfo"
$studentName
"Harry Potter"
$studentAge
19
$studentContact
"London"
https://www.datacamp.com/community/tutorials/r-objects-and-classes 4/11
1/1/2021 Objects and Classes in R - DataCamp
attr(,"class")
"StudentInfo"
If you are not comfortable about writing function in R, then read a A Tutorial on Using
Functions in R!
You will be implementing your generic function below by using the following code.
The code below shows the generic function called 'contact' where the function passing
with the object is assigned to it. Also, 'UseMethod' is used with the generic function called
'contact' is passed where 'dispatching' of method occurs, which is the process where the
result varies based on the arguments passed.
Let's make a method for the particular class that you've created earlier named
'StudentInfo.' The method 'contact' followed by dot notation along with the class
'StudentInfo' is used where the function body contains the access of the attribute
'studentContact' is done by using '$.'
The above code uses object$studentContact , where the object passed as an argument
to function along with '$' to access attribute 'studentContact,' which results in output as
below.
https://www.datacamp.com/community/tutorials/r-objects-and-classes 5/11
1/1/2021 Objects and Classes in R - DataCamp
S4 Class
S4 Class is stricter, conventional, and closely related to Object-Oriented concepts. The
classes are represented by the formal class de nitions of S4. More speci cally, S4 has
setter and getter functions for methods and generics. As compared to the S3 class, S4 can
be able to facilitate multiple dispatches.
Let us see the above-discussed classes of R by creating them and discuss with an
example.
setClass()
As discussed earlier, the S4 class is de ned by the setClass() method. You will use the
setClass() method to create and de ne the S4 class. We will use a subroutine to verify that
the data is consistent (validation) and also set the default values (the paradigm).
The following is used to de ne a class, also called 'prototype', where the slots are de ned
where the class name is 'employee', name is a character, id as a numeric, and contact as a
character.
The following code can create the speci c instance or objects of S4.
Let's create an object using 'new' with the class name inside as 'employee' with the slots
like name, id, and contact are lled with a value.
[1] "Steven"
Slot "id":
[1] 1002
Slot "contact":
[1] "West Avenue"
Slot "id":
1002
Slot "contact":
"West Avenue"
The above output shows that the object created was of class "employee" and the Slot with
"name" called "Steven" along with Slot "id" with value 1002 and Slot "contact" with value is
"West Avenue".
For checking whether an object is an S4 object or not following two commands can be
used:
is.object(obj)
TRUE
TRUE
https://www.datacamp.com/community/tutorials/r-objects-and-classes 7/11
1/1/2021 Objects and Classes in R - DataCamp
isS4(obj)
TRUE
TRUE
'obj@name' is used where the object name along with the slot name is to access the name
'Steven' and is similarly done to 'obj@id'.
obj@name
obj@id
'Steven'
1002
'Steven'
1002
Let's modify the slot value just by using the assignment operator("<-") where the
'obj@contact' is assigned with a new value as "North Avenue".
https://www.datacamp.com/community/tutorials/r-objects-and-classes 8/11
1/1/2021 Objects and Classes in R - DataCamp
Slot "id":
[1] 1002
Slot "contact":
[1] "North Avenue"
Slot "id":
1002
Slot "contact":
"North Avenue"
The above output shows that the Slot "contact" has changed it's value previously "West
Avenue" to "North Avenue".
'slot(obj, "id")' indicates the slot "id" value to ve changed with the assignment of 1004, and
the result is printed with 'obj'.
Slot "id":
https://www.datacamp.com/community/tutorials/r-objects-and-classes 9/11
1/1/2021 Objects and Classes in R - DataCamp
[1] 1004
Slot "contact":
[1] "North Avenue"
Slot "id":
1004
Slot "contact":
"North Avenue"
The above output shows that the Slot "id" has changed its value previously 1002 to 1004.
Let's create a function that helps print the output according to our needs, where the
'setMethod()' and the generic function called 'show()' is used. Also, all the slots' value is
accessed by '@', and their names are printed out. Finally, the output is obtained with the
help of 'obj'.
setMethod("show",
"employee",
function(object) {
cat("Name:",object@name, "\n")
cat("Id:",object@id, "\n")
cat("Contact:", object@contact, "\n")
}
)
obj
Name: Steven
Id: 1004
Contact: North Avenue
https://www.datacamp.com/community/tutorials/r-objects-and-classes 10/11
1/1/2021 Objects and Classes in R - DataCamp
Name: Steven
Id: 1004
Contact: North Avenue
The above output shows that the results are printed as in the form of Slot names along
with respective Slot values.
Congratulations
In this tutorial, you've learned about Object-Oriented Programming(OOP) along with R's
objects, different classes like S3 and S4, along with its construction, creating its generic
function with examples and many more.
If you would like to learn more about R, take DataCamp's Object-Oriented Programming
with S3 and R6 in R.
9 0
Subscribe to RSS
https://www.datacamp.com/community/tutorials/r-objects-and-classes 11/11