macro_rules! r_str {
($code:expr $(,)?) => { ... };
($code:expr, env = $env:expr $(,)?) => { ... };
}Expand description
Parse and evaluate runtime R source from Rust.
r_str! is sugar around expression::r_eval_str. It is the right tool
when the R code is genuinely dynamic — built with format!, derived from
user input, or otherwise not known at compile time. For code you can write
literally, prefer r!, which gives a cheap compile-time
sanity check on the token stream.
The argument is any expression evaluating to something AsRef<str>
(&str, String, &String, …). It is parsed with R_ParseVector and
evaluated with Rf_eval, with every intermediate SEXP protected and the
parse status checked, so a syntax error becomes an Err, never a segfault
or silent wrong answer.
§Forms
r_str!(code)— evaluate inR_GlobalEnv.r_str!(code, env = e)— evaluate in the environment SEXPe.
Both forms evaluate to Result<SEXP, String>; the SEXP is unprotected
(protect it before further allocations).
§Safety
Expands to an unsafe block. Must be reached from (or routed to) the R
main thread — the underlying FFI is #[r_ffi_checked], so calls from a
worker thread are serialized onto the R thread.
§Example
let obj = "mtcars";
let code = format!("summary({obj})");
let summary = r_str!(&code)?; // in R_GlobalEnv
let three = r_str!("1L + 2L")?;
let in_env = r_str!("x + 1", env = my_env)?;