Introduction to S3 Classes
S3 is one of the simplest object-oriented systems in R, based on a “class attribute” approach. Objects can belong to multiple classes, and method dispatch is based on the class attribute.
Creating an S3 Class
An S3 class is essentially a list with a class attribute that specifies the class of the object.
Example of Creating an S3 Class
Define an Object of S3 Class:
# Create a simple object person <- list(name = "Alice", age = 30) # Assign a class to this object class(person) <- "Person" # Check the class of the object class(person) # Prints "Person"
Using structure() to Create an Instance:
# Create an instance of an S3 object using `structure()` person <- structure(list(name = "Bob", age = 25), class = "Person")
Defining Methods for S3 Classes
Methods in S3 are functions associated with classes. A generic function calls different methods based on the class of the object.
Example of a Method for print of an S3 Class
Define a print Method for the Person Class:
# Print method for the Person class print.Person <- function(x) { cat("Name:", x$name, "\nAge:", x$age, "\n") } # Use the print method print(person) # Calls print.Person
Creating Generic Functions
Generic functions choose which method to call based on the class of the object.
Example of a Generic Function
Define a Generic Function describe:
# Generic function describe describe <- function(x) { UseMethod("describe") } # Method describe for the Person class describe.Person <- function(x) { cat("Name:", x$name, "\nAge:", x$age, "\n") } # Use the describe function describe(person) # Calls describe.Person
Managing Inheritance
In S3, inheritance is managed by assigning multiple classes to an object. Methods are called based on the first class in the list.
Example of Inheritance
Define a Subclass Student:
# Create an object with a subclass student <- structure(list(name = "Charlie", age = 22, student_id = "S123"), class = c("Student", "Person")) # Print method for Student print.Student <- function(x) { print.Person(x) # Calls the print method for Person cat("Student ID:", x$student_id, "\n") } # Use the print method print(student) # Calls print.Student
S3 Methods and Dispatch
S3 dispatch is based on the first class of the object’s class attribute. When a generic function is called, R looks for methods in the order of the classes.
Example of Dispatch
# Method for another class print.Animal <- function(x) { cat("Animal Name:", x$name, "\n") } # Create an Animal object animal <- structure(list(name = "Lion"), class = "Animal") # Call print print(animal) # Calls print.Animal
Extending and Adapting Methods
S3 methods can be extended to handle new functionalities or data types.
Example of Extension
Add a summary Method:
# Generic function summary summary <- function(x) { UseMethod("summary") } # Method summary for Person summary.Person <- function(x) { cat("Summary:\n") cat("Name:", x$name, "\nAge:", x$age, "\n") } # Use the summary function summary(person) # Calls summary.Person
Managing S3 Objects
S3 objects can be listed, removed, saved, and checked for existence.
Example of Object Management
List Objects:
# List objects in the environment ls()
Remove an Object:
# Remove the person object rm(person)
Save Objects:
# Save objects to a file save(person, file = "person.RData")
Check Existence of an Object:
# Check if the person object exists exists("person")
Conclusion
S3 classes in R provide a simple and flexible way to perform object-oriented programming. They allow you to define objects with class attributes, create generic functions and methods, and manage inheritance intuitively. This approach is widely used for its simplicity and adaptability to specific needs