R’s S3 generic-function object-oriented system

February 22, 2018

(This article was first published on Jasmine Dumas' R Blog, and kindly contributed to R-bloggers)

Just like your mom’s chippity chipper recipe –

In Data Science, there are numerous instances where different techniques call for the use of different tools. For me, this means hopping between R and python on a weekly basis. I’ve been fortunate enough to have taken formal courses in python & R in the last few years and just by circumstances have chosen R as my primary language in my data science toolkit. This usually equates to a real mental struggle when jumping into a Jupyter Notebook and making trivial mistakes in the first 15 minutes, like below.

I don’t have any tricks yet, aside from a set of data analysis starter notebooks to quell the initial confusion, but I have noticed when I mentor other Data Scientists (those who have R skills and are now learning python or vice versa) and participate in pair-programming, I often drift to explaining the differences between R’s generic-function OO and python (and a many other languages) message-passing OO as a means of remembering how to structure functions. I’m only knowledgeable about explaining this differences from printing out and bookmarking Hadley Wickham’s OO Field Guide chapter in the Advanced R book! Don’t be worried by the title of being too ‘Advanced’, if you are interested in learning more about the why of certain things in R, it’s a great resource.

With the first few paragraphs of the field guide outlining some examples of the different systems, I wanted to dig in a bit further and learn more about the generic-function object-oriented system that I could use to explain to others and hopefully help me remember.

What is a generic function?

According to Wikipedia:

a generic function is a function defined for [polymorphism](https://en.wikipedia.org/wiki/Polymorphism_(computer_science) which is the provision of a single interface to entities of different types.

Wow, that’s a mouthful but essentially meaning that the function decides which method to call on different types of classes. Imagine the generic function as a recipe for cookies and the common steps include mixing, baking, and cooling and some different methods for specific types! ? Ultimately the cookies you make are dictated by what type of ingredients you include.

Generic Function (recipe) Method (finishing) Class (ingredient)
Cookies Make criss-cross pattern smooth peanut butter
Cookies Roll in remaining confectioners’ sugar chopped almonds
Cookies Decorate with frosting ground ginger

Further Reading

To leave a comment for the author, please follow the link and comment on their blog: Jasmine Dumas' R Blog.

R-bloggers.com offers daily e-mail updates about R news and tutorials on topics such as: Data science, Big Data, R jobs, visualization (ggplot2, Boxplots, maps, animation), programming (RStudio, Sweave, LaTeX, SQL, Eclipse, git, hadoop, Web Scraping) statistics (regression, PCA, time series, trading) and more...

If you got this far, why not subscribe for updates from the site? Choose your flavor: e-mail, twitter, RSS, or facebook...

Comments are closed.

Search R-bloggers


Never miss an update!
Subscribe to R-bloggers to receive
e-mails with the latest R posts.
(You will not see this message again.)

Click here to close (This popup will not appear again)