1. What is Object-Oriented Programming?
Hi, I'm Richie. I'm a data scientist at DataCamp and I'll be your instructor for this course. Most of the time when you work with R, you use a functional programming style.
2. f(x)
That is, you start with some data.
3. f(x)
To manipulate it, you apply a function.
4. f(x)
This returns some new data.
5. f(x)
Then you apply another function, and you repeat this until you get an answer.
6. function
With a functional mindset,
7. function
you typically start by thinking about what you want a function to do.
8. function
Next you worry about the objects that get passed into the function - that is, the arguments to that function.
9. function
Finally you worry about the objects that come out the other end - that is, the return values. Object-oriented programming has a different approach.
10. teapot
In it, you start by thinking about the objects that you have to work with - in this case a teapot.
11. teapot
Then you think about what data you need to describe the object. For a teapot, you might want to store the total capacity, and how much liquid is currently in the pot. Next you think about the functionality of that object.
12. teapot
The main purpose of a teapot is to pour tea, so you'd add a pour function.
13. A method is just a function
In object-oriented programming, functions are known as methods. There is no special "method" type of variable in R, so throughout the course, whenever you hear the word "method" it just means a function, but used in an object-oriented context.
14. object types
You’ve already come across many of the object types that are available in R. For example, you’ve seen numeric, logical and character vectors, as well as factors, data-dot-frames, and lists.
In total, there are about twenty types of objects available in R - the exact number depends on how you count them. These are all the basic building blocks that you need for data analysis. There are two variable types here that become important for object-oriented programming:
15. list
lists and environments. Because these variable types can contain any other variables, you can use them to create many other more complex types.
16. legos
Think of it like Legos. In this case you have twenty types of Lego block, but you can use those blocks to build an infinite number of more complex structures.
17. warning
Before we begin, a word of warning. A lot of the time, object-oriented programming isn’t necessary or even desirable for data analysis. Most of the time, you want your data to be stored in a data frame,
18. f(x)
and you should use functions that take a data frame, manipulate it, and return another data frame. That is, the functional programming approach.
19. tidyverse
This is the philosophy of the tidyverse packages, most notably dplyr. After you’ve finished this course, take one of our dplyr courses to see the alternate approach.
20. When is OOP a good idea?
So when should you use object-oriented programming? It works best when you have a limited number of objects that you completely understand the behavior of. This is true of industry-specific data analysis.
21. bioconductor
For example, the Bioconductor project has over 1000 R packages for analysing biological data, particularly genomics. Sections of a genome occur in many contexts here, so many Bioconductor packages make use of a GenomicRange object. By reusing this object throughout Bioconductor, the behaviour is predictable across many packages.
22. apis
Another example is accessing data through web application programming interfaces, or APIs. In this case, there is a limited number of responses that the website can provide, and you can define objects to store these responses.
23. shiny
A third example is graphical user interfaces, or GUIs. Many of the languages used to build GUIs, such as Java or C#, are object-oriented. For a GUI, again, there are a small number of well-understood objects that you need to consider, like buttons, or textboxes, or checkboxes. This screenshot is from a Shiny app, which means that it's a web GUI built in R. What you may notice about all three examples is that none of them really involved data analysis. Just to make it really clear,
24. oop
object-oriented programming is brilliant for building tools for data analysis, but bad for data analysis itself. In fact, there is no data analysis in this course at all. Instead the focus is on learning important coding skills.
25. Summary
To summarise, with functional programming, you think about the functions first, and then how you use them on your data. With object-oriented programming, by contrast, you think about the data structures first, then worry about their functionality. For general purpose data analyses, the functional style is more appropriate. An object-oriented style is preferable when you have a limited number of complex objects.
26. Let's practice!
Now, let's try some examples.