Classes in R Programming

Classes in detail

Classes and Objects form the foundation of Object-Oriented Programming (OOP). They revolve around real-world entities. In R, everything is treated as an object. An object is essentially a data structure that contains attributes and methods. A class acts as a template or blueprint for creating objects, defining a set of properties or behaviors common to all objects of that type.

Unlike many other programming languages, R features a three-class system: S3S4, and Reference Classes.

Classes and Objects

class is a template or blueprint from which objects are created by encapsulating data and methods. An object is a data structure containing attributes and methods that act upon those attributes.

S3 Class

The S3 class is the simplest and most flexible OOP system in R. It does not enforce a rigid structure or formal definitions. You can create an object of the S3 class by assigning a class attribute to it.

Example:

# Create a list with relevant attributes
bookDetails <- list(title = "The Alchemist", author = "Paulo Coelho")

# Assign a class name
class(bookDetails) <- "book"

bookDetails

Output:

$title
[1] "The Alchemist"

$author
[1] "Paulo Coelho"

In S3, methods are not tied to specific classes but to generic functions. For example, you can customize how the print function behaves for your objects.

Example: Custom Print Method

# Define a custom print method for class "book"
print.book <- function(obj) {
    cat("Title:", obj$title, "\n")
    cat("Author:", obj$author, "\n")
}

# Print the object
print(bookDetails)

Output:

Title: The Alchemist
Author: Paulo Coelho
S4 Class

The S4 class offers a more structured approach to OOP in R, which programmers familiar with C++ or Java may find more intuitive. It provides formal class definitions, which include specified slots for storing data.

Example:

library(methods)

# Define an S4 class
setClass("novel", slots = list(title = "character", author = "character"))

# Create an object of the S4 class
novelDetails <- new("novel", title = "1984", author = "George Orwell")
novelDetails

Output:

An object of class "novel"
Slot "title":
[1] "1984"

Slot "author":
[1] "George Orwell"

S4 classes also allow you to define methods. Here’s an example:

Example: Custom Method

# Define a custom method for class "novel"
setMethod("show", "novel",
          function(object) {
              cat("Title:", object@title, "\n")
              cat("Author:", object@author, "\n")
          })

# Display the object
novelDetails

Output:

# View attributes
attributes(student)

You can also define methods for Reference Classes:

Example: Adding Methods

libraryDetails <- setRefClass("library", fields = list(
    name = "character",
    location = "character",
    booksAvailable = "numeric"
), methods = list(
    addBooks = function(count) {
        booksAvailable <<- booksAvailable + count
    },
    removeBooks = function(count) {
        booksAvailable <<- booksAvailable - count
    }
))

# Create an object and manipulate its fields
libraryInfo <- libraryDetails(name = "Central Library", location = "Downtown", booksAvailable = 5000)

# Print books available
libraryInfo$booksAvailable

# Add 200 books and print the count
libraryInfo$addBooks(200)
libraryInfo$booksAvailable

# Remove 100 books and print the count
libraryInfo$removeBooks(100)
libraryInfo$booksAvailable

Output:

[1] 5000
[1] 5200
[1] 5100

Comments

Leave a Reply

Your email address will not be published. Required fields are marked *