Generic functions, methods, and classes
R has different class systems, the most important ones are S3
and S4
classes. Programming with S3
classes is easy living, it's easier than S4
. However, S4
is clean and the use of S4
can make packages very user-friendly.
In any case, in R each object is assigned to a class (the attribute class
). Classes allow object-oriented programming and overloading of generic functions. Generic functions produce different output for objects of different classes as soon as methods are written for such classes.
This sounds complex, but with the following example it should get clearer.
As an example of a generic function, we will use the function summary
. summary
is a generic function used to produce result summaries. The function invokes particular methods that depend on the class of the first argument:
## how often summary is overloaded with methods ## on summary for certain classes (the number depends on loaded packages) length(methods(summary)) ## [1] 137 class...