Generic Functions

Polymorphism in R can be obtained by the generics. It allows certain names to be treated as methods and they act as dispatchers. Let’s understand with the help of plot() function and summary function. In R programming the plot() and summary() functions return different results depending on the objects being passed to them and that’s why they are generic functions that implement polymorphism. 

Polymorphism in R Programming

R language is evolving and it implements parametric polymorphism, which means that methods in R refer to functions, not classes. Parametric polymorphism primarily lets you define a generic method or function for types of objects you haven’t yet defined and may never do. This means that one can use the same name for several functions with different sets of arguments and from various classes. R’s method call mechanism is generics which allows registering certain names to be treated as methods in R, and they act as dispatchers. 

Similar Reads

Generic Functions

Polymorphism in R can be obtained by the generics. It allows certain names to be treated as methods and they act as dispatchers. Let’s understand with the help of plot() function and summary function. In R programming the plot() and summary() functions return different results depending on the objects being passed to them and that’s why they are generic functions that implement polymorphism....

plot() in R

plot() is one of the generic functions that implement polymorphism in R. It produces a different graph if it is given a vector, a factor, a data frame, etc. But have you ever wondered how does the class of vectors or factors determine the method used for plotting?  Let’s see the code for the plot function....

summary() in R

...

Creating Generic Method

...

Contact Us