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
}
)
)
x <- Accumulator$new()
x$add(4)
x$sum
## [1] 4
x$refresh()
x$add(10)$add(12)
x$sum
## [1] 22
x$refresh()
x$sum
## [1] 0
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"))
}
)
)
ann <- Person$new(name = "Ann", hair = "black")
## Hello, my name is Ann.
ann
## <Person>
## Public:
## clone: function (deep = FALSE)
## greet: function ()
## hair: black
## initialize: function (name = NA, hair = NA)
## name: Ann
## set_hair: function (val)
ann$hair
## [1] "black"
ann$greet()
## Hello, my name is Ann.
ann$set_hair("red")
ann$hair
## [1] "red"
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 = data.frame(),
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
}
)
)
TPSpecify$new() ->
df_plus
df_plus$set_source_data(df = cars); df_plus
df_plus$compute_out_data(); df_plus$out_data
df_plus$source_data
df_plus$refresh() ; df_plus
tidypivot:::pivot_helper
tp <- function(nothing = NULL, data){
df_plus$set_source_data(df = data)
df_plus$compute_out_data()
print(df_plus$out_data)
}
tp_init <- function(){
df_plus <<- TPSpecify$new()
df_plus$compute_out_data()
print(df_plus$source_data)
}
tp_init() %>%
tp(data = mtcars)
tp(data = cars)
tp(rows = speed, cols = )
library(R6)
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
},
print = function() { # print method; default is to print everything
cat(self$current)
}
)
)
my_fib <- Fibonnacci$new(); my_fib
## 1
my_fib$fib_next(); my_fib
## 2
my_fib$fib_next() ; my_fib
## 3
my_fib$fib_next() ; my_fib
## 5
my_fib$fib_next() ; my_fib
## 8
my_fib$fib_next() ; my_fib
## 13
my_fib$fib_next() ; my_fib
## 21
my_fib$fib_next() ; my_fib
## 34
my_fib$fib_init(5,4) ; my_fib
## 9
my_fib$fib_next() ; my_fib
## 13
my_fib$fib_next() ; my_fib
## 22
my_fib$fib_next() ; my_fib
## 35
my_fib$fib_next() ; my_fib
## 57
my_fib$current
## [1] 57
## wrap and pipe
fibi_init <- function(x1 = 0, x2 = 1){
my_fib <- Fibonnacci$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_init() |>
fibi_advance() |>
fibi_advance() |>
fibi_advance() |>
fibi_advance() |>
fibi_advance() |>
fibi_advance()
## 21