Get startedGet started for free

Too Much Class

1. Too Much Class

Variables can have more than one class. In this case, rather than the class being a single string, it is a character vector. Let's take a look at an example.

2. class()

Consider this vector. It's a numeric vector, but you can also be more specific in describing it. It's also a vector of natural numbers; that is, positive whole numbers. Even more specifically, it's a vector of triangular numbers. That means that you could describe it using three or more classes. The order of the classes is important. You should always have the most specific class first, and gradually get less specific as you move from left to right. It is good practise to keep the original class – in this case "numeric" – as the final class. To test for numeric vectors, as I'm sure you know by now,

3. is.numeric()

you would use is-dot-numeric. Since you've just invented the triangular_numbers class, base-R doesn't have an equivalent is-dot-triangular_numbers function. To test for arbitrary classes, you can use

4. inherits

the general purpose inherits function. As you may imagine, x inherits from triangular_numbers, and from natural_numbers, and from numeric. In this last case, testing that x inherits from numeric will return the same thing as calling is-dot-numeric, but the more gernal function is slower. For this reason you should use the more specific function if it is available. If your object has multiple classes, you can call multiple S3 methods by using the NextMethod function.

5. what_am_i

To demonstrate this, take a look at this S3 generic, called what_am_i.

6. what_am_i.triangular_numbers

Now you can define a method for what_am_i that acts on triangular numbers. This just prints a message that explains what it is, and moves on the next method. That is, R will look at the second class of x, in this case natural numbers, and call that method. Lets define the natural numbers method in the same way. Now the methods are being chained together, so R will next look at the third class of x, which is numeric. Finally you can add a method for numeric vectors. Since this is the last class, you can't call NextMethod here. Now lets see it in action.

7. what_am_i()

When you call what_am_i on x, you can see that three messages have been printed. The generic function was called first, then UseMethod found

8. what_am_i()

the triangular numbers method. That printed the first message, then NextMethod looked at the second class and

9. what_am_i()

found the natural numbers method. That printed the second message, and this time NextMethod looked at the third class and

10. what_am_i()

found the numeric method. That printed the third message.

11. Summary

To summarise, objects can have more than one class. To test for arbitrary classes, you can use the inherits function. When you have multiple classes, you can chain methods together using NextMethod.

12. Let's practice!