25 OO field guide

25.1 S3

  1. Q: Read the source code for t() and t.test() and confirm that t.test() is an S3 generic and not an S3 method. What happens if you create an object with class test and call t() with it?
    A: We can see that t.test() is a generic, because it calls UseMethod()

    If we create an object with class test, t will cause R to call t.test.default(), unless you create a method t.test() for the generic t().

  2. Q: What classes have a method for the Math group generic in base R? Read the source code. How do the methods work?
    A:

  3. Q: R has two classes for representing date time data, POSIXct and POSIXlt, which both inherit from POSIXt. Which generics have different behaviours for the two classes? Which generics share the same behaviour?
    A: Since both inherit from “POSIXt”, these should be the same for both classes:

    And these should be different (or only existing for one of the classes):

  4. Q: Which base generic has the greatest number of defined methods?
    A:

  5. Q: UseMethod() calls methods in a special way. Predict what the following code will return, then run it and read the help for UseMethod() to figure out what’s going on. Write down the rules in the simplest form possible.

    A: g(10) will return 2. Since only x is in the execution environment of g.numeric R will search for y in the enclosing environment, where y is defined as 2. h("a") will return "class a", because x = "a" is given as input to the called method. From ?UseMethod:

    UseMethod creates a new function call with arguments matched as they came in to the generic. Any local variables defined before the call to UseMethod are retained (unlike S).

    So generics look at the class of their first argument (default) for method dispatch. Then a call to the particular method is made. Since the methods are created by the generic, R will look in the generics environment (including all objects defined before (!) the UseMethod statement) when an object is not found in the environment of the called method.

    h("a") will return "char a", because x = "a" is given as input to the called method, which is of class character and so h.character is called and R also doesn’t need to look elsewhere for x.

  6. Q: Internal generics don’t dispatch on the implicit class of base types. Carefully read ?"internal generic" to determine why the length of f and g is different in the example below. What function helps distinguish between the behaviour of f and g?

    A: From ?"internal generic":

    Many R objects have a class attribute, a character vector giving the names of the classes from which the object inherits. If the object does not have a class attribute, it has an implicit class, “matrix”, “array” or the result of mode(x) (except that integer vectors have implicit class “integer”). (Functions oldClass and oldClass<- get and set the attribute, which can also be done directly.)

    In the first case, the internal generic length does not find the class of f (“function”), so the method length.function is not called. This is because f doesn’t have a class - which is needed for the S3 method dispatch of internal generics (those that are implemented in C, you can check if they are generics with pryr::ftype) - only an implicit class. It is very confusing, because class(f) returns this (implicit) class.
    You can check if a class is only implicit by using one of the following approaches:
    • is.object(f) returns FALSE
    • oldClass(f) returns NULL
    • attributes(f) doesn’t contain a $class field

25.2 S4

  1. Q: Which S4 generic has the most methods defined for it? Which S4 class has the most methods associated with it?
    A:

    Generics:

    We restrict our search to those packages that everyone should have installed:

    Then we start our search for generics and keep those of otype S4:

    Finally we calculate the S4-generic with the most methods:

    Classes:

    We collect all S4 classes within a character vector:

    Then we are going to steal the following function from S4 system development in Bioconductor that returns all methods to a given class

    Finally we apply this function to get the methods of each class and format a little bit to answer the question:

  2. Q: What happens if you define a new S4 class that doesn’t “contain” an existing class? (Hint: read about virtual classes in ?Classes.)
    A: Since ?Classes is deprecated we refer to ?setClass:

    Calls to setClass() will also create a virtual class, either when only the Class argument is supplied (no slots or superclasses) or when the contains= argument includes the special class name “VIRTUAL”.

    In the latter case, a virtual class may include slots to provide some common behavior without fully defining the object—see the class traceable for an example. Note that “VIRTUAL” does not carry over to subclasses; a class that contains a virtual class is not itself automatically virtual.

  3. Q: What happens if you pass an S4 object to an S3 generic? What happens if you pass an S3 object to an S4 generic? (Hint: read ?setOldClass for the second case.)
    A:

25.3 RC

  1. Q: Use a field function to prevent the account balance from being directly manipulated. (Hint: create a “hidden” .balance field, and read the help for the fields argument in setRefClass().)
    A: We are not that experienced in general RC classes, but it is easy with R6 classes. You can find all the information you need here. To solve the exercise this introduction should be sufficient:

  2. Q: I claimed that there aren’t any RC classes in base R, but that was a bit of a simplification. Use getClasses() and find which classes extend() from envRefClass. What are the classes used for? (Hint: recall how to look up the documentation for a class.)
    A: We get these classes as described in the exercise:

    Their need is best described in class?envRefClass “Purpose of the Class”:

    This class implements basic reference-style semantics for R objects. Objects normally do not come directly from this class, but from subclasses defined by a call to setRefClass. The documentation below is technical background describing the implementation, but applications should use the interface documented under setRefClass, in particular the $ operator and field accessor functions as described there.