What's in a Name?
S3 uses a strict naming convention: all S3 methods have a name of the form generic.class
.
The converse is not true: a function can have a name containing a dot without being an S3 method. This is the case with many of the functions that have been around since the early days of the S language. For example, all.equal()
(docs) is actually an S3 generic, not a method. (This is an example of how leopard.case
can be confusing.)
You can check if a function is an S3 generic by calling is_s3_generic()
(docs) from the pryr
package. You can also print it (by typing its name in the console), then looking to see if it calls UseMethod()
.
Similarly, you can check if a function is an S3 method by calling is_s3_method()
(docs) from pryr
. For example,
library(pryr)
is_s3_generic("t") # generic transpose function
is_s3_method("t.data.frame") # transpose method for data.frames
is_s3_method("t.test") # a function for Student's t-tests
Which statements are true?
is.complex()
(docs) is a method of theis
generic that acts oncomplex
objects.seq.Date()
(docs) is a method of theseq
generic that acts onDate
objects.is.na.data.frame()
(docs) is a method of theis.na
generic that acts ondata.frame
objects.sort()
(docs) is a generic function.order()
(docs) is a generic function.
This exercise is part of the course
Object-Oriented Programming with S3 and R6 in R
Hands-on interactive exercise
Turn theory into action with one of our interactive exercises
