encapsulate {mlr3misc} | R Documentation |
Evaluates a function while both recording an output log and measuring the elapsed time. There are currently three different modes implemented to encapsulate a function call:
"none"
: Just runs the call in the current session and measures the elapsed time.
Does not keep a log, output is printed directly to the console.
Works well together with traceback()
.
"evaluate"
: Uses the package evaluate to call the function, measure time and do the logging.
"callr"
: Uses the package callr to call the function, measure time and do the logging.
This encapsulation spawns a separate R session in which the function is called.
While this comes with a considerable overhead, it also guards your session from being teared down by segfaults.
encapsulate(method, .f, .args = list(), .opts = list(), .pkgs = character(), .seed = NA_integer_)
method |
:: |
.f |
:: |
.args |
:: |
.opts |
:: named |
.pkgs |
:: |
.seed |
:: |
(named list()
) with three fields:
"result"
: the return value of .f
"elapsed"
: elapsed time in seconds. Measured as proc.time()
difference before/after the function call.
"log"
: data.table()
with columns "class"
(ordered factor with levels "output"
, "warning"
and "error"
) and "message"
(character()
).
f = function(n) { message("hi from f") if (n > 5) { stop("n must be <= 5") } runif(n) } encapsulate("none", f, list(n = 1), .seed = 1) encapsulate("evaluate", f, list(n = 1), .seed = 1) encapsulate("callr", f, list(n = 1), .seed = 1)