class: center, middle, inverse, title-slide .title[ # Using R6 under the hood ] .subtitle[ ## piping more than is printed ] .author[ ### Gina Reynolds ] --- <style type="text/css"> .remark-code{line-height: 1.5; font-size: 70%} @media print { .has-continuation { display: block; } } code.r.hljs.remark-code{ position: relative; overflow-x: hidden; } code.r.hljs.remark-code:hover{ overflow-x:visible; width: 500px; border-style: solid; } </style> --- ```r library(R6) library(magrittr) Fibonnacci <- R6Class("Fibonnacci", public = list( # objects init = c(0, 1), last = 1, current = 1, # functions fib_next = function(){ # a method x <- self$last y <- self$current self$last <- y self$current <- x + y invisible(self) #returns }, fib_init = function(x1, x2){ self$init <- c(x1, x2) self$last <- x2 self$current <- x1 + x2 invisible(self) #returns } ) ) ``` --- count: false .panel1-r6-auto[ ```r *my_fib <- Fibonnacci$new() my_fib ``` ] .panel2-r6-auto[ ``` <Fibonnacci> Public: clone: function (deep = FALSE) current: 1 fib_init: function (x1, x2) fib_next: function () init: 0 1 last: 1 ``` ] --- count: false .panel1-r6-auto[ ```r my_fib <- Fibonnacci$new() *my_fib$fib_next() my_fib ``` ] .panel2-r6-auto[ ``` <Fibonnacci> Public: clone: function (deep = FALSE) current: 2 fib_init: function (x1, x2) fib_next: function () init: 0 1 last: 1 ``` ] --- count: false .panel1-r6-auto[ ```r my_fib <- Fibonnacci$new() my_fib$fib_next() *my_fib$fib_next() my_fib ``` ] .panel2-r6-auto[ ``` <Fibonnacci> Public: clone: function (deep = FALSE) current: 3 fib_init: function (x1, x2) fib_next: function () init: 0 1 last: 2 ``` ] --- count: false .panel1-r6-auto[ ```r my_fib <- Fibonnacci$new() my_fib$fib_next() my_fib$fib_next() *my_fib$fib_next() my_fib ``` ] .panel2-r6-auto[ ``` <Fibonnacci> Public: clone: function (deep = FALSE) current: 5 fib_init: function (x1, x2) fib_next: function () init: 0 1 last: 3 ``` ] --- count: false .panel1-r6-auto[ ```r my_fib <- Fibonnacci$new() my_fib$fib_next() my_fib$fib_next() my_fib$fib_next() *my_fib$fib_next() my_fib ``` ] .panel2-r6-auto[ ``` <Fibonnacci> Public: clone: function (deep = FALSE) current: 8 fib_init: function (x1, x2) fib_next: function () init: 0 1 last: 5 ``` ] --- count: false .panel1-r6-auto[ ```r my_fib <- Fibonnacci$new() my_fib$fib_next() my_fib$fib_next() my_fib$fib_next() my_fib$fib_next() *my_fib$fib_next() my_fib ``` ] .panel2-r6-auto[ ``` <Fibonnacci> Public: clone: function (deep = FALSE) current: 13 fib_init: function (x1, x2) fib_next: function () init: 0 1 last: 8 ``` ] --- count: false .panel1-r6-auto[ ```r my_fib <- Fibonnacci$new() my_fib$fib_next() my_fib$fib_next() my_fib$fib_next() my_fib$fib_next() my_fib$fib_next() *my_fib$fib_next() my_fib ``` ] .panel2-r6-auto[ ``` <Fibonnacci> Public: clone: function (deep = FALSE) current: 21 fib_init: function (x1, x2) fib_next: function () init: 0 1 last: 13 ``` ] --- count: false .panel1-r6-auto[ ```r my_fib <- Fibonnacci$new() my_fib$fib_next() my_fib$fib_next() my_fib$fib_next() my_fib$fib_next() my_fib$fib_next() my_fib$fib_next() *my_fib$fib_next() my_fib ``` ] .panel2-r6-auto[ ``` <Fibonnacci> Public: clone: function (deep = FALSE) current: 34 fib_init: function (x1, x2) fib_next: function () init: 0 1 last: 21 ``` ] --- count: false .panel1-r6-auto[ ```r my_fib <- Fibonnacci$new() my_fib$fib_next() my_fib$fib_next() my_fib$fib_next() my_fib$fib_next() my_fib$fib_next() my_fib$fib_next() my_fib$fib_next() *my_fib$fib_init(5,4) my_fib ``` ] .panel2-r6-auto[ ``` <Fibonnacci> Public: clone: function (deep = FALSE) current: 9 fib_init: function (x1, x2) fib_next: function () init: 5 4 last: 4 ``` ] --- count: false .panel1-r6-auto[ ```r my_fib <- Fibonnacci$new() my_fib$fib_next() my_fib$fib_next() my_fib$fib_next() my_fib$fib_next() my_fib$fib_next() my_fib$fib_next() my_fib$fib_next() my_fib$fib_init(5,4) *my_fib$fib_next() my_fib ``` ] .panel2-r6-auto[ ``` <Fibonnacci> Public: clone: function (deep = FALSE) current: 13 fib_init: function (x1, x2) fib_next: function () init: 5 4 last: 9 ``` ] --- count: false .panel1-r6-auto[ ```r my_fib <- Fibonnacci$new() my_fib$fib_next() my_fib$fib_next() my_fib$fib_next() my_fib$fib_next() my_fib$fib_next() my_fib$fib_next() my_fib$fib_next() my_fib$fib_init(5,4) my_fib$fib_next() *my_fib$fib_next() my_fib ``` ] .panel2-r6-auto[ ``` <Fibonnacci> Public: clone: function (deep = FALSE) current: 22 fib_init: function (x1, x2) fib_next: function () init: 5 4 last: 13 ``` ] --- count: false .panel1-r6-auto[ ```r my_fib <- Fibonnacci$new() my_fib$fib_next() my_fib$fib_next() my_fib$fib_next() my_fib$fib_next() my_fib$fib_next() my_fib$fib_next() my_fib$fib_next() my_fib$fib_init(5,4) my_fib$fib_next() my_fib$fib_next() *my_fib$fib_next() my_fib ``` ] .panel2-r6-auto[ ``` <Fibonnacci> Public: clone: function (deep = FALSE) current: 35 fib_init: function (x1, x2) fib_next: function () init: 5 4 last: 22 ``` ] --- count: false .panel1-r6-auto[ ```r my_fib <- Fibonnacci$new() my_fib$fib_next() my_fib$fib_next() my_fib$fib_next() my_fib$fib_next() my_fib$fib_next() my_fib$fib_next() my_fib$fib_next() my_fib$fib_init(5,4) my_fib$fib_next() my_fib$fib_next() my_fib$fib_next() *my_fib$fib_next() my_fib ``` ] .panel2-r6-auto[ ``` <Fibonnacci> Public: clone: function (deep = FALSE) current: 57 fib_init: function (x1, x2) fib_next: function () init: 5 4 last: 35 ``` ] <style> .panel1-r6-auto { color: black; width: 38.6060606060606%; hight: 32%; float: left; padding-left: 1%; font-size: 80% } .panel2-r6-auto { color: black; width: 59.3939393939394%; hight: 32%; float: left; padding-left: 1%; font-size: 80% } .panel3-r6-auto { color: black; width: NA%; hight: 33%; float: left; padding-left: 1%; font-size: 80% } </style> --- ```r library(R6) library(magrittr) Fibonnacci_quiet <- R6Class("Fibonnacci_quiet", public = list( # objects init = c(0, 1), last = 1, current = 1, # functions fib_next = function(){ # a method x <- self$last y <- self$current self$last <- y self$current <- x + y invisible(self) #returns }, fib_init = function(x1, x2){ self$init <- c(x1, x2) self$last <- x2 self$current <- x1 + x2 invisible(self) #returns }, # print method; default is to print everything print = function() { print(self$current) } ) ) ``` --- count: false .panel1-r6quiet-auto[ ```r *my_fib_quiet <- Fibonnacci_quiet$new() my_fib_quiet ``` ] .panel2-r6quiet-auto[ ``` [1] 1 ``` ] --- count: false .panel1-r6quiet-auto[ ```r my_fib_quiet <- Fibonnacci_quiet$new() *my_fib_quiet$fib_next() my_fib_quiet ``` ] .panel2-r6quiet-auto[ ``` [1] 2 ``` ] --- count: false .panel1-r6quiet-auto[ ```r my_fib_quiet <- Fibonnacci_quiet$new() my_fib_quiet$fib_next() *my_fib_quiet$fib_next() my_fib_quiet ``` ] .panel2-r6quiet-auto[ ``` [1] 3 ``` ] --- count: false .panel1-r6quiet-auto[ ```r my_fib_quiet <- Fibonnacci_quiet$new() my_fib_quiet$fib_next() my_fib_quiet$fib_next() *my_fib_quiet$fib_next() my_fib_quiet ``` ] .panel2-r6quiet-auto[ ``` [1] 5 ``` ] --- count: false .panel1-r6quiet-auto[ ```r my_fib_quiet <- Fibonnacci_quiet$new() my_fib_quiet$fib_next() my_fib_quiet$fib_next() my_fib_quiet$fib_next() *my_fib_quiet$fib_next() my_fib_quiet ``` ] .panel2-r6quiet-auto[ ``` [1] 8 ``` ] --- count: false .panel1-r6quiet-auto[ ```r my_fib_quiet <- Fibonnacci_quiet$new() my_fib_quiet$fib_next() my_fib_quiet$fib_next() my_fib_quiet$fib_next() my_fib_quiet$fib_next() *my_fib_quiet$fib_next() my_fib_quiet ``` ] .panel2-r6quiet-auto[ ``` [1] 13 ``` ] --- count: false .panel1-r6quiet-auto[ ```r my_fib_quiet <- Fibonnacci_quiet$new() my_fib_quiet$fib_next() my_fib_quiet$fib_next() my_fib_quiet$fib_next() my_fib_quiet$fib_next() my_fib_quiet$fib_next() *my_fib_quiet$fib_next() my_fib_quiet ``` ] .panel2-r6quiet-auto[ ``` [1] 21 ``` ] --- count: false .panel1-r6quiet-auto[ ```r my_fib_quiet <- Fibonnacci_quiet$new() my_fib_quiet$fib_next() my_fib_quiet$fib_next() my_fib_quiet$fib_next() my_fib_quiet$fib_next() my_fib_quiet$fib_next() my_fib_quiet$fib_next() *my_fib_quiet$fib_next() my_fib_quiet ``` ] .panel2-r6quiet-auto[ ``` [1] 34 ``` ] --- count: false .panel1-r6quiet-auto[ ```r my_fib_quiet <- Fibonnacci_quiet$new() my_fib_quiet$fib_next() my_fib_quiet$fib_next() my_fib_quiet$fib_next() my_fib_quiet$fib_next() my_fib_quiet$fib_next() my_fib_quiet$fib_next() my_fib_quiet$fib_next() *my_fib_quiet$fib_init(5,4) my_fib_quiet ``` ] .panel2-r6quiet-auto[ ``` [1] 9 ``` ] --- count: false .panel1-r6quiet-auto[ ```r my_fib_quiet <- Fibonnacci_quiet$new() my_fib_quiet$fib_next() my_fib_quiet$fib_next() my_fib_quiet$fib_next() my_fib_quiet$fib_next() my_fib_quiet$fib_next() my_fib_quiet$fib_next() my_fib_quiet$fib_next() my_fib_quiet$fib_init(5,4) *my_fib_quiet$fib_next() my_fib_quiet ``` ] .panel2-r6quiet-auto[ ``` [1] 13 ``` ] --- count: false .panel1-r6quiet-auto[ ```r my_fib_quiet <- Fibonnacci_quiet$new() my_fib_quiet$fib_next() my_fib_quiet$fib_next() my_fib_quiet$fib_next() my_fib_quiet$fib_next() my_fib_quiet$fib_next() my_fib_quiet$fib_next() my_fib_quiet$fib_next() my_fib_quiet$fib_init(5,4) my_fib_quiet$fib_next() *my_fib_quiet$fib_next() my_fib_quiet ``` ] .panel2-r6quiet-auto[ ``` [1] 22 ``` ] --- count: false .panel1-r6quiet-auto[ ```r my_fib_quiet <- Fibonnacci_quiet$new() my_fib_quiet$fib_next() my_fib_quiet$fib_next() my_fib_quiet$fib_next() my_fib_quiet$fib_next() my_fib_quiet$fib_next() my_fib_quiet$fib_next() my_fib_quiet$fib_next() my_fib_quiet$fib_init(5,4) my_fib_quiet$fib_next() my_fib_quiet$fib_next() *my_fib_quiet$fib_next() my_fib_quiet ``` ] .panel2-r6quiet-auto[ ``` [1] 35 ``` ] --- count: false .panel1-r6quiet-auto[ ```r my_fib_quiet <- Fibonnacci_quiet$new() my_fib_quiet$fib_next() my_fib_quiet$fib_next() my_fib_quiet$fib_next() my_fib_quiet$fib_next() my_fib_quiet$fib_next() my_fib_quiet$fib_next() my_fib_quiet$fib_next() my_fib_quiet$fib_init(5,4) my_fib_quiet$fib_next() my_fib_quiet$fib_next() my_fib_quiet$fib_next() *my_fib_quiet$fib_next() my_fib_quiet ``` ] .panel2-r6quiet-auto[ ``` [1] 57 ``` ] <style> .panel1-r6quiet-auto { color: black; width: 38.6060606060606%; hight: 32%; float: left; padding-left: 1%; font-size: 80% } .panel2-r6quiet-auto { color: black; width: 59.3939393939394%; hight: 32%; float: left; padding-left: 1%; font-size: 80% } .panel3-r6quiet-auto { color: black; width: NA%; hight: 33%; float: left; padding-left: 1%; font-size: 80% } </style> --- count: false .panel1-wrapandpipe-auto[ ```r ## wrap and pipe *fibi_init <- function(x1 = 0, x2 = 1){ * my_fib <- Fibonnacci_quiet$new() * my_fib$fib_init(x1,x2) * my_fib *} ``` ] .panel2-wrapandpipe-auto[ ] --- count: false .panel1-wrapandpipe-auto[ ```r ## wrap and pipe fibi_init <- function(x1 = 0, x2 = 1){ my_fib <- Fibonnacci_quiet$new() my_fib$fib_init(x1,x2) my_fib } *fibi_advance <- function(my_fib){ * my_fib <- my_fib * my_fib$fib_next() * my_fib *} ``` ] .panel2-wrapandpipe-auto[ ] --- count: false .panel1-wrapandpipe-auto[ ```r ## wrap and pipe fibi_init <- function(x1 = 0, x2 = 1){ my_fib <- Fibonnacci_quiet$new() my_fib$fib_init(x1,x2) my_fib } fibi_advance <- function(my_fib){ my_fib <- my_fib my_fib$fib_next() my_fib } *fibi_collect <- function(my_fib){ * my_fib <- my_fib * my_fib$current *} ``` ] .panel2-wrapandpipe-auto[ ] --- count: false .panel1-wrapandpipe-auto[ ```r ## wrap and pipe fibi_init <- function(x1 = 0, x2 = 1){ my_fib <- Fibonnacci_quiet$new() my_fib$fib_init(x1,x2) my_fib } fibi_advance <- function(my_fib){ my_fib <- my_fib my_fib$fib_next() my_fib } fibi_collect <- function(my_fib){ my_fib <- my_fib my_fib$current } ``` ] .panel2-wrapandpipe-auto[ ] <style> .panel1-wrapandpipe-auto { color: black; width: 38.6060606060606%; hight: 32%; float: left; padding-left: 1%; font-size: 80% } .panel2-wrapandpipe-auto { color: black; width: 59.3939393939394%; hight: 32%; float: left; padding-left: 1%; font-size: 80% } .panel3-wrapandpipe-auto { color: black; width: NA%; hight: 33%; float: left; padding-left: 1%; font-size: 80% } </style> --- count: false .panel1-chain-auto[ ```r *fibi_init() ``` ] .panel2-chain-auto[ ``` [1] 1 ``` ] --- count: false .panel1-chain-auto[ ```r fibi_init() %>% * fibi_advance() ``` ] .panel2-chain-auto[ ``` [1] 2 ``` ] --- count: false .panel1-chain-auto[ ```r fibi_init() %>% fibi_advance() %>% * fibi_advance() ``` ] .panel2-chain-auto[ ``` [1] 3 ``` ] --- count: false .panel1-chain-auto[ ```r fibi_init() %>% fibi_advance() %>% fibi_advance() %>% * fibi_advance() ``` ] .panel2-chain-auto[ ``` [1] 5 ``` ] --- count: false .panel1-chain-auto[ ```r fibi_init() %>% fibi_advance() %>% fibi_advance() %>% fibi_advance() %>% * fibi_advance() ``` ] .panel2-chain-auto[ ``` [1] 8 ``` ] --- count: false .panel1-chain-auto[ ```r fibi_init() %>% fibi_advance() %>% fibi_advance() %>% fibi_advance() %>% fibi_advance() %>% * fibi_advance() ``` ] .panel2-chain-auto[ ``` [1] 13 ``` ] --- count: false .panel1-chain-auto[ ```r fibi_init() %>% fibi_advance() %>% fibi_advance() %>% fibi_advance() %>% fibi_advance() %>% fibi_advance() %>% * fibi_advance() ``` ] .panel2-chain-auto[ ``` [1] 21 ``` ] --- count: false .panel1-chain-auto[ ```r fibi_init() %>% fibi_advance() %>% fibi_advance() %>% fibi_advance() %>% fibi_advance() %>% fibi_advance() %>% fibi_advance() %>% * fibi_collect() ``` ] .panel2-chain-auto[ ``` [1] 21 ``` ] <style> .panel1-chain-auto { color: black; width: 38.6060606060606%; hight: 32%; float: left; padding-left: 1%; font-size: 80% } .panel2-chain-auto { color: black; width: 59.3939393939394%; hight: 32%; float: left; padding-left: 1%; font-size: 80% } .panel3-chain-auto { color: black; width: NA%; hight: 33%; float: left; padding-left: 1%; font-size: 80% } </style> --- count: false .panel1-collect-2[ ```r fibi_init() %>% str() fibi_init() %>% fibi_advance() %>% str() fibi_init() %>% fibi_advance() %>% fibi_collect() %>% str() ``` ] .panel2-collect-2[ ``` Classes 'Fibonnacci_quiet', 'R6' <Fibonnacci_quiet> Public: clone: function (deep = FALSE) current: 1 fib_init: function (x1, x2) fib_next: function () init: 0 1 last: 1 print: function () ``` ``` Classes 'Fibonnacci_quiet', 'R6' <Fibonnacci_quiet> Public: clone: function (deep = FALSE) current: 2 fib_init: function (x1, x2) fib_next: function () init: 0 1 last: 1 print: function () ``` ``` num 2 ``` ] --- count: false .panel1-collect-2[ ```r fibi_init() %>% str() fibi_init() %>% fibi_advance() %>% str() fibi_init() %>% fibi_advance() %>% fibi_collect() %>% str() ``` ] .panel2-collect-2[ ``` Classes 'Fibonnacci_quiet', 'R6' <Fibonnacci_quiet> Public: clone: function (deep = FALSE) current: 1 fib_init: function (x1, x2) fib_next: function () init: 0 1 last: 1 print: function () ``` ``` Classes 'Fibonnacci_quiet', 'R6' <Fibonnacci_quiet> Public: clone: function (deep = FALSE) current: 2 fib_init: function (x1, x2) fib_next: function () init: 0 1 last: 1 print: function () ``` ``` num 2 ``` ] <style> .panel1-collect-2 { color: black; width: 38.6060606060606%; hight: 32%; float: left; padding-left: 1%; font-size: 80% } .panel2-collect-2 { color: black; width: 59.3939393939394%; hight: 32%; float: left; padding-left: 1%; font-size: 80% } .panel3-collect-2 { color: black; width: NA%; hight: 33%; float: left; padding-left: 1%; font-size: 80% } </style>