S3 and S4 classesBased on years of experience in Bioconductor, it is fair to say that S4 classes have been very successful in this project. S4 classes has allowed us to construct rich and complicated data representations that nevertheless seems simple to the end user. An example, which we will return to, are the data containers ExpressionSet and SummarizedExperiment.
Let us look at a S3 object, the output of the linear model function lm in base R:
- df <- data.frame(y = rnorm(10), x = rnorm(10))
- lm.object <- lm(y ~ x, data = df)
- lm.object
复制代码- ##
- ## Call:
- ## lm(formula = y ~ x, data = df)
- ##
- ## Coefficients:
- ## (Intercept) x
- ## 0.3147 -0.7131
复制代码- ## [1] "coefficients" "residuals" "effects" "rank"
- ## [5] "fitted.values" "assign" "qr" "df.residual"
- ## [9] "xlevels" "call" "terms" "model"
复制代码
In standard R, an S3 object is essentially a list with a class attribute on it. The problem with S3 is that we can assign any class to any list, which is nonsense. Let us try an example
- xx <- list(a = letters[1:3], b = rnorm(3))
- xx
复制代码- ## $a
- ## [1] "a" "b" "c"
- ##
- ## $b
- ## [1] 0.3380950 0.8861906 -1.1216766
复制代码- ##
- ## Call:
- ## NULL
- ##
- ## No coefficients
复制代码
At least we don’t get an error when we print it.
S4 classes have a formal definition and formal validity checking. To the end user, this gurantees validity of the object.