19.11 Introducing ggproto
It’s essentially a list of functions
String <- list(
add = function(x, y) paste0(x, y),
subtract = function(x, y) gsub(y, "", x, fixed = TRUE),
show = function(x, y) paste0(x, " and ", y)
)
Number <- list(
add = function(x, y) x + y,
subtract = function(x, y) x - y,
show = String$show
)
[1] "ab"
[1] "jun"
[1] "ggplot and bookclub"
[1] 3
[1] 5
[1] "1 and 2"
19.11.1 ggproto syntax
From the book:
Person <- ggproto("Person", NULL,
first = "",
last = "",
birthdate = NA,
full_name = function(self) {
paste(self$first, self$last)
},
age = function(self) {
days_old <- Sys.Date() - self$birthdate
floor(as.integer(days_old) / 365.25)
},
description = function(self) {
paste(self$full_name(), "is", self$age(), "old")
}
)