A convenience wrapper that creates a SecureSession, executes code, and returns the result. The session is automatically closed when done.
Usage
execute_r(
code,
tools = list(),
timeout = 30,
sandbox = TRUE,
limits = NULL,
verbose = FALSE,
validate = TRUE,
sandbox_strict = FALSE,
audit_log = NULL
)Arguments
- code
Character string of R code to execute.
- tools
List of tools created with
securer_tool(), or a named list of functions (legacy format).- timeout
Timeout in seconds for the execution, or
NULLfor no timeout (default 30).- sandbox
Logical, whether to enable OS-level sandboxing (default TRUE).
- limits
Optional named list of resource limits (see SecureSession for details).
- verbose
Logical, whether to emit diagnostic messages via
message(). Useful for debugging. Users can suppress withsuppressMessages().- validate
Logical, whether to pre-validate the code for syntax errors before sending it to the child process (default
TRUE).- sandbox_strict
Logical, whether to error if sandbox tools are not available (default
FALSE). See SecureSession for details.- audit_log
Optional path to a JSONL file for persistent audit logging (default
NULL, no file logging).
Examples
# \donttest{
# Simple computation
execute_r("1 + 1", sandbox = FALSE)
#> [1] 2
# With tools
result <- execute_r(
code = 'add(2, 3)',
tools = list(
securer_tool("add", "Add two numbers",
fn = function(a, b) a + b,
args = list(a = "numeric", b = "numeric"))
),
sandbox = FALSE
)
# }
if (FALSE) { # \dontrun{
# With resource limits (Unix only)
execute_r("1 + 1", limits = list(cpu = 10, memory = 256 * 1024 * 1024))
} # }