Crate savvy

Source
Expand description

§Savvy - A Simple R Interface

savvy is a simple R extension interface using Rust, like the extendr framework. The name “savvy” comes from the Japanese word “錆” (pronounced as sàbí), which means “Rust”.

With savvy, you can automatically generate R functions from Rust code. Please refer to the user guide for a detailed introduction!

§Example

Rust:

use savvy::savvy;

/// Convert to Upper-case
///
/// @param x A character vector.
/// @export
#[savvy]
fn to_upper(x: StringSexp) -> savvy::Result<savvy::Sexp> {
    // Use `Owned{type}Sexp` to allocate an R vector for output.
    let mut out = OwnedStringSexp::new(x.len())?;

    for (i, e) in x.iter().enumerate() {
        // To Rust, missing value is an ordinary value. In `&str`'s case, it's just "NA".
        // You have to use `.is_na()` method to distinguish the missing value.
        if e.is_na() {
            // Set the i-th element to NA
            out.set_na(i)?;
            continue;
        }

        let e_upper = e.to_uppercase();
        out.set_elt(i, e_upper.as_str())?;
    }

    out.into()
}

R:

to_upper(c("a", "b", "c"))
#> [1] "A" "B" "C"

§Feature flags

  • complex: Provides the support for complex with the num-complex crate.

  • altrep: Provides the support for ALTREP.

  • logger: Provides an env_logger for R’s stderr.

  • use-custom-error: Opt-out the auto error conversion in order to allow defining custom errors. See Error handling section of the user guide.

Re-exports§

Modules§

Macros§

Functions§

Type Aliases§

Attribute Macros§