Assignment 7
Assignment 7: Exploring R’s Object-Oriented Systems (S3 & S4)
In this assignment, I explored R's object-oriented systems, focusing on S3 and S4 classes. The objective was to understand how R dispatches generic functions based on object class and to practice creating and manipulating S3 and S4 objects.
Objectives
The goal of this assignment was to explore R’s two primary object-oriented systems—S3 and S4—and understand how generic functions work with different object types.
Specifically, we:
-
Selected and inspected a dataset.
-
Tested base R generic functions.
-
Created custom S3 and S4 objects.
-
Compared the behavior and structure of both systems.
R Code
Step 1: Loading the Dataset
We begin by using one of R’s built-in datasets, PlantGrowth, which measures the effect of different treatments on plant growth.
# Load dataset
data("PlantGrowth")
head(PlantGrowth)
# Describe structure
str(PlantGrowth)
Output:
Step 2: Test Generic Functions
# Summarize dataset
summary(PlantGrowth)
# Basic plot of PlantGrowth data
plot(PlantGrowth$group, PlantGrowth$weight,
main = "PlantGrowth Weight by Group",
xlab = "Group",
ylab = "Weight",
col = c("red", "green", "blue"))
Output:
The dataset contains 30 observations and 2 variables:
weight— numeric values representing plant weights.group— factor variable indicating treatment groups (“ctrl”, “trt1”, “trt2”).
Everything loads and displays correctly.Figure 1: Boxplot of PlantGrowth Weight by Group
Step 3: Creating an S3 Object
S3 objects in R are flexible and informal. They can be built from simple lists and assigned a custom class name.
# Create an S3 object
s3_obj_pg <- list(weight ~ group, data = PlantGrowth)
print(s3_obj_pg)
# Assign S3 class
class(s3_obj_pg) <- "PlantExperiment"
print(s3_obj_pg)
print(class(s3_obj_pg))
# Check object type
install.packages("pryr")
library(pryr)
otype(s3_obj_pg) # Object type
typeof(s3_obj_pg) # Base type ("list")
Output:
"PlantExperiment".print() or summary().Step 4: Creating an S4 Class and Object
S4 objects are more formal and rigorous. They require explicit class definitions with defined slots and types.
# Define S4 class
setClass("PlantExperimentS4",
slots = c(weight = "numeric", group = "character"))
# Create an instance
s4_obj_pg <- new("PlantExperimentS4", weight = 5.2, group = "ctrl")
# Check class system
class(s4_obj_pg)
isS4(s4_obj_pg) # TRUE
print(s4_obj_pg)
Output:
# Base type and attributes
typeof(s4_obj_pg) # "S4"
attributes(s4_obj_pg) # Slots
is.object(s4_obj_pg) # TRUE
Output:
The S4 system uses slots instead of list components, and the structure is strictly validated when the object is created. Any mismatch in slot types or missing slots would generate an error.
Questions
1. How can you tell whether an object uses S3 or S4?
-
Use
class(object)to check the class of an object. -
Use
isS4(object)to see if it is an S4 object (TRUEmeans S4,FALSEmeans S3). -
S3 objects have a simple
classattribute; S4 objects are formally defined withsetClass()and contain slots.
2. How do you determine an object’s underlying type (e.g., integer vs. list)?
-
Use
typeof(object)to identify the low-level storage type (e.g.,"integer","list","double","character"). -
mode(object)can also provide type information. -
You can test specific types using logical functions like
is.numeric(),is.list(), oris.character().
3. What is a generic function in R?
-
A generic function in R is a function that performs different actions depending on the class of the object it is applied to. This behavior is called method dispatch.
-
Examples:
print(),summary(),plot(). -
R automatically dispatches the correct method for the object’s class—for example,
summary.lm()for linear models orsummary.data.frame()for data frames.
4. What are the principal differences between S3 and S4?
- S3 is simple and flexible. Classes are created informally by adding a
class()attribute, and methods are defined by naming convention (likeprint.myclass). There’s no strict checking of data types or structure, which makes S3 easy to use but less controlled. - S4 is formal and strict. Classes are defined with
setClass()and have specific slots that must match declared data types. Methods are created withsetMethod(), and objects are validated to ensure they fit the definition. - In short, S3 is quick and convenient, while S4 is structured and safer for larger, more complex projects.
Conclusion
This assignment explored R’s object-oriented systems (S3 and S4) using the PlantGrowth dataset, which contains 30 observations of plant weights under three treatment groups (ctrl, trt1, trt2). The dataset loaded, summarized, and plotted correctly, showing clear weight differences between groups.
The S3 object (PlantExperiment) was created easily from a list and identified as an S3 object, highlighting its simplicity and flexibility. In contrast, the S4 object (PlantExperimentS4) required formal class and slot definitions, ensuring data consistency and structure.
Overall, the outputs showed that S3 is quick and informal—great for small, simple tasks—while S4 is formal and reliable, better suited for larger, complex data modeling.
Comments
Post a Comment