R6 Classes in R Programming

In Object-Oriented Programming (OOP) of R Language, encapsulation means binding of the data and methods inside a class. 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. 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:

filter_none

edit
close

play_arrow

link
brightness_4
code

library(R6)
  
Queue <- R6Class("Queue",
  
# public members of the class
# that can be accessed by object
public  =  list(
      
# constructor/initializer  
initialize = function(...)
{
    private$elements = list(...)
},
  
# add to the queue
enqueue = function(num) 
{
    private$elements <- append(private$elements, num)
},
  
# remove from the queue
dequeue = function()
{
    if(self$size() == 0)
    stop("Queue is empty")
    element <- private$elements[[1]]
    private$elements <- private$elements[-1]
    element
},
  
# get the size of elements
size = function()
{
    length(private$elements)
}),
  
private = list(
      
  # queue list
  elements = list())
)
  
# create an object
QueueObject = Queue$new()
  
# add 2
QueueObject$enqueue(2)
  
# add 5
QueueObject$enqueue(5)
  
# remove 2
QueueObject$dequeue()
  
# remove 5
QueueObject$dequeue()

chevron_right


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:

filter_none

edit
close

play_arrow

link
brightness_4
code

childQueue <- R6Class("childQueue",
  
# inherit Queue class which is the super class
inherit = Queue,
public = list(
initialize = function(...)
{
    private$elements <- list(...)
},
size = function()
{
    # super is used to access methods from super class
    super$size()
}
))
  
childQueueObject <- childQueue$new()
childQueueObject$enqueue(2)
childQueueObject$enqueue(3)
childQueueObject$dequeue()
childQueueObject$dequeue()

chevron_right


Output:

[1] 2
[1] 3

childQueue class can use enqueue() and dequeue() functions from the Queue super class. size method overrides the size() from super class Queue but calls size() of Queue internally using super. These classes can be inherited across packages.




My Personal Notes arrow_drop_up

Recommended Posts:

    Article Tags :

    5


    Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.