| compile_rs_lib | R Documentation |
Compiles the #[extendr] functions in a Rust script with rextendr::rust_source() and returns a self-contained bundle (the compiled shared library plus its generated R wrappers) that run_rs_step_prebuilt() can reload in any branch without recompiling. This is the function the companion <name>_rust_lib target built by tar_target_rs(..., pattern = tarpolyglot_map(...)) calls; it is exported so the call resolves at run time, but package users should not call it directly.
compile_rs_lib(
script,
dependencies = NULL,
features = NULL,
profile = NULL,
toolchain = NULL
)
script |
Path to the Rust script containing |
dependencies, features, profile |
Passed to |
toolchain |
Optional rustup toolchain (e.g. |
The bundle embeds the library bytes, so it travels with the target's value to any worker or machine, and reloading is a dyn.load() (near-instant) rather than a fresh cargo build. See tarpolyglot_map() for the motivation and run_rs_step() for the per-branch (recompiling) alternative.
An object of class tp_rust_lib: a list with the library basename, the raw library bytes, and the generated wrapper objs (named list of R functions).
A worker receives whatever the constructor already resolved, which is one of two things: a path to a file on disk, or an inline carrier built by tar_code() that holds the code in memory. Both are accepted, so a direct call may pass either.
tar_target_path() is deliberately not a third form at this level. It is a constructor-level convenience: tar_target_py() and the other constructors rewrite it while the pipeline's DAG is built, so that by the time a worker runs it has already become the ordinary file path held by the upstream target. Handing the result of tar_target_path() straight to a worker therefore does not resolve to a file. The three forms as written in _targets.R, and which of them tracks your edits, are covered in vignette("scripts").
tarpolyglot_map(), run_rs_step_prebuilt(), tar_target_rs()
# Compiling needs a Rust toolchain, so this is gated on
# TARPOLYGLOT_EXAMPLES=true and runs in a temporary directory.
if (identical(Sys.getenv("TARPOLYGLOT_EXAMPLES"), "true")) {
targets::tar_dir({
# Normally invoked by tar_target_rs(pattern = tarpolyglot_map(...)).
writeLines("#[extendr] fn square(x: f64) -> f64 { x * x }", "square.rs")
lib <- compile_rs_lib(script = "square.rs")
class(lib)
})
}
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.