Introducing three object models in R – S3, S4, and R6
Now that you have a basic understanding of general object-oriented concepts, we will dig into R's own object models. There are two main sources of confusion when doing object-oriented programming in R. Before we start developing code, we will explain what these sources of confusion are. After we do, we will develop a small example to illustrate inheritance, composition, polymorphism, and encapsulation in R's S3, S4, and R6 object models. The same example will be used for all three models so that the reader can pinpoint precise differences. Specifically, we will model a Square
inheriting from a Rectangle
, which is in turn composed with a Color
.
The first source of confusion – various object models
The way you work with object-oriented programming in R is different from what you may see in other languages, such as Python, Java, C++, and many others. For the most part, these languages have a single object model that all people use. In the...