Slide 1

Slide 1 text

Hadley Wickham on behalf of 
 The R Consortium Working Group on OOP 
 https://rconsortium.github.io/OOP-WG/ R7: A fi rst look at a new OOP system for R June 2022

Slide 2

Slide 2 text

bizarro < - function(x) { if (is.numeric(x)) { - x } else if (is.logical(x)) { !x } else if (is.character(x)) { str_reverse(x) } else if (is.factor(x)) { levels(x) < - rev(level(x)) x } else { stop("Not supported") } } Why do we need OOP? This is even more important for functions in base R, like print()

Slide 3

Slide 3 text

S3 S4 English Fairy Tales (1918) by Flora Annie Steel, illustrated by Arthur Rackham R7 R7 = S3 + S4

Slide 4

Slide 4 text

library(R7) bizarro < - new_generic("bizarro", "x") With R7, we fi rst de fi ne a generic Name of the generic Name of the argument used for dispatch

Slide 5

Slide 5 text

method(bizarro, class_numeric) < - function(x) { - x } method(bizarro, class_logical) < - function(x) { !x } method(bizarro, class_character) < - function(x) { str_reverse(x) } Then we de fi ne methods Name of the generic Built-in class

Slide 6

Slide 6 text

bizarro #> function (x, . . . ) with 4 methods: #> 1 : method(bizarro, "integer") #> 2 : method(bizarro, "double") #> 3 : method(bizarro, "character") #> 4 : method(bizarro, "logical")

Slide 7

Slide 7 text

bizarro(1) #> [1] -1 bizarro(TRUE) #> [1] FALSE bizarro(mean) #> Error: generic `bizarro()` can't f i nd method #> for function object

Slide 8

Slide 8 text

Generics de fi ne behaviour Classes de fi ne data

Slide 9

Slide 9 text

range < - new_class("range", properties = list( start = class_numeric, end = class_numeric ) ) Creating a new class Name of the class Property type Property name Constructor function

Slide 10

Slide 10 text

x < - range(start = 1, end = 10) x #> #> @ start: num 1 #> @ end : num 10 x@start x@end < - 20 x@end < - "x" #> Error: @end must be , not

Slide 11

Slide 11 text

r < - range(start = 10, end = 1) 
 #> #> @ start: num 10 #> @ end : num 1 But…

Slide 12

Slide 12 text

range < - new_class("range", properties = . . . , validator = function(self) { if (length(self@start) ! = 1) { "@start must be length 1" } else if (length(self@end) ! = 1) { "@end must be length 1" } else if (self@end < self@start) { "@end must be greater than or equal to @start" } } ) Can add a validator to prevent that from happening

Slide 13

Slide 13 text

range < - new_class("range", properties = list( start = class_double, end = class_double, length = new_property( getter = function(self) self@end - self@start, ) ), … ) Can also create dynamic properties

Slide 14

Slide 14 text

S3 S4 English Fairy Tales (1918) by Flora Annie Steel, illustrated by Arthur Rackham R7

Slide 15

Slide 15 text

No content

Slide 16

Slide 16 text

1. Team e ff ort. 2. Formal de fi nition of class and properties (like S4). 3. Embrace data access. 4. Backward compatible with S3. 5. Pit of success. Why do we hope this will succeed?

Slide 17

Slide 17 text

Please try it out! https://rconsortium.github.io/OOP-WG/ remotes : : install_github("rconsortium/OOP-WG") What doesn’t work? 
 What don’t you understand? These slides at https://speakerdeck.com/hadley/r7-a- fi rst-look

Slide 18

Slide 18 text

No content