R Tutorial
Fundamentals of R
Variables
Input and Output
Decision Making
Control Flow
Functions
Strings
Vectors
Lists
Arrays
Matrices
Factors
DataFrames
Object Oriented Programming
Error Handling
File Handling
Packages in R
Data Interfaces
Data Visualization
Statistics
Machine Learning with R
Encapsulation is a foundational concept in object-oriented programming (OOP). It refers to the bundling of data (attributes) and methods (functions) that operate on the data into a single unit called an object. Moreover, it restricts direct access to some of the object's components, ensuring that unwanted or unexpected modifications can't occur.
While R is primarily a functional programming language, it provides support for OOP principles, including encapsulation, through systems like S3, S4, and R6. In this tutorial, we will focus on the R6 system, which is the most similar to traditional OOP languages.
To use R6, you need to install and load the R6 package:
install.packages("R6") library(R6)
Let's create a simple class for a bank account:
BankAccount <- R6Class("BankAccount", # Private data members private = list( balance = 0 ), # Public methods public = list( deposit = function(amount) { if (amount <= 0) { warning("Deposit amount should be positive.") return(invisible(self)) } private$balance <- private$balance + amount cat("Deposited:", amount, "\n") invisible(self) }, withdraw = function(amount) { if (amount <= 0) { warning("Withdrawal amount should be positive.") return(invisible(self)) } if (amount > private$balance) { warning("Insufficient funds.") return(invisible(self)) } private$balance <- private$balance - amount cat("Withdrew:", amount, "\n") invisible(self) }, check_balance = function() { cat("Current balance:", private$balance, "\n") invisible(self) } ) )
Now, you can create objects of this class and interact with them:
# Create a new bank account object account1 <- BankAccount$new() # Interact with the account account1$deposit(100) account1$check_balance() account1$withdraw(30) account1$check_balance()
While R6 is a popular and modern OOP system in R, there are older systems:
However, neither S3 nor S4 offers the same level of encapsulation as R6.
Encapsulation is a powerful concept that helps to ensure data integrity and structure in your code. While R isn't a fully object-oriented language like Python or Java, with packages like R6, you can effectively use OOP principles, including encapsulation, in your R projects.
Encapsulation in R programming:
# Example of encapsulation in R person <- list( name = "John", age = 30, getAge = function() { return(age) } ) # Accessing encapsulated data and method print(person$name) print(person$getAge())
Object-oriented programming in R:
# Example of creating an object in R my_object <- list(attribute1 = "value1", attribute2 = 42)
Encapsulating data and methods in R:
# Define a class in R Person <- R6::R6Class( "Person", public = list( name = NULL, age = NULL, initialize = function(name, age) { private$name <- name private$age <- age }, getAge = function() { return(private$age) } ), private = list( name = NULL, age = NULL ) ) # Create an instance of the class john <- Person$new("John", 30) # Accessing encapsulated data and method print(john$name) print(john$getAge())