Object-oriented programming using R

As you already know, R is primarily a functional language; it also supports OOP. OOP in R is an archetype wherein objects and their interactions are used to design various generic functions. It defines the process of constructing modular bits of code, which can be integrated to form a large function. Some key concepts related to OOP are as follows:

  • Object: An instance of a class or an output of a function in R
  • Class: Used to define type and attributes of objects in R
  • Method: An implementation of a generic function for an object of a particular class
  • Generic function: A generalized function which calls multiple methods without performing any computation itself

R supports three forms of OOP systems based on different objects, classes, and methods:

  • S3: An informal, simple, interactive, and widely used OOP system in R. Basic packages such as base and stats are primarily built using the S3 system. The following are some generic functions built for multiple objects such as dataframes, vectors, or the output of lm() function for its corresponding method:

    Object-oriented programming using R

    Figure 3.8: Generic S3 functions for different methods

  • S4: Unlike S3, S4 is much more formal, robust, and provides a uniform mode to create objects. Also, the generic function can be dispatched multiple times to pick methods based on the class of any number of arguments. In S4, new objects are created using the new() function, and class components are defined using the setClass() function. A class has three main properties:
    • a name: This is an alphanumeric string used to identify the class.
    • a representation: This is used to define a list of attributes (or slots) along with their data types. For example, an employee class of shop will be represented by a name represented as character, age as numeric, and gender represented as character as shown below:
                        representation(name="character", age="numeric",                             gender="character")
    • contains or character vector: a vector of classes used for multiple inheritance. Caution should be taken while using contains in S4 as it makes method lookup intricate.
  • R5 (Reference classes): Unlike S3 and S4 which implement generic functions, R5 implements message passing object-oriented programs similar to other object-oriented programs such as Java, C++, and C#, where methods belong to classes rather than functions. R5 objects are also mutable, as they are not dependent on R's modify semantics.

The following table compares the S3, S4, and R5 systems:

Object-oriented programming using R

Figure 3.9: A comparison between S3, S4 and R5 OOs

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset