Sunday, February 28, 2021

Mod 7: OOP

Mod-7.utf8


Using Orange dataset from package ‘datasets’

Let’s load the dataset.

data("Orange")


Generic functions

Generic functions are functions that work with polymorphism which means that the same function call leads to different operations for objects of different classes.

When a generic function is called, R will dispatch the call to the proper class method, meaning that it will reroute the call to a function defined for the object’s class.

Let’s consider two EDA functions on the Orange dataset: summary and plot.

summary(Orange)
##  Tree       age         circumference  
##  3:7   Min.   : 118.0   Min.   : 30.0  
##  1:7   1st Qu.: 484.0   1st Qu.: 65.5  
##  5:7   Median :1004.0   Median :115.0  
##  2:7   Mean   : 922.1   Mean   :115.9  
##  4:7   3rd Qu.:1372.0   3rd Qu.:161.5  
##        Max.   :1582.0   Max.   :214.0
plot(Orange)


The functions summary and plot are generic S3 functions.

  • a call to ?summary and ?plot will have the R Documentation tell us: ...generic function...
  • a call to summary and plot will show the function UseMethod which is used to declare a generic S3 method
  • a call to isS4(summary) and isS4(plot) will return FALSE
summary
## function (object, ...) 
## UseMethod("summary")
## <bytecode: 0x000000001585b0f8>
## <environment: namespace:base>
isS4(plot)
## [1] FALSE


S3 vs S4

S3, is still the dominant OOP class paradigm in R use today. S4 classes were developed later, with goal of adding safety, meaning that you cannot accidentally access a class component that is not already in existence.

The best summary is probably: convenience of S3 vs the safety of S4.

A quick S3 vs S4 comparison table.

table of S3 vs S4


Sample S3 and S4

Let’s model a car for efficiency (mpg), engine size (cyl), and transmission (gear).

A S3 object (Mazda RX4) is instantiated via a list.

s3 <- list(name = "Mazda RX4", mpg = 21, cyl = 6, gear = 4)
class(s3)
## [1] "list"
s3
## $name
## [1] "Mazda RX4"
## 
## $mpg
## [1] 21
## 
## $cyl
## [1] 6
## 
## $gear
## [1] 4


A S4 object (Mazda RX4) first requires a class definition and is then instantiated via the new() method

setClass(
    "car",representation(
        mpg = "numeric", cyl = "numeric", gear = "numeric"
    )
)
s4 <- new("car", mpg = 21, cyl = 6, gear = 4)
class(s4)
## [1] "car"
## attr(,"package")
## [1] ".GlobalEnv"
s4
## An object of class "car"
## Slot "mpg":
## [1] 21
## 
## Slot "cyl":
## [1] 6
## 
## Slot "gear":
## [1] 4




GitHub

Related file(s) can be found at Git Me

No comments:

Post a Comment