0% found this document useful (0 votes)
4 views7 pages

Classes and Objects Are Basic Concepts of Object

The document explains the concepts of Classes and Objects in Object-Oriented Programming (OOP) within the R programming language, detailing its three-class system: S3, S4, and Reference Classes. S3 is the simplest and most popular, allowing object creation by adding attributes, while S4 provides a more structured approach with defined slots. Reference Classes further enhance OOP by allowing methods to belong to classes, similar to other programming languages, and includes examples of creating and manipulating objects.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
4 views7 pages

Classes and Objects Are Basic Concepts of Object

The document explains the concepts of Classes and Objects in Object-Oriented Programming (OOP) within the R programming language, detailing its three-class system: S3, S4, and Reference Classes. S3 is the simplest and most popular, allowing object creation by adding attributes, while S4 provides a more structured approach with defined slots. Reference Classes further enhance OOP by allowing methods to belong to classes, similar to other programming languages, and includes examples of creating and manipulating objects.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 7

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 Class
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 components

movieList <- list(name = "Iron man", leadActor = "Robert Downey Jr")

# give a name to your class

class(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 method

print.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.
S4 Class
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 class

setClass("movies", slots=list(name="character", leadActor =


"character"))

# creating an object using new() by passing class name and slot


values

movieList <- 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 method

setMethod("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
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 objects

movieList <- 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 rating

movieList$rating

# increment and then print the rating

movieList$increment_rating()

movieList$rating

# decrement and print the rating

movieList$decrement_rating()

movieList$rating

Output:
[1] 7
[1] 8
[1] 7

You might also like