Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add initialize_callback stage #118

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 11 additions & 1 deletion R/Callback.R
Original file line number Diff line number Diff line change
Expand Up @@ -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.
#'
Expand All @@ -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
Expand Down
15 changes: 14 additions & 1 deletion man/Callback.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

26 changes: 24 additions & 2 deletions tests/testthat/test_Callback.R
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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)
})
Loading