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
In R, the concept of "classes" is not as rigorous as in traditional object-oriented languages like Java or Python. Instead, R uses a more flexible system based on generic functions and method dispatch, known as the S3 system. The S4 system is another system that's more formal than S3 but less commonly used.
In this tutorial, we will go over the basics of the S3 system, which is the more prevalent method of creating and using classes in R.
In the S3 system, an object's class is simply a string that gets attached to it. Here's how you can create a new S3 class:
# Creating an object of class 'person' person <- list(name = "John", age = 30) class(person) <- "person"
Methods in the S3 system are just regular functions with a special naming convention: [generic].[class]
.
For example, let's create a print
method for our person
class:
print.person <- function(x) { cat(paste("Name:", x$name, "\n")) cat(paste("Age:", x$age, "\n")) } # Now, using the print function on a 'person' object will use our custom method print(person)
A generic function is a function that calls the class-specific method based on the class of its argument.
# The `print` function is already a generic, but let's see how we'd define one ourselves: greet <- function(x) { UseMethod("greet") } greet.person <- function(x) { paste("Hello, my name is", x$name, "and I'm", x$age, "years old.") } greet(person)
S3 classes can have inheritance. If a method for a specific class is not found, R looks for methods of the parent class.
class(person) <- c("employee", "person") greet.employee <- function(x) { paste("Hello, I work here and my name is", x$name) } greet(person) # This will use the `greet.employee` method because "employee" is the first class in the class vector
Use class(object)
to get the class of an object and is(object, "class")
to check if an object is of a certain class.
class(person) # Returns "employee" "person" is(person, "employee") # Returns TRUE
Classes in R, particularly using the S3 system, provide a flexible way to implement object-oriented-like behavior without the strictness seen in languages like Java. With S3, you can create classes, define methods for these classes, and make use of inheritance. The simplicity and flexibility of S3 make it popular, but for more formal class definitions, the S4 system or the R6 package can be used.
How to Create and Define Classes in R:
# Define a simple S3 class setClass("Person", slots = c(name = "character", age = "numeric")) person <- new("Person", name = "John", age = 30)
Object-Oriented Programming in R:
# Create methods for the Person class setMethod("show", "Person", function(object) { cat("Name:", object@name, "\n") cat("Age:", object@age, "\n") }) # Call the method show(person)
Inheritance and Polymorphism in R Classes:
# Define a Student class inheriting from Person setClass("Student", contains = "Person", slots = c(grade = "numeric")) student <- new("Student", name = "Alice", age = 25, grade = 95) # Polymorphism - show method works for both Person and Student show(student)
S3 Classes vs S4 Classes in R:
# S3 class example my_s3_object <- structure(list(name = "Bob", age = 40), class = "Person") # S4 class example setClass("S4Person", slots = c(name = "character", age = "numeric")) my_s4_object <- new("S4Person", name = "Bob", age = 40)
Class Methods and Functions in R:
# Define a method for the Person class setMethod("introduce", "Person", function(object) { cat("Hi, I'm", object@name, "and I'm", object@age, "years old.\n") }) # Call the method introduce(person)
Defining Constructors in R Classes:
# Define a constructor for the Person class Person <- setClass( "Person", representation(name = "character", age = "numeric"), prototype = list(name = character(), age = numeric()) ) # Create an instance using the constructor person_instance <- new("Person", name = "Sam", age = 35)
Accessing and Modifying Class Attributes in R:
# Access and modify attributes person_name <- person@name person@age <- 31