15 S4

15.1 Classes

  1. Q: What happens if you define a new S4 class that doesn’t “contain” an existing class? (Hint: read about virtual classes in ?setClass.)

    A: It depends on the other arguments. If we supply a class that doesn’t exist, we’ll get an error

    However, we can get around that, if we register the class before

    Supplying neither slots, nor contains results in a contructor for virtual classes

    Just leaving out contains, but supplying slots results in a constructor without superclass

  2. Q: Imagine you were going to reimplement ordered factors, dates, and data frames in S4. Sketch out the setClass() calls that you would use to define the classes. What should they inherit from? What slots should they use?

    A: The basic idea is to use a slot for the base type and one slot per attribute. Inheritance matters for ordered factors and dates. Special checks like equal lengths of list elements for columns of a data frame should be done within a validator.

15.2 Generics and methods

  1. Q: In the defintion of the generic, why is it necessary to repeat the name of the generic twice?

    A: The first time it is needed as name of the generic and the second time it is needed to explicitly incorporate method dispacth via standardGeneric() within the generic’s body (def parameter). This is similar to UseMethod() within S3.

  2. Q: What’s the difference between the generics generated by these two calls?

    A: The first call defines a standard generic and the second one creates a nonstandard generic. One can confirm this directly whlie printing (showing in S4 jargon) the function.

  3. Q: What happens if you define a method with different argument names to the generic?

    A: It depends. Lets first create the object hadley of class “Person”:

    Now let us see, which arguments can be supplied to the show() generic

    Usually we would use this argument when defining a new method

    When we supply another name, for example x instead of object, as a first element of our method, this becomes matched to the correct object argument and we get a warning. However, our method will work

    If we add more arguments to our method than our generic can handle, we will get an error

    However, if we do this with arguments added to the correctly written object argument, we will get the informative error message, that we could in general add other argument names for generics, which can take the ... argument

  4. Q: What other ways can you find help for a method? Read ?"?" and summarise the details.

    A: We can get

    • general documentation of the generic via ?genericName
    • general documentation of the methods from a generic via methods?genericName
    • documentation of a specific method via ClassName?methodName

    Regarding the latter, we can also get help on a specific method via adding a ? in front of a function call, like ?show(hadley).

15.3 Method dispatch

  1. Q: Take the last example which shows multiple dispatch over two classes that use multiple inheritance. What happens if you define a method for all terminal classes? Why does method dispatch not save us much work here?

    A: We will introduce ambiguity, since one class has distance 2 to all terminal nodes and the other 4 have distance 1 to two terminal nodes each. To resolve this ambiguity we have to define 5 more methods, one per class combination.