R Tutorial: Object-Oriented Programming in R: S3 & R6 (part 2)

Richard Cotton explains how S3 object systems in R enable function overloading through generic functions and class-specific methods. Method names must follow the pattern generic.class, like print.date or summary.factor, and always include an ellipsis argument. He recommends avoiding dots in function names and using lowersnakecase or lowerCamelCase instead.
Chapters
In chapter one, you saw how the summary function behaves differently for different types of input. For example, if you pass it a logical vector, it returns the number of occurrences of trues, falses and nas in the input. By contrast, if you pass a numeric vector, you get the mean and some quartiles having different behaviors for functions under different different kinds of input is called function overloading. Its main purpose is to simplify your code. If there was no such thing as function overloading, then there will be a lot more functions to learn. The S3 system exists entirely to make this problem easier. It does this by splitting a function into two a generic function and method functions for each class. Summary is an example of a generic, and another widely used generic function is print. The print function controls how variables are displayed in the console. Let's take a look at the code for the generic print function. As you can see, print is really simple. It's only one line long. This is typical for an S3 generic. All the function needs to do is call use method with its own name, I.e. print is passed to usemethod as a string. There are two conditions that you must follow for S3 methods. Firstly, the name of each method must be the name of the generic, then a dot, then the name of the the class of the variable. For example, the print method for date objects is called print date. Likewise, the summary method for factor objects is called summary factor, and the unique method for arrays is called unique array. Secondly, the arguments to the method must include all the arguments to the generic. For example, the argument the arguments to print are x and the ellipsis or argument. The arguments to print date are the arguments to the generic with an extra max argument. The ellipsis argument allows other arguments to be passed from one method to another. A full understanding of how it works is beyond the scope of the course. For now, you just need to know that it's good practice to always include an ellipsis argument in both the generic and the methods. All the methods corresponding to a generic are completely independent. For example, here's the code to print function. You don't need to worry about what it does. And here's the code for print date. As you can see, it's completely unrelated. Because S3 requires a dot to separate the name of the generic and the class of the input, it's usually a bad idea to include other dots in the names of your variable. Variable names separated by dots are sometimes called leopard case. Don't use this naming convention. Better conventions are lower snake case which uses lowercase words separated by underscores or lower camel case, where the first word is lowercase and subsequent words start with a capital letter. To summarise, S3 classes let you overload functions by splitting them into generics and methods. S s 3 works by defining a strict naming convention of generic class. The arguments to each method must contain the arguments to the generic, and both generic and method should include a argument. It's usually better to avoid having a dot in the name of your functions. Lower snake case or lowercamel case are preferable. Now let's try some examples.
More from Richie

#359 My Best Friend is AI with Valerie Tiberius, Professor of Philosophy at University of Minnesota

How AI Agents Will Work While You Sleep | Ruslan Salakhutdinov, Professor at Carnegie Mellon

#358 How AI Agents Will Work While You Sleep | Ruslan Salakhutdinov, Professor at Carnegie Mellon







