Creating a generic function and defining a method for the S3 class
A generic function is an encapsulation of any generic concept such as printing, calculating summary information of any R objects, creating graphs, and so on. The generic function does not perform any calculation, rather it just dispatches methods based on the class of the objects. In this recipe, you will create your own generic function and then define the method for the class robustStatistics
.
Getting ready
Suppose you want to create a generic function alternative to summary()
in R. The name of the function is robSum()
. The default action will be just to check the class and produce a message whether the input object has a certain class or not. In the second stage, you will define a method for the new generic function.
How to do it…
Let's take a look at the following steps:
- The new generic function can be created as follows:
robSum <- function(obj) { UseMethod("robSum") }
- A default method for
robSum...