forked from ravichas/OOP-S3-in-R
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathR-OOP-S3-Hands-on.Rmd
402 lines (303 loc) · 9.1 KB
/
R-OOP-S3-Hands-on.Rmd
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
---
title: "Introduction to Object-Oriented Programming and S3 System in R"
author: "S.Ravichandran ( https://github.com/ravichas/OOP-S3-in-R )"
date: "April 27, 2019"
output: pdf_document
theme: flatly
---
<!-- based on Carl Broman's code -->
<style type="text/css">
ul,ol {
margin: 0 0 0 35px;
}
body, td {
font-size: 14px;
}
code.r{
font-size: 20px;
}
pre {
font-size: 20px
}
</style>
```{r setup, include=FALSE}
# set global chunk options: images will be 7x5 inches
knitr::opts_chunk$set(fig.width = 7, fig.height = 5)
options(digits = 4)
knitr::opts_chunk$set(echo = TRUE)
```
## Preliminary information about object types in R
Let us create a logical object, x.
```{r logicaltype}
(x <- TRUE) # logical
print(class(x))
```
Let us create a list, also called x.
```{r numericaltype}
(x <- list(nums = 1:10,
chars = c("one","two","three"),
ints = c(1L,2L,3L)
))
print(class(x))
```
BMI is a data.frame with four variables, Gender, Height, Weight and Age.
```{r dftype}
(BMI <- data.frame(
Gender = c("Male", "Male","Female"),
Height = c(153.1, 173.6, 165.0),
Weight = c(81,93, 78),
Age = c(42,38,26)
))
print(class(BMI))
```
## 1:Hands-on 1
```{r, libraries, echo = FALSE, warning = FALSE, message = FALSE}
# needed libraries
packages <- c("pryr", "sloop","magrittr","dplyr","ggplot2")
if ( length(missing_pkgs <- setdiff(packages, rownames(installed.packages()))) > 0) {
message("Installing missing package(s): ", paste(missing_pkgs, collapse = ", "))
install.packages(missing_pkgs)
}
library(magrittr)
library(dplyr)
library(ggplot2)
library(pryr)
library(sloop)
```
### 1.1 Functional programming explained using dplyr
In Functional programming, we accomplish tasks using functions. We usually chain the functions during this task.
Tidyverse is a good tool-kit for this task.
Let us use mtcars dataset (basic R dataset) and tidyverse (a package from the tidyverse collection) to explain
functional programming.
```{r, FunProg, eval = TRUE}
mtcars
mtcars %>% group_by(cyl) %>% summarize(mean_mpg = mean(mpg), mean_hp = mean(hp))
```
### 1.2 Function Overloading
One of the important concept of OOP is functions can
respond in different ways depending on the input object type.
To explain this concept, let us create the following objects:
* Numeric vector of 10 random numbers
* Categorical vector of length 6
* A linear model object
First, let us create a numerical vector with 10 elements.
```{r numeric-var, eval=TRUE }
set.seed(111)
(x_num <- rnorm(10) )
```
Next, we build a categorical vector with 6 elements.
```{r factor-var, eval=TRUE}
(x_fac <- factor(c("A", "B", "A", "C", "A", "B")))
```
Finally, let us create a linear model variable.
But, first let us create two variables x and y
```{r model-vars, eval=TRUE}
# setting seed
set.seed(123)
(x <- 1:10)
(y <- jitter(x, amount = 2))
```
```{r var-plot, eval=TRUE}
data.frame(x, y) %>% ggplot(aes(x, y)) +
geom_point() +
geom_smooth(method = "lm", col = "red", se = FALSE)
```
Build a model
```{r model-object, eval=TRUE}
model <- lm(y ~ x)
model
```
### Behavior of summary function on different class of objects
```{r summary-numeric}
x_num
summary(x_num)
```
```{r summary-factor}
x_fac
summary(x_fac)
```
```{r summary-model}
model
summary(model)
```
### 1.3 How does R distinguish types of variables?
what command(s) can be used for this task?
```{r}
# matrix
(int_mat <- matrix(1:12, nrow = 4, ncol = 3 )) # column major
# determine the variable
class(int_mat) # obj is a matrix
# what type of matrix (elements are of what type)
typeof(int_mat) # int matrix; content of the matrix
```
```{r}
(float_mat <- matrix(rnorm(12), nrow = 4, ncol = 3))
class(float_mat) # matrix
typeof(float_mat) # double; type of var that makes up matrix
# c code; in C floating point #s are double
```
## 2: Hands-on 2:
Interrogation of objects to see whether they are S3 objects
```{r}
(int_mat <- matrix(1:12, nrow = 4, ncol = 3 )) # column major
sloop::otype(int_mat) # package::command(object)
head(mtcars)
sloop::otype(mtcars)
```
### 2.1: S3 & R6: How to assign classes?
* Can I override the class?
* Yes
* And as expected, it wont break the functionality
* Can I woverride the type?
* No
```{r}
x_num
class(x_num)
typeof(x_num)
(class(x_num) <- "random-numbers")
# the class that we have added has become an attribute
x_num
# we cannot override typeof
typeof(x_num)
is.numeric(x_num) # no matter what the class says
```
### 2.2: S3 & R6: Function overloading
S3 exists so that we dont have to write many many functions to take
care of different data types.
How does it work?
* S3 splits a function into generic and method functions.
* Methods named generic.class (Ex. print.Date)
Example of generic functions are print, summary etc.
```{r}
(x_Date <- Sys.Date()) # "YYYY-MM-DD"
class(x_Date) # "Date"
print(x_Date) # "YYYY-MM-DD", 2019-03-26
# is same as calling print.Date
print.Date(x_Date)
# Let us explore the print function
print
```
print function is just a simple one line function.
You can ignore the last two lines that shows the memory
location and the object environment.
print function calls UseMethod("print") to provide the final output.
### 2.3: What methods exist for a generic function?
* For example, for the generic function what methods are available
* generic.class1, generic.class2, generic.class3
Exmaple. print (generic), print.data.frame, print.Date etc.
```{r What-methods-for-generic}
head(methods(print)) # too many methods
```
### 2.4: What methods are available for a given class of an object?
* The methods could be coming from different generic classes.
For example, generic1.class, generic2.class etc.
* Note this methods call for this case will return both S3 and s4 objects.
```{r}
# gives both S3 and S4
methods(class = lm) # or methods(class="lm")
# of them which ones are S3
.S3methods(class = "lm")
```
### 2.5: Is the object/function generic or method?
```{r}
pryr::is_s3_generic("print") # TRUE
pryr::is_s3_method("print") # FALSE; becos print is a gneric not a method
pryr::is_s3_method("print.Date") # TRUE
```
Let us define our object.
```{r}
(people <- c("Frank Blanchard",
"Andrea Gnuschke",
"Max Cole",
"Maryellen Hackett",
"Victoria Brun",
"Jonathan Summers",
"Christopher Worthington",
"Samuel Lopez",
"Richard Frederickson",
"Chris Hu") )
class(people)
(class(people) <- "InsiteGroup")
```
Suppose, we want to write an S3 function that gets the
first name from the InsiteGroup object.
```{r}
GetFirst <- function(obj) {
UseMethod("GetFirst",obj)
}
# create methods function
GetFirst.InsiteGroup <- function(obj) {
return(obj[1])
}
# create default function
GetFirst.default <- function(obj){
cat("This is a generic class\n")
# do something
}
GetFirst(people)
```
If no suitable methods can be found for a generic,
then an error is thrown. For example, at the moment, get_n_elements() only has 2 methods available. If you
pass a data.frame/matrix to get_n_elements() instead,
you'll see an error. One could use generic.default to
deal with all the missing class of objects.
### 2.6: Can variables have more than one class?
```{r S3-MultipleClasses}
(human <- "laugh")
# less specific to more specific; final default class,character
class(human) <- c("mammalia","eukaryota","character")
# create a generic method for who_am_i
who_am_i <- function(x, ...) {
UseMethod("who_am_i")
}
# create mammalia method for who_am_i
who_am_i.mammalia <- function(x, ...) {
# let us write a message
message("I am a Mammal")
}
# create eukarota method for who_am_i
who_am_i.eukaryota <- function(x, ...) {
# let us write a message
message("I am a Eukaryote")
}
# finally one for character method
who_am_i.character <- function(x, ...) {
# let us write a message
message("I am a simple character!")
}
# call human to see all the 3 messages are displayed
class(human)
who_am_i(human)
```
## 3: Advanced example: Inheritance
According to Hadley Wickam, "The NextMethod function provides a simple inheritance mechanism, using the fact that the class of an S3 object is a vector. This is very different behaviour to most other languages because it means that it's possible to have different inheritance hierarchies for different objects:"
```{r S3-MultipleClasses1}
(human <- "laugh")
# less specific to more specific; final default class,character
class(human) <- c("mammalia","eukaryota","character")
# create a generic method for who_am_i
who_am_i <- function(x, ...) {
UseMethod("who_am_i")
}
# create mammalia method for who_am_i
who_am_i.mammalia <- function(x, ...) {
# let us write a message
message("I am a Mammal")
NextMethod("x")
}
# create eukarota method for who_am_i
who_am_i.eukaryota <- function(x, ...) {
# let us write a message
message("I am a Eukaryote")
NextMethod("x")
}
# finally one for character method
who_am_i.character <- function(x, ...) {
# let us write a message
message("I am a simple character!")
# since this is the last, no NextMethod
}
# call human to see all the 3 messages are displayed
class(human)
who_am_i(human)
```