R6 Classes in R Programming
Last Updated :
06 Jun, 2023
In Object-Oriented Programming (OOP) of R Language, encapsulation means binding the data and methods inside a class. The R6 package is an encapsulated OOP system that helps us use encapsulation in R. R6 package provides R6 class which is similar to the reference class in R but is independent of the S4 classes. In R6, you define a class by creating a new R6Class object, with the name of the class and a list of its properties and methods. Properties can be any R object, while methods are functions that can be called on objects of the class.
To create an instance of an R6 class, you use the $new() method of the class object, passing in any initial values for the properties. Once you have an object instantiated from the class, you can call its methods and access or modify its properties using the $ operator.
One key feature of R6 is that it allows for encapsulation and information hiding, meaning that the internal workings of an object can be hidden from the user. This can make it easier to create more complex and robust programs.
Overall, R6 provides a powerful way to organize your code and create custom objects with their own properties and behaviors, making it a useful tool for building more advanced R programs. Along with the private and public members, R6 Classes also support inheritance even if the classes are defined in different packages. Some of the popular R packages that use R6 classes are dplyr and shiny.
Example:
R
library (R6)
Queue <- R6Class ( "Queue" ,
public = list (
initialize = function (...)
{
private$elements = list (...)
},
enqueue = function (num)
{
private$elements <- append (private$elements, num)
},
dequeue = function ()
{
if (self$ size () == 0)
stop ( "Queue is empty" )
element <- private$elements[[1]]
private$elements <- private$elements[-1]
element
},
size = function ()
{
length (private$elements)
}),
private = list (
elements = list ())
)
QueueObject = Queue$ new ()
QueueObject$ enqueue (2)
QueueObject$ enqueue (5)
QueueObject$ dequeue ()
QueueObject$ dequeue ()
|
Output:
[1] 2
[1] 5
Both public and private members can be used. The queue (elements) is private so that the object can not modify it externally. The initialize function is the constructor for initializing the object. object$member is used for accessing the public members outside the class whereas private$member is used for accessing the private members inside the class methods. self refers to the object that has called the method.
The following example is a demonstration of Inheritance in R6 classes by creating another class childQueue that inherits the class Queue.
Example
R
childQueue <- R6Class ( "childQueue" ,
inherit = Queue,
public = list (
initialize = function (...)
{
private$elements <- list (...)
},
size = function ()
{
super$ size ()
}
))
childQueueObject <- childQueue$ new ()
childQueueObject$ enqueue (2)
childQueueObject$ enqueue (3)
childQueueObject$ dequeue ()
childQueueObject$ dequeue ()
|
Output:
[1] 2
[1] 3
childQueue class can use enqueue() and dequeue() functions from the Queue superclass. size method overrides the size() from the super class Queue but calls the size() of Queue internally using super. These classes can be inherited across packages.