The S3 Object System in R
Jim Duggan · 2024
S3 is an object-oriented system in R. It is widely used in base R, and also in other packages. It is built on the idea of a generic functions, which provides a clever mechanism to hide specific implementation details from the user when functions are called. Classic examples of generic functions include summary(), print() and plot(). The chapter introduces the idea of attributes, which are metadata that can be associated with any variable. It explains how attributes are used by the S3 system to implement the generic function approach, using a mechanism known as method dispatch. The approach for writing custom generic functions is shown, and more advanced features such as inheritance are presented. A mini-case showing how to implement a simple queue system concludes the chapter.