0
0
R Programmingprogramming~10 mins

S4 object system in R Programming - Step-by-Step Execution

Choose your learning style9 modes available
Concept Flow - S4 object system
Define Class with setClass()
Create Object with new()
Access Slots with @
Use Methods with setMethod()
Call Method on Object
Output Result
This flow shows how you define a class, create an object, access its parts, define methods, and then use those methods on the object.
Execution Sample
R Programming
setClass("Person", slots = list(name = "character", age = "numeric"))
p <- new("Person", name = "Alice", age = 30)
p@name
setGeneric("greet", function(object) standardGeneric("greet"))
setMethod("greet", "Person", function(object) {
  paste("Hello, my name is", object@name)
})
greet(p)
Defines a Person class, creates an object, accesses a slot, defines a greet method, and calls it.
Execution Table
StepActionEvaluationResult
1Define class Person with slots name and agesetClass(...) calledClass Person created with slots name (character), age (numeric)
2Create object p of class Personnew("Person", name="Alice", age=30)Object p created with name='Alice', age=30
3Access slot name of pp@name"Alice"
4Define generic greet and method greet for class PersonsetGeneric("greet", ...) and setMethod("greet", "Person", ...)Generic greet created and method greet defined for Person
5Call greet(p)greet(p)"Hello, my name is Alice"
6End of execution-No more steps
💡 All steps executed; method greet called successfully on object p
Variable Tracker
VariableStartAfter Step 2After Step 5Final
pundefinedPerson object(name='Alice', age=30)Person object(name='Alice', age=30)Person object(name='Alice', age=30)
Key Moments - 3 Insights
Why do we use @ instead of $ to access slots in S4 objects?
In the execution_table step 3, p@name accesses the slot 'name'. S4 objects use @ to access slots because slots are formal parts of the object defined by the class, unlike list elements accessed by $.
What happens if you try to create an object with a wrong slot type?
If you try to create an object with a slot value of the wrong type, R will give an error at step 2 because S4 enforces slot types strictly.
How does setMethod link a function to a class?
At step 4, setMethod defines a function 'greet' specifically for objects of class 'Person'. This means calling greet on a Person object uses this method.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table at step 3, what is the value of p@name?
A"Bob"
B"Alice"
C30
DNULL
💡 Hint
Check the 'Evaluation' and 'Result' columns at step 3 in the execution_table.
At which step is the greet method defined for the Person class?
AStep 4
BStep 3
CStep 2
DStep 5
💡 Hint
Look for setMethod call in the execution_table.
If you change the slot 'age' to a character instead of numeric when creating p, what happens?
AObject p is created successfully
BThe greet method fails at call time
CAn error occurs at object creation
DThe slot 'age' is automatically converted to numeric
💡 Hint
Refer to key_moments about slot type enforcement during object creation.
Concept Snapshot
S4 Object System in R:
- Define class with setClass("ClassName", slots = list(...))
- Create object with new("ClassName", ...)
- Access slots with @ (e.g., obj@slot)
- Define generic functions with setGeneric("genericName", ...)
- Define methods with setMethod("genericName", "ClassName", function(object) {...})
- Call methods like greet(obj)
- S4 enforces slot types strictly
Full Transcript
The S4 object system in R lets you create formal classes with defined slots. First, you define a class using setClass, specifying slot names and types. Then, you create an object of that class with new, providing values for each slot. You access slot values using the @ symbol. You define generic functions with setGeneric, then define methods for your class using setMethod, linking a function to a class and a generic name. Finally, you call the method on your object, and it runs the class-specific code. The system checks slot types strictly, so you must provide correct types when creating objects.