Quantcast
Channel: R-bloggers
Viewing all articles
Browse latest Browse all 12081

R’s S3 generic-function object-oriented system

$
0
0

(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)
CookiesMake criss-cross patternsmooth peanut butter
CookiesRoll in remaining confectioners’ sugarchopped almonds
CookiesDecorate with frostingground ginger

Further Reading

var vglnk = { key: '949efb41171ac6ec1bf7f206d57e90b8' }; (function(d, t) {var s = d.createElement(t); s.type = 'text/javascript'; s.async = true;s.src = '//cdn.viglink.com/api/vglnk.js';var r = d.getElementsByTagName(t)[0]; r.parentNode.insertBefore(s, r); }(document, 'script'));

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...


Viewing all articles
Browse latest Browse all 12081

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>