S3-S4-R5 dictionary
S3 and S4 are different beasts. So are French and English, but we still have French-English dictionaries even though there’s always the possibility of getting lost in translation.
Below is a table with ‘translations’ that I’ve found can be useful. I intend to keep updating this as I come across other useful ‘translations’. Suggestions welcome. Being much stronger with S3, I hope S4 people share their constructive advice.
Consider an (either S3 or S4) object called x
(for simplicity I assume that if x
is S3 then its also a list). I use cls
, cls1
, cls2
, etc. to denote names of classes. I use gnrc
, gnrc1
, gnrc2
, etc. to denote names of generic functions.
EDIT: Now covering the R5
system as well. This system is based on reference classes.
S3 | S4 | R5 | Notes |
---|---|---|---|
‘elements’ | ‘slots’ | ‘fields’ | |
x$ |
x@ |
x$ |
|
as.cls(x) |
as(x, 'cls') |
||
class(x) |
class(x) |
x$getRefClass() |
|
names(x) |
slotNames(class(x)) , getSlots(class(x)) |
x$getRefClass()$fields() OR ls(x) |
|
gnrc.cls |
getMethod('gnrc', 'cls') |
||
methods('gnrc') |
showMethods('gnrc') |
x$getRefClass()$methods() |
Because S4 methods allow for dispatching on multiple arguments, there are some pretty important differences between S3 and S4 here: The S3 version shows the names of all methods; S4 version shows the acceptable classes for each argument of the method. |
methods(class = 'cls') |
showMethods(classes = 'cls') |
||
class(x) <- |
setClass |
setRefClass |
Because of the differences between the three systems, these are VERY rough translations |
just a reminder. How about the analogue of `methods(class=”lm”)` for example, or the corresponding `methods(“profile”)` ?
Thanks for the reminder. My best S4 translations for this are up now.
another couple of comments. (1) table formatting is messed up in my browser (Firefox on Ubuntu). (2) It would be *really* handy to show how to debug a class method (e.g. debug(method.class), trace(method,sig=”class”), etc.)
– In newer versions of R, `methods(.)` shows both S3 and S4 methods for a generic function, ..
– John Chambers (JMC), the creator of all of these really really did never like the term “R5”.
to the contrary, he has emphasized that both “S4” and reference classes or “formal” classes as opposed to the informal classes (they have no definition!) in S3.
Just “reference classes” I think is his term.