S4
S4 Introduction¶
- S4 is stricter than S3.
- It has a formal definition and a uniform way to create objects.
- Slots: properties of objects. Similar to attributes in JAVA
Define S4 class¶
Create S4 Object¶
The function setClass() returns a generator function, which can be used to create new objects.
Access and modify slots¶
List all generic functions¶
Customize S4 methods¶
setGeneric("print_info", function(object) standardGeneric("print_info"))
setMethod(
"print_info",
"student",
function(object) {
cat("Name:", object@name, "\n")
cat("Age:", object@age, "years old\n")
}
)