| encapsulate | R Documentation |
Evaluates a function, capturing conditions and measuring elapsed time. There are currently five modes:
"none": Runs the call in the current session.
Conditions are signaled normally; no log is kept.
Works well together with traceback().
"try": Like "none", but catches errors and writes them to the log.
Warnings and messages are still signaled.
"evaluate": Uses evaluate to capture errors, warnings, and messages into the log.
Printed output is lost.
"callr": Uses callr to run the function in a fresh R session.
Errors, warnings, and messages are captured into the log; printed output is lost.
Protects the calling session from segfaults at the cost of session startup overhead.
The RNG state is propagated back to the calling session after evaluation.
"mirai": Uses mirai to run the function on a daemon.
Errors, warnings, and messages are captured into the log; printed output is lost.
The daemon can be pre-started via daemons(1); if none is running, a new session is started per call.
Offers similar safety to "callr" with lower overhead when a daemon is reused across calls.
The RNG state is propagated back to the calling session after evaluation.
encapsulate(
method,
.f,
.args = list(),
.opts = list(),
.pkgs = character(),
.seed = NA_integer_,
.timeout = Inf,
.compute = "default"
)
method |
( |
.f |
( |
.args |
( |
.opts |
(named |
.pkgs |
( |
.seed |
( |
.timeout |
( |
.compute |
( |
Named list() with three elements:
"result": return value of .f, or NULL if an error was caught.
"elapsed": elapsed time in seconds, measured via proc.time().
"log": data.table() with columns "class" (ordered factor with levels
"output", "warning", "error") and "condition" (list of condition objects).
Messages are classified as "output" for historical reasons.
Empty when no conditions were captured.
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)
if (requireNamespace("evaluate", quietly = TRUE)) {
encapsulate("evaluate", f, list(n = 1), .seed = 1)
}
if (requireNamespace("callr", quietly = TRUE)) {
encapsulate("callr", f, list(n = 1), .seed = 1)
}
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.