Classes in R Programming

22 Apr, 2020
Classes and Objects are basic concepts of Object-Oriented Programming that revolve around the real-life entities. Everything in R is an object. An object is simply a data structure that has some methods and attributes. A class is just a blueprint or a sketch of these objects. It represents the set of properties or methods that are common to all objects of one type.
Unlike most other programming languages, R has a three-class system. These are S3, S4, and Reference Classes.
S3 is the simplest yet the most popular OOP system and it lacks formal definition and structure. An object of this type can be created by just adding an attribute to it. Following is an example to make things more clear:
Example:
# create a list with required componentsmovieList <- list(name = "Iron man", leadActor = "Robert Downey Jr")  # give a name to your classclass(movieList) <- "movie"  movieList
Output:
$name
[1] "Iron man"
$leadActor
[1] "Robert Downey Jr"

In S3 systems, methods don’t belong to the class. They belong to generic functions. It means that we can’t create our own methods here, as we do in other programming languages like C++ or Java. But we can define what a generic method (for example print) does when applied to our objects.
print(movieList)
Output:
$name
[1] "Iron man"
$leadActor
[1] "Robert Downey Jr"

Example: Creating a user-defined print function
# now let us write our methodprint.movie <- function(obj){    cat("The name of the movie is", obj$name,".\n")    cat(obj$leadActor, "is the lead actor.\n")}
Output:
The name of the movie is Iron man .
Robert Downey Jr is the lead actor.

Programmers of other languages like C++, Java might find S3 to be very much different than their normal idea of classes as it lacks the structure that classes are supposed to provide. S4 is a slight improvement over S3 as its objects have a proper definition and it gives a proper structure to its objects.
Example:
library(methods)  # definition of S4 classsetClass("movies", slots=list(name="character", leadActor = "character"))  # creating an object using new() by passing class name and slot valuesmovieList <- new("movies", name="Iron man", leadActor = "Robert Downey Jr")movieList
Output:
An object of class "movies"
Slot "name":
[1] "Iron man"
Slot "leadActor":
[1] "Robert Downey Jr"

As shown in the above example, setClass() is used to define a class and new() is used to create the objects.
The concept of methods in S4 is similar to S3, i.e., they belong to generic functions. The following example shows how to create a method:
movieList
Output:
An object of class "movies"
Slot "name":
[1] "Iron man"

Slot "leadActor":
[1] "Robert Downey Jr"

Example:
# using setMethod to set a methodsetMethod("show", "movies",function(object) {    cat("The name of the movie is ", object@name, ".\n")    cat(object@leadActor, "is the lead actor.\n")})movieList
Output:
[1] "show"
The name of the movie is  Iron man .
Robert Downey Jr is the lead actor.

Reference Class is an improvement over S4 Class. Here the methods belong to the classes. These are much similar to object-oriented classes of other languages.
Defining a Reference class is similar to defining S4 classes. We use setRefClass() instead of setClass() and “fields” instead of “slots”.
Example:
library(methods)   # setRefClass returns a generator movies <- setRefClass("movies", fields = list(name = "character",                       leadActor = "character", rating = "numeric"))  #now we can use the generator to create objectsmovieList <- movies(name = "Iron Man",                     leadActor = "Robert downey Jr", rating = 7)movieList
Output:
Reference class object of class "movies"
Field "name":
[1] "Iron Man"
Field "leadActor":
[1] "Robert downey Jr"
Field "rating":
[1] 7

Now let us see how to add some methods to our class with an example.
Example
library(methods)  movies <- setRefClass("movies", fields = list(name = "character",                        leadActor = "character", rating = "numeric"), methods = list(                       increment_rating = function()                       {                           rating <<- rating + 1                       },                       decrement_rating = function()                       {                           rating <<- rating - 1                       }                     ))movieList <- movies(name = "Iron Man",                     leadActor = "Robert downey Jr", rating = 7)  # print the value of ratingmovieList$rating  # increment and then print the ratingmovieList$increment_rating()movieList$rating  # decrement and print the ratingmovieList$decrement_rating()movieList$rating
Output:
[1] 7
[1] 8
[1] 7

Picked
R-OOPs
R Language
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.