Expand description
Safe wrappers for R expression evaluation.
This module provides ergonomic types for building and evaluating R function calls from Rust, handling GC protection and error propagation automatically.
§Types
| Type | Purpose |
|---|---|
RSymbol | Interned R symbol (SYMSXP) |
RCall | Builder for R function calls (LANGSXP) |
REnv | Well-known R environments |
REvalError | An R error caught by RCall::eval / r_eval_str / try_eval_with_handlers |
§Example
ⓘ
use miniextendr_api::expression::{RCall, REnv};
unsafe {
// Call paste0("hello", " world") in the base environment
let result = RCall::new("paste0")
.arg(Rf_mkString(c"hello".as_ptr()))
.arg(Rf_mkString(c" world".as_ptr()))
.eval(REnv::base().as_sexp())?;
}Structs§
- Eval
Args 🔒 - An R expression and the environment to evaluate it in:
eval_body’s data. - RCall
- Builder for constructing and evaluating R function calls.
- REnv
- Handle to a well-known R environment.
- REval
Error - An R error caught by
RCall::eval/RCall::eval_base,r_eval_str, the helpers built on them (REnv::package_namespace,RCall::namespaced,dollar_extract), andtry_eval_with_handlers/RCall::try_eval_with_handlers. - RSymbol
- A safe wrapper around R symbols (SYMSXP).
- TryEval
Data 🔒 - What
catch_errorshares with its callbacks.
Enums§
- Caught 🔒
- How an evaluation with R errors caught ended. The SEXPs are unprotected.
Constants§
- BASE_
ERROR_ 🔒CLASSES - The trailing classes
REvalError::specific_classesleaves out: R’s base error layers, and therust_errorlayer a miniextendr function adds.error!and aResultreturn add them again. - EXIT_
MESSAGE 🔒 - The message of the
simpleErrorthat stands for an evaluation that jumped to the top level without an error condition.
Functions§
- catch_
error 🔒 ⚠ - Run a
TryEvalData’s body witherrorconditions caught (R_tryCatchError), and store its value, or the caught condition, in its slot. ReturnsR_tryCatchError’s value: the body’s, orNULLafter an error (TryEvalData::outcometells them apart). - condition_
call 🔒 ⚠ conditionCall(cond), orNULLif the method fails or the call is thetryCatch()frame’s (is_try_catch_frame_call). Unprotected.- condition_
message 🔒 ⚠ conditionMessage(cond), through S3 dispatch, so a class with its own method (rlang’s errors, say) words its message. If the method fails or returns no string: the condition’smessagefield, then a placeholder.- dollar_
extract ⚠ - Build and evaluate
target$name— the R$extraction operator. - dots_
symbol 🔒 ⚠ - The symbol
.... - eval_
body 🔒 ⚠ - Evaluate an
EvalArgs. An R error jumps from here to thetryCatch()frame, over no Rust frame holding anything to drop. - eval_
with_ ⚠handlers - Evaluate
exprinenvin the caller’s R context, the way R code callingeval(expr, env)does. - into_
result 🔒 ⚠ - An evaluation’s outcome with a caught error as an
REvalError. The value is unprotected. - is_
try_ 🔒catch_ frame_ call - Whether
callisdoTryCatch(return(expr), name, parentenv, handler), the frame of R’stryCatch(), whichR_tryCatchErrorevaluates in. An error raised at the top of the evaluated expression (stop()called directly, not from a function) takes that frame’s call as its own; it is no call of the user’s code, soREvalError::callreportsNonethere. Base R’stry()recognises the frame by the samedoTryCatchhead. - needs_
root 🔒 - Whether
valueneeds a GC root while Rust holds it across an allocation. - quote 🔒 ⚠
quote(<value>), unprotected.valueis rooted across the allocation unless it is a symbol (needs_root).- r_
eval_ ⚠str - Parse a string of R source and evaluate it in
env. - r_
eval_ ⚠str_ global - Parse and evaluate a string of R source in
R_GlobalEnv. - string_
vector 🔒 ⚠ - The elements of a character vector (
NAas"NA"); empty for anything else. Does not allocate. - try_
call_ 🔒 ⚠raw - Call
body(body_data), hidden from the caller’s handlers and restarts (a new top-level context,R_ToplevelExec), witherrorconditions caught (catch_error). - try_
eval 🔒 ⚠ try_eval_rawwith a caught error as anREvalError. The value is unprotected.- try_
eval_ 🔒 ⚠raw - Evaluate
exprinenvastry_call_rawcalls a body. - try_
eval_ 🔒 ⚠unwinding - Evaluate
exprinenvin the caller’s R context, witherrorconditions caught (catch_error): the caller’s handlers and restarts stay in place, and any other R exit leaves as a Rust unwind (call_unwinding). NeverCaught::Exit. - try_
eval_ ⚠with_ handlers - Evaluate
exprinenvin the caller’s R context, aseval_with_handlersdoes, but return an R error asErrinstead of letting it unwind. - without_
try_ 🔒 ⚠catch_ frame_ call cond, or a shallow copy of it whosecallelement isNULLwhen that element is thetryCatch()frame’s call: the conditionREvalError::reraisesignals, with the callREvalError::callreports.
Type Aliases§
- TryBody 🔒
- A C callback run with R errors caught, given the caller’s data. An R error jumps out of it, so it holds nothing to drop.