Exploring Object-Oriented Systems in R: S3 vs. S4

R is a powerhouse for data analysis, and its Object-Oriented (OO) systems—S3 and S4—offer flexible ways to structure data. Through this adventure of debugging, testing, and optimizing functions, I have explored both systems hands-on. Here’s a breakdown of what I learned!


How Do We Determine If a Generic Function Can Be Used?

A dataset supports a generic function if: ✔️ It contains multiple data types (e.g., numeric, character, factor). ✔️ I need different methods for different types (e.g., numeric summaries vs. categorical summaries). ✔️ The function can dispatch methods dynamically.

Testing These Conditions

NOTE: 📂 Check out my full code on GitHub!

# Check data structure

str(df)

# Verify multiple data types

sapply(df, class)

# Ensure dataset supports generic methods

summarize_fire_data(df$Cause)  #  Works on categorical data

summarize_fire_data(df$Fatalities)  #  Works on numeric data

Since methods are dispatched dynamically, I confirm that a generic function is applicable. I did go overboard with the methods. See the code in Github. 😊 Here are the results:


How Do We Determine If Our Dataset Supports S3 and S4?

I conducted multiple tests to determine if our dataset could be structured as S3 or S4.

Check If the Dataset is S3-Compatible (Lists and Attributes) is.list(df) 

Check If S4 is Supported (Explicit Class Definition Required)

isS4(df) 

Since the dataset is not already an S4 object, I converted it using setClass().

Use otype() from pryr to Confirm Object Types

library(pryr)

otype(df)  # Returns “S3” since data.frame is S3

fireS3 <- wildfire_s3(df, 3)

otype(fireS3) 

fireS4 <- wildfire_s4(df, 3)

otype(fireS4) 

Final Validation Using isS4() and class()

More 💡R Code: Github

These checks confirm that our dataset supports both S3 and S4!


S3 vs. S4: Examples for GitHub

S3 Example

# Define S3 constructor # Create an S3 object
💡 S3 R Code Solution: 📂 GitHub

S4 Example

# Define S4 class

# Define S4 constructor

# Create an S4 object

💡 S4 R Code Solution: 📂 GitHub

Final Thoughts: S3 vs. S4 Differences

FeatureS3 (The Laid-Back System 😎)S4 (The Strict Professor 👨‍🏫)
FlexibilityVery flexible, no formal structureStrict rules, requires class definitions
ValidationNo automatic checksEnforces data type validation
Method DispatchUses UseMethod()Uses setGeneric() and setMethod()
DefinitionUses simple listsUses setClass() and slots
Ease of UseQuick and easy to modifyRequires more setup but ensures structure
Best ForFast prototyping and simple modelsComplex applications that need data integrity

I have determined that ✔️ Generic functions can be applied because our dataset has numeric and categorical data types. ✔️ S3 and S4 can be assigned because our dataset represents structured objects (wildfire incidents).

This project was a great deep dive into R’s object-oriented systems!

NOTE: 📂 Check out my full code on GitHub!

References:

Matloff, N. (2011). The art of R programming: A tour of statistical software design. No Starch Press. Chapter 9.

Leisch, F. (2014). S4 classes and methods. R Development Core Team. Retrieved from usflearn.instructure.com/courses/

Chambers, J. M. (1998). Programming with data: A guide to the S language. Springer.

Wickham, H. (2015). Advanced R. Chapman and Hall/CRC. Retrieved from http://adv-r.had.co.nz/

Leave a comment