$30 off During Our Annual Pro Sale. View Details »

OOP in R: Understanding S3 and S4 systems

Avatar for Manisha Barse Manisha Barse
October 10, 2025
7

OOP in R: Understanding S3 and S4 systems

LIBD Rstats Club: Exploring SpaceRanger summary metrics
Presented By : Manisha Barse
Date: October 10, 2025

GitHub Link: https://github.com/manishabarse/LIBD_presentation/tree/main/OOP_in_R

This presentation introduces object-oriented programming (OOP) in R, focusing on the S3 and S4 systems: the foundations behind most R and Bioconductor packages. We explore what objects, classes, and methods are, how S3 and S4 differ in structure, dispatch, and validation, and why both remain relevant. Real-world examples using SummarizedExperiment (airway dataset) demonstrate how S4 formal classes work in bioinformatics.

#RStats #OOP #S3 #S4 #Bioinformatics #SummarizedExperiment #Rprogramming

Avatar for Manisha Barse

Manisha Barse

October 10, 2025
Tweet

Transcript

  1. Outline Object Oriented Programming Story behind S, S3, S4, R

    Object types Features of S3 and S4 R demo
  2. Object-Oriented Programming Programming technique that manages - complexity of a

    program more efficiently: modular approach Goal: code organization, reusability, and maintainability Principles https://blog.rheinwerk-computing.com/the-principles-of-object-oriented-programming https://codersite.dev/understanding-oop-concepts/
  3. OOP vs Functional Programming (FP) R supports both functional programming

    and OOP. Functional Programming: Focuses on functions transforming data. sum(c(1, 2, 3)) # function operates on data Object-Oriented Programming: Focuses on objects carrying data and behavior. print(lm(y ~ x)) #method behavior depends on object class FP : more common in the tidyverse, OOP : powers base R, statistical modeling, and large frameworks (like Bioconductor).
  4. The Story Behind S, S3, S4, and R • R

    is historically derived from S, a statistical language developed at Bell Labs (Chambers et al.). • R adopted these OOP systems from S, so the names S3 and S4 carry over • R also got reference classes (RC or “R5”), R6, and now S7, but S3 and S4 are foundational and still heavily used, especially in core and bioinformatics packages. System Era Features S1 & S2 ~1976, 1978 No OOP S3 ~1983 Informal, simple OOP S4 ~1995 Formal, strict OOP R6 ~2014 Reference-based OOP S7 ~2024 Next-gen OOP (hybrid)
  5. Base Types “Everything that exists in R is an object”

    - John Chambers While everything is an object, not everything is object-oriented. To know the difference, we can use: sloop package. • provide tools to help you interactively explore and understand object oriented programming in R. • difference between base and OO objects is that OO objects have a “class” attribute and have has a base type. • s3_dispatch(): set of methods that are considered, found, and actually called.
  6. S3 System in R • S3 is the simplest and

    most widely used object-oriented system in R. • Found throughout base R (lm, data.frame, summary , etc.) • It is informal, no explicit class definitions. • Focuses on generic functions and method dispatch (process of picking the right method) based on the object’s class
  7. Constructors and Dispatch in S3 • Constructor: function that builds

    and tags an object with a class. • Good constructors: ◦ Validate inputs ◦ Set class properly ◦ Return structured object • Dispatch: R decides which function (method) to call based on the class of the object. • Implemented with UseMethod() inside a generic function. • R looks for generic.class() functions in this order: ◦ generic.class ◦ generic.superclass ◦ generic.default
  8. S3 Best Practices • Avoid . in function/class names conflicts

    with base R method naming • Use snake_case or camelCase (print_person() / printPerson()) • Provide constructor, validator, and helper functions • Use NextMethod() to delegate in subclassing https://adv-r.hadley.nz/s3.html#s3
  9. S4 system in R • S3 is flexible but unstructured

    → hard to maintain large codebases • S4 provides: ◦ Explicit class definitions ◦ Typed slots (fields) ◦ Validity checks ◦ Formal generic/method dispatch ◦ Ideal for complex packages (Bioconductor, bioinformatics)
  10. S4 Features • setClass() → define class and slots •

    new() → create object • setGeneric() + setMethod() → define methods • Slots: strongly typed, optional validity function • Supports multiple inheritance
  11. S3 vs S4 Feature S3 S4 Class class attribute, naming

    convention Explicit with slots Methods generic.class setGeneric() + setMethod() Accessing data list-style $ slots @ Dispatch UseMethod() Formal: setGeneric()/setMethod() Use Base R (lm, data.frame) Bioconductor, SummarizedExperiment