Skip to main content

Crate miniextendr_engine

Crate miniextendr_engine 

Source
Expand description

miniextendr-engine: standalone R embedding for Rust binaries and tests.

This crate centralizes libR linking (via build.rs), R initialization, and a minimal runtime handle for processing events and interrupts. It is intended for Rust-only executables and integration tests that embed R.

Not for R packages: this crate uses non-API R internals (Rembedded.h, Rinterface.h). For R packages, depend on miniextendr-api and keep nonapi disabled.

§When to use

  • Rust binaries that embed R.
  • Integration tests or benchmarks that need full control over R startup.

§Quick start

// SAFETY: Must be called once, from the main thread.
let engine = unsafe {
    miniextendr_engine::REngine::build()
        .with_args(&["R", "--quiet", "--vanilla"])
        .init()
        .expect("Failed to initialize R")
};

// ... use R APIs from the main thread ...

std::mem::forget(engine); // optional: intentionally leak the handle

§Initialization details

  • Ensures R_HOME (via R RHOME) if missing.
  • Calls Rf_initialize_R directly to avoid double setup_Rmainloop().
  • Calls setup_Rmainloop() exactly once after initialization.

§Runtime sentinel

if miniextendr_engine::r_initialized_sentinel() {
    // R has been initialized in this process.
}

§Safety

  • Must only be initialized once per process.
  • Must be called from the main thread.
  • No shutdown: Rf_endEmbeddedR is intentionally not called because the cleanup path is not reentrant-safe. The OS reclaims resources on exit.

Structs§

REngine
Handle to an initialized R runtime.
REngineBuilder
Builder for configuring and initializing the R runtime.

Enums§

REngineError
Errors that can occur during R engine initialization.

Statics§

R_CStackDir 🔒
R_CStackStart 🔒
R_Interactive 🔒
R_SignalHandlers 🔒

Functions§

R_CheckUserInterrupt 🔒
R_ProcessEvents 🔒
Rf_endEmbeddedR 🔒
Rf_initialize_R 🔒
ensure_r_home_env 🔒
r_initialized_sentinel
Check whether Rf_initialize_R has run by inspecting stack sentinels.
set_r_interactive 🔒
Write to R’s global R_Interactive flag.
set_r_signal_handlers 🔒
Write to R’s global R_SignalHandlers flag.
setup_Rmainloop 🔒