class: center, middle, inverse, title-slide .title[ # Exploded code ] .subtitle[ ## Using flipbookr and xaringan ] .author[ ### Me ] --- <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> - https://www.datacamp.com/tutorial/r-objects-and-classes - https://adv-r.hadley.nz/oo.html - https://www.youtube.com/watch?v=3GEFd8rZQgY Winston Chang --- count: false .panel1-AccumulatorNew0-auto[ ```r *library(R6) NA ``` ] .panel2-AccumulatorNew0-auto[ ``` [1] NA ``` ] --- count: false .panel1-AccumulatorNew0-auto[ ```r library(R6) *Accumulator <- R6Class("Accumulator", * public = list( * sum = 0, # value that can evolve * add = function(x){ # a method * self$sum <- self$sum + x * invisible(self) #returns * }, * refresh = function(x){ * self$sum <- 0 * invisible(self) #returns * } * ) *) NA ``` ] .panel2-AccumulatorNew0-auto[ ``` [1] NA ``` ] <style> .panel1-AccumulatorNew0-auto { color: black; width: 38.6060606060606%; hight: 32%; float: left; padding-left: 1%; font-size: 80% } .panel2-AccumulatorNew0-auto { color: black; width: 59.3939393939394%; hight: 32%; float: left; padding-left: 1%; font-size: 80% } .panel3-AccumulatorNew0-auto { color: black; width: NA%; hight: 33%; float: left; padding-left: 1%; font-size: 80% } </style> --- count: false .panel1-AccumulatorNew-auto[ ```r *x <- Accumulator$new() ``` ] .panel2-AccumulatorNew-auto[ ] --- count: false .panel1-AccumulatorNew-auto[ ```r x <- Accumulator$new() *x$add(4) ``` ] .panel2-AccumulatorNew-auto[ ] --- count: false .panel1-AccumulatorNew-auto[ ```r x <- Accumulator$new() x$add(4) *x$sum ``` ] .panel2-AccumulatorNew-auto[ ``` [1] 4 ``` ] --- count: false .panel1-AccumulatorNew-auto[ ```r x <- Accumulator$new() x$add(4) x$sum *x$refresh() ``` ] .panel2-AccumulatorNew-auto[ ``` [1] 4 ``` ] --- count: false .panel1-AccumulatorNew-auto[ ```r x <- Accumulator$new() x$add(4) x$sum x$refresh() *x$add(10)$add(12) ``` ] .panel2-AccumulatorNew-auto[ ``` [1] 4 ``` ] --- count: false .panel1-AccumulatorNew-auto[ ```r x <- Accumulator$new() x$add(4) x$sum x$refresh() x$add(10)$add(12) *x$sum ``` ] .panel2-AccumulatorNew-auto[ ``` [1] 4 ``` ``` [1] 22 ``` ] --- count: false .panel1-AccumulatorNew-auto[ ```r x <- Accumulator$new() x$add(4) x$sum x$refresh() x$add(10)$add(12) x$sum *x$refresh() ``` ] .panel2-AccumulatorNew-auto[ ``` [1] 4 ``` ``` [1] 22 ``` ] --- count: false .panel1-AccumulatorNew-auto[ ```r x <- Accumulator$new() x$add(4) x$sum x$refresh() x$add(10)$add(12) x$sum x$refresh() *x$sum ``` ] .panel2-AccumulatorNew-auto[ ``` [1] 4 ``` ``` [1] 22 ``` ``` [1] 0 ``` ] <style> .panel1-AccumulatorNew-auto { color: black; width: 38.6060606060606%; hight: 32%; float: left; padding-left: 1%; font-size: 80% } .panel2-AccumulatorNew-auto { color: black; width: 59.3939393939394%; hight: 32%; float: left; padding-left: 1%; font-size: 80% } .panel3-AccumulatorNew-auto { color: black; width: NA%; hight: 33%; float: left; padding-left: 1%; font-size: 80% } </style> --- count: false .panel1-example2-auto[ ```r *library(R6) NA ``` ] .panel2-example2-auto[ ``` [1] NA ``` ] --- count: false .panel1-example2-auto[ ```r library(R6) *Person <- R6Class("Person", * public = list( * name = NULL, * hair = NULL, * initialize = function(name = NA, hair = NA) { * self$name <- name * self$hair <- hair * self$greet() * }, * set_hair = function(val) { * self$hair <- val * }, * greet = function() { * cat(paste0("Hello, my name is ", self$name, ".\n")) * } * ) *) NA ``` ] .panel2-example2-auto[ ``` [1] NA ``` ] <style> .panel1-example2-auto { color: black; width: 38.6060606060606%; hight: 32%; float: left; padding-left: 1%; font-size: 80% } .panel2-example2-auto { color: black; width: 59.3939393939394%; hight: 32%; float: left; padding-left: 1%; font-size: 80% } .panel3-example2-auto { color: black; width: NA%; hight: 33%; float: left; padding-left: 1%; font-size: 80% } </style> --- count: false .panel1-example2demo-auto[ ```r *ann <- Person$new(name = "Ann", hair = "black") ``` ] .panel2-example2demo-auto[ ``` Hello, my name is Ann. ``` ] --- count: false .panel1-example2demo-auto[ ```r ann <- Person$new(name = "Ann", hair = "black") *ann ``` ] .panel2-example2demo-auto[ ``` Hello, my name is Ann. ``` ``` <Person> Public: clone: function (deep = FALSE) greet: function () hair: black initialize: function (name = NA, hair = NA) name: Ann set_hair: function (val) ``` ] --- count: false .panel1-example2demo-auto[ ```r ann <- Person$new(name = "Ann", hair = "black") ann *ann$hair ``` ] .panel2-example2demo-auto[ ``` Hello, my name is Ann. ``` ``` <Person> Public: clone: function (deep = FALSE) greet: function () hair: black initialize: function (name = NA, hair = NA) name: Ann set_hair: function (val) ``` ``` [1] "black" ``` ] --- count: false .panel1-example2demo-auto[ ```r ann <- Person$new(name = "Ann", hair = "black") ann ann$hair *ann$greet() ``` ] .panel2-example2demo-auto[ ``` Hello, my name is Ann. ``` ``` <Person> Public: clone: function (deep = FALSE) greet: function () hair: black initialize: function (name = NA, hair = NA) name: Ann set_hair: function (val) ``` ``` [1] "black" ``` ``` Hello, my name is Ann. ``` ] --- count: false .panel1-example2demo-auto[ ```r ann <- Person$new(name = "Ann", hair = "black") ann ann$hair ann$greet() *ann$set_hair("red") ``` ] .panel2-example2demo-auto[ ``` Hello, my name is Ann. ``` ``` <Person> Public: clone: function (deep = FALSE) greet: function () hair: black initialize: function (name = NA, hair = NA) name: Ann set_hair: function (val) ``` ``` [1] "black" ``` ``` Hello, my name is Ann. ``` ] --- count: false .panel1-example2demo-auto[ ```r ann <- Person$new(name = "Ann", hair = "black") ann ann$hair ann$greet() ann$set_hair("red") *ann$hair ``` ] .panel2-example2demo-auto[ ``` Hello, my name is Ann. ``` ``` <Person> Public: clone: function (deep = FALSE) greet: function () hair: black initialize: function (name = NA, hair = NA) name: Ann set_hair: function (val) ``` ``` [1] "black" ``` ``` Hello, my name is Ann. ``` ``` [1] "red" ``` ] <style> .panel1-example2demo-auto { color: black; width: 38.6060606060606%; hight: 32%; float: left; padding-left: 1%; font-size: 80% } .panel2-example2demo-auto { color: black; width: 59.3939393939394%; hight: 32%; float: left; padding-left: 1%; font-size: 80% } .panel3-example2demo-auto { color: black; width: NA%; hight: 33%; float: left; padding-left: 1%; font-size: 80% } </style> --- ```r library(R6) library(magrittr) TPSpecify <- R6Class("TPSpecify", public = list( sum = 0, # value that can evolve add = function(x){ self$sum <- self$sum + x ; invisible(self)}, source_data = NULL, set_source_data = function(df) { self$source_data <- df }, rows = NULL, set_rows = function(rows){ self$rows <- rows}, cols = NULL, set_cols = function(cols){ self$cols <- cols}, fun = NULL, set_fun = function(cols){ self$fun <- fun}, out_data = NULL, compute_out_data = function(){ self$out_data <- self$source_data %>% dplyr::count() }, refresh = function(x){ self$source_data <- NULL } ) ) ``` --- count: false .panel1-tpspecify2-auto[ ```r *df_plus <- TPSpecify$new() df_plus ``` ] .panel2-tpspecify2-auto[ ``` <TPSpecify> Public: add: function (x) clone: function (deep = FALSE) cols: NULL compute_out_data: function () fun: NULL out_data: NULL refresh: function (x) rows: NULL set_cols: function (cols) set_fun: function (cols) set_rows: function (rows) set_source_data: function (df) source_data: NULL sum: 0 ``` ] --- count: false .panel1-tpspecify2-auto[ ```r df_plus <- TPSpecify$new() *df_plus$set_source_data(df = cars) df_plus ``` ] .panel2-tpspecify2-auto[ ``` <TPSpecify> Public: add: function (x) clone: function (deep = FALSE) cols: NULL compute_out_data: function () fun: NULL out_data: NULL refresh: function (x) rows: NULL set_cols: function (cols) set_fun: function (cols) set_rows: function (rows) set_source_data: function (df) source_data: data.frame sum: 0 ``` ] --- count: false .panel1-tpspecify2-auto[ ```r df_plus <- TPSpecify$new() df_plus$set_source_data(df = cars) *df_plus$compute_out_data() df_plus ``` ] .panel2-tpspecify2-auto[ ``` <TPSpecify> Public: add: function (x) clone: function (deep = FALSE) cols: NULL compute_out_data: function () fun: NULL out_data: data.frame refresh: function (x) rows: NULL set_cols: function (cols) set_fun: function (cols) set_rows: function (rows) set_source_data: function (df) source_data: data.frame sum: 0 ``` ] --- count: false .panel1-tpspecify2-auto[ ```r df_plus <- TPSpecify$new() df_plus$set_source_data(df = cars) df_plus$compute_out_data() *df_plus$refresh() df_plus ``` ] .panel2-tpspecify2-auto[ ``` <TPSpecify> Public: add: function (x) clone: function (deep = FALSE) cols: NULL compute_out_data: function () fun: NULL out_data: data.frame refresh: function (x) rows: NULL set_cols: function (cols) set_fun: function (cols) set_rows: function (rows) set_source_data: function (df) source_data: NULL sum: 0 ``` ] <style> .panel1-tpspecify2-auto { color: black; width: 38.6060606060606%; hight: 32%; float: left; padding-left: 1%; font-size: 80% } .panel2-tpspecify2-auto { color: black; width: 59.3939393939394%; hight: 32%; float: left; padding-left: 1%; font-size: 80% } .panel3-tpspecify2-auto { color: black; width: NA%; hight: 33%; float: left; padding-left: 1%; font-size: 80% } </style>