diff --git a/R/Callback.R b/R/Callback.R index c0818848..34825687 100644 --- a/R/Callback.R +++ b/R/Callback.R @@ -52,6 +52,10 @@ Callback = R6Class("Callback", #' A callback can write data into the state. state = named_list(), + #' @field initialize_callback (`function()`)\cr + #' Function to be called when the callback is initialized. + initialize_callback = NULL, + #' @description #' Creates a new instance of this [R6][R6::R6Class] class. #' @@ -62,10 +66,16 @@ Callback = R6Class("Callback", #' @param man (`character(1)`)\cr #' String in the format `[pkg]::[topic]` pointing to a manual page for this object. #' The referenced help package can be opened via method `$help()`. - initialize = function(id, label = NA_character_, man = NA_character_) { + #' @param initialize_callback (`function()`)\cr + #' Function to be called when the callback is initialized. + #' The function should have the signature `function(self)`. + #' The function can be used to set default values in the state via `self$state$...`. + initialize = function(id, label = NA_character_, man = NA_character_, initialize_callback = NULL) { self$id = assert_string(id) self$label = assert_string(label, na.ok = TRUE) self$man = assert_string(man, na.ok = TRUE) + self$initialize_callback = assert_function(initialize_callback, null.ok = TRUE, args = "self") + if (!is.null(self$initialize_callback)) self$initialize_callback(self) }, #' @description diff --git a/man/Callback.Rd b/man/Callback.Rd index 9b33ac9e..142783ae 100644 --- a/man/Callback.Rd +++ b/man/Callback.Rd @@ -50,6 +50,9 @@ Defaults to \code{NA}, but can be set by child classes.} \item{\code{state}}{(named \code{list()})\cr A callback can write data into the state.} + +\item{\code{initialize_callback}}{(\verb{function()})\cr +Function to be called when the callback is initialized.} } \if{html}{\out{}} } @@ -70,7 +73,12 @@ A callback can write data into the state.} \subsection{Method \code{new()}}{ Creates a new instance of this \link[R6:R6Class]{R6} class. \subsection{Usage}{ -\if{html}{\out{