Context objects allow Callback objects to access and modify data. The following packages implement context subclasses:
ContextOptimization
in bbotk.ContextEval
in mlr3tuning.ContextTorch
inmlr3torch
Details
Context is an abstract base class. A subclass inherits from Context. Data is stored in public fields. Access to the data can be restricted with active bindings (see example).
Public fields
id
(
character(1)
)
Identifier of the object. Used in tables, plot and text output.label
(
character(1)
)
Label for this object. Can be used in tables, plot and text output instead of the ID.
Methods
Examples
library(data.table)
library(R6)
# data table with column x an y
data = data.table(x = runif(10), y = sample(c("A", "B"), 10, replace = TRUE))
# context only allows to access column y
ContextExample = R6Class("ContextExample",
inherit = Context,
public = list(
data = NULL,
initialize = function(data) {
self$data = data
}
),
active = list(
y = function(rhs) {
if (missing(rhs)) return(self$data$y)
self$data$y = rhs
}
)
)
context = ContextExample$new(data)
# retrieve content of column y
context$y
#> [1] "B" "A" "A" "A" "A" "B" "A" "A" "B" "B"
# change content of column y to "C"
context$y = "C"