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
Inheritance is a fundamental concept in object-oriented programming where a new class (derived class) inherits properties and behaviors from an existing class (base class). R provides support for inheritance through its S3 and S4 object systems, and more recently, with the R6 system.
In this tutorial, we'll explore inheritance in the R6 system, which offers a more formal and consistent object-oriented approach in R.
The R6 package in R offers a way to encapsulate stateful classes with reference semantics. Unlike S3 and S4, where objects are copied by value, R6 objects are reference-based, which means they can be modified in place.
To use R6, you'll need to install and load the R6 package:
install.packages("R6") library(R6)
Let's create a simple base class called Person
:
Person <- R6Class("Person", public = list( name = NULL, age = NULL, initialize = function(name, age) { self$name <- name self$age <- age }, print_info = function() { cat("Name:", self$name, "\n") cat("Age:", self$age, "\n") } ) )
To create a derived class that inherits from the Person
class, we specify the inherit
argument:
Employee <- R6Class("Employee", inherit = Person, public = list( title = NULL, initialize = function(name, age, title) { super$initialize(name, age) self$title <- title }, print_info = function() { super$print_info() cat("Title:", self$title, "\n") } ) )
Here, Employee
is a derived class that inherits from Person
. The super$initialize(name, age)
call is invoking the initialize method of the base class, Person
.
Let's create objects of these classes:
# Create a Person object john <- Person$new("John Doe", 30) john$print_info() # Output: # Name: John Doe # Age: 30 # Create an Employee object jane <- Employee$new("Jane Smith", 28, "Engineer") jane$print_info() # Output: # Name: Jane Smith # Age: 28 # Title: Engineer
Overriding: As seen in the Employee
class, we overrode the print_info
method. This means the derived class provides a new implementation for a method that's already defined in the base class.
Super: The super
keyword is used to call methods from the base class. This is especially useful when overriding methods but still wanting to use the base class's original functionality.
Inheritance allows for creating hierarchies of classes, promoting code reuse, and establishing relationships between objects. R6 provides a more formal and intuitive approach to object-oriented programming and inheritance in R, especially when compared to the S3 and S4 systems. As always, it's essential to use the right tool for the job �C and for certain tasks, R6's reference-based system and OOP features make it the best choice.
S4 classes and inheritance in R: S4 classes in R are a system for object-oriented programming that supports formal class definitions and inheritance.
R base class and subclass relationships:
# Define a base class setClass("Animal", slots = list(name = "character", age = "numeric")) # Define a subclass inheriting from the base class setClass("Mammal", contains = "Animal", slots = list(furColor = "character"))
Defining and using methods in R inheritance:
# Define a method for the Animal class setMethod("print", "Animal", function(object) { cat("Name:", object@name, "\n") cat("Age:", object@age, "\n") }) # Create an instance of the Mammal class my_mammal <- new("Mammal", name = "Leo", age = 5, furColor = "Brown") # Use the print method for the Mammal class print(my_mammal)
Inheriting from multiple classes in R:
# Define a second base class setClass("Bird", slots = list(name = "character", age = "numeric", featherColor = "character")) # Define a subclass inheriting from both Animal and Bird classes setClass("Parrot", contains = c("Animal", "Bird"))
Method dispatch and inheritance in R: Method dispatch in R refers to the process of selecting the appropriate method to execute based on the class hierarchy of the objects involved.
Creating custom classes and inheritance hierarchies in R:
# Define a custom class setClass("Person", slots = list(name = "character", age = "numeric")) # Define a subclass inheriting from Person class setClass("Employee", contains = "Person", slots = list(employeeID = "character")) # Create an instance of the Employee class my_employee <- new("Employee", name = "John", age = 30, employeeID = "E123") # Access slots my_employee@name my_employee@employeeID