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§

pub use error::Error;
pub use error::Result;
pub use sexp::environment::EnvironmentSexp;
pub use sexp::external_pointer::get_external_pointer_addr;
pub use sexp::external_pointer::take_external_pointer_value;
pub use sexp::external_pointer::ExternalPointerSexp;
pub use sexp::external_pointer::IntoExtPtrSexp;
pub use sexp::function::FunctionArgs;
pub use sexp::function::FunctionSexp;
pub use sexp::integer::IntegerSexp;
pub use sexp::integer::OwnedIntegerSexp;
pub use sexp::list::ListSexp;
pub use sexp::list::OwnedListSexp;
pub use sexp::logical::LogicalSexp;
pub use sexp::logical::OwnedLogicalSexp;
pub use sexp::na::NotAvailableValue;
pub use sexp::null::NullSexp;
pub use sexp::numeric::NumericScalar;
pub use sexp::numeric::NumericSexp;
pub use sexp::numeric::NumericTypedSexp;
pub use sexp::raw::OwnedRawSexp;
pub use sexp::raw::RawSexp;
pub use sexp::real::OwnedRealSexp;
pub use sexp::real::RealSexp;
pub use sexp::string::OwnedStringSexp;
pub use sexp::string::StringSexp;
pub use sexp::Sexp;
pub use sexp::TypedSexp;
pub use sexp::complex::ComplexSexp;
pub use sexp::complex::OwnedComplexSexp;
pub use unwind_protect::unwind_protect;
pub use eval::assert_eq_r_code;
pub use eval::eval_parse_text;
pub use eval::EvalResult;

Modules§

altrep
error
Error types. See Error handling section of the user guide.
eval
ffi
io
log
panic_hook
protect
sexp
unwind_protect

Macros§

r_eprint
r_eprintln
r_print
r_println
savvy_err

Functions§

handle_error

Type Aliases§

Complex64
Alias for a Complex<f64>

Attribute Macros§

savvy
savvy_init