Exploring Object-Oriented Programming in R:

  

 Hello all 👋

In this blog post, we will explore the world of Object-Oriented Programming (OOP) in R. Specifically, we will assign S3 and S4 classes to the ToothGrowth dataset, a well-known dataset in R. Through this exploration, we aim to understand how these OOP systems work and how they can be applied to datasets. Additionally, we will discuss the implications of using these systems for data analysis workflows.

The Effect of Vitamin C on Tooth Growth in Guinea Pigs

Description

The response is the length of odontoblasts (cells responsible for tooth growth) in 60 guinea pigs. Each animal received one of three dose levels of vitamin C (0.5, 1, and 2 mg/day) by one of two delivery methods, orange juice or ascorbic acid (a form of vitamin C and coded as VC).

Format

A data frame with 60 observations on 3 variables.

[,1]lennumericTooth length
[,2]suppfactorSupplement type (VC or OJ).
[,3]dosenumericDose in milligrams/day

Source

C. I. Bliss (1952). The Statistics of Bioassay. Academic Press.

Please explore the URL to run the datasets and explore the S3 and S4 | S3 Vs S4 |

Conclusion:

The generic function can indeed be assigned to the ToothGrowth dataset. This dataset is a standard data frame in R, and generic functions can be applied to it to perform various operations.

Both S3 and S4 classes can be assigned to the ToothGrowth dataset. This means that the dataset can be treated as objects with formal or informal class definitions, allowing for object-oriented programming paradigms to be applied to them.


  1. Determining OO System: In R, which Object Oriented system (S3 vs. S4)  an object is associated with, can be determined by using the following functions ‘is’ and ‘methods’.

    methods(class=class(x))

    It will result in methods linked with the object’s class. If the object is an S3 class, it will result in names of the function without class. But, if it S4 class it will result in function names defined with functions set Method’ or set Generic’.

    In addition, function isS4(x) can be used to determine if the object is S4 or not. If it results in TRUE, then the object is S4 class or if it results in FALSE, then the object is S3 class or other system.


  2. Determining Base Type: The base type of an object can be determined by using the following function ‘class’which when implemented will result in its base type like an integer or list.

    class(x)

    If the resultant is ‘list’, then the object is a list. If the resultant is an ‘integer’, then the object is an integer. Sometimes, the object can have more than one class, in such a case, it will result in a vector of character strings.

    s <- list(name = "Myself", age = 29, GPA = 3.5)
    class(s)
    [1] "list"


  3. Generic Function: A generic function is a kind of function used in object-oriented programming that performs differently based on the class or type of the object to which it is utilized. This can use different types of objects, as opposed to being limited to a particular data type, which is why it is named "generic." Methods in S3 and S4 object systems are part of generic functions rather than classes. In fact, in S3, the ‘print’ function is generic, and ‘print. default’ is a method of the generic function.

    With the help of the ‘setGeneric’ function in S4, it's indeed feasible to develop functions that will have various interpretations based on the class of the object to which they are implemented. The ‘setMethod’ function can be utilized to create alternatives for the generic function.


  4. Main Differences between S3 and S4: S3 is simpler and more informal, relying on naming conventions, while S4 is more formal and complex, allowing for strict definitions of classes and methods.


References:

My Instructor: Alon Friedman, PhD --- Associate Professor, School of Information

Matloff. N. The Art of R Programming (Object Oriented Programming). Page number: 207-226  

Comments

Popular posts from this blog

Mastering Debugging: A Step-by-Step Guide to Fixing Code Errors

My intro to R programming

A Visual Analysis of Patient Data : Blood Pressure and Medical Assessments