R’s S3 generic-function object-oriented system

[This article was first published on Jasmine Dumas' R Blog, and kindly contributed to R-bloggers]. (You can report issue about the content on this page here)
Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.

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 about learning R and many other topics. Click here if you're looking to post or find an R/data-science job.
Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.

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)