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{
}}\preformatted{Callback$new(id, label = NA_character_, man = NA_character_)}\if{html}{\out{
}} +\if{html}{\out{
}}\preformatted{Callback$new( + id, + label = NA_character_, + man = NA_character_, + initialize_callback = NULL +)}\if{html}{\out{
}} } \subsection{Arguments}{ @@ -85,6 +93,11 @@ Label for the new instance.} \item{\code{man}}{(\code{character(1)})\cr String in the format \verb{[pkg]::[topic]} pointing to a manual page for this object. The referenced help package can be opened via method \verb{$help()}.} + +\item{\code{initialize_callback}}{(\verb{function()})\cr +Function to be called when the callback is initialized. +The function should have the signature \verb{function(self)}. +The function can be used to set default values in the state via \code{self$state$...}.} } \if{html}{\out{}} } diff --git a/tests/testthat/test_Callback.R b/tests/testthat/test_Callback.R index 80491b5a..46a32ae1 100644 --- a/tests/testthat/test_Callback.R +++ b/tests/testthat/test_Callback.R @@ -89,8 +89,6 @@ test_that("call_back() function works", { expect_equal(context$d, 1) }) - - test_that("as_callbacks.Callback works", { CallbackTest = R6Class("CallbackTest", inherit = Callback, @@ -105,3 +103,27 @@ test_that("as_callbacks.Callback works", { expect_list(as_callbacks(test_callback)) }) + +test_that("initialize callback works", { + CallbackTest = R6Class("CallbackTest", + inherit = Callback, + public = list( + on_stage = function(callback, context) { + context$a = 1 + }, + info = "Test" + ) + ) + + initialize_callback = function(self) { + self$state$b = 1 + } + + test_callback = CallbackTest$new( + id = "mlr3misc.test", + label = "Test Callback", + man = "mlr3misc::Callback", + initialize_callback = initialize_callback) + + expect_equal(test_callback$state$b, 1) +})