54 releases

new 0.8.3 Jan 11, 2025
0.8.2 Dec 25, 2024
0.8.1 Nov 17, 2024
0.6.5 Jul 2, 2024
0.1.16 Oct 16, 2023

#144 in Text processing

Download history 288/week @ 2024-09-23 247/week @ 2024-09-30 312/week @ 2024-10-07 729/week @ 2024-10-14 758/week @ 2024-10-21 646/week @ 2024-10-28 129/week @ 2024-11-04 343/week @ 2024-11-11 226/week @ 2024-11-18 168/week @ 2024-11-25 83/week @ 2024-12-02 648/week @ 2024-12-09 99/week @ 2024-12-16 187/week @ 2024-12-23 32/week @ 2024-12-30 469/week @ 2025-01-06

832 downloads per month

MIT license

260KB
5K SLoC

Savvy - A simple R extension interface using Rust

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. This is an example of what a savvy-powered function would look like:

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"

Documents

Contributing

CONTRIBUTING.md

Examples

A toy example R package can be found in R-package/ directory.

Savvy is used in the following R packages:

Thanks

Savvy is not quite unique. This project is made possible by heavily taking inspiration from other great projects:

  • The basic idea is of course based on extendr. Savvy would not exist without extendr.
  • cpp11’s “writable” concept influenced the design a lot. Also, I learned a lot from the great implementation such as the protection mechanism.
  • PyO3 made me realize that the FFI crate doesn’t need to be a “sys” crate.

Dependencies

~0.3–1.2MB
~27K SLoC