A safe, isolated execution environment for running R code generated by LLMs. Uses callr for subprocess isolation with timeout protection, output capture, and structured result handling.
Details
RCodeRunner provides:
Process isolation: Code runs in a separate R process via callr
Timeout protection: Configurable timeout prevents runaway execution
Output capture: Captures stdout, stderr, messages, and warnings
Context passing: Inject data into the execution environment
Output limiting: Truncates large outputs to prevent memory issues
Security note: callr provides process isolation but NOT a security sandbox. For production with untrusted inputs, use containers or OS-level sandboxing.
Examples
if (FALSE) { # \dontrun{
runner <- r_code_runner(timeout = 5)
# Simple execution
result <- runner$execute("1 + 1")
result$result
# [1] 2
# With context
result <- runner$execute(
"mean(.context$data$mpg)",
context = list(data = mtcars)
)
result$result
# [1] 20.09062
# Timeout handling
result <- runner$execute("Sys.sleep(100)")
result$success
# [1] FALSE
result$error
# "Execution timed out after 5 seconds"
} # }
