40 releases (5 breaking)

new 0.6.4 May 25, 2024
0.6.1 Apr 26, 2024
0.4.1 Mar 30, 2024
0.1.19 Dec 17, 2023
0.1.16 Oct 16, 2023

#181 in Text processing

Download history 157/week @ 2024-01-29 153/week @ 2024-02-05 551/week @ 2024-02-12 33/week @ 2024-02-19 400/week @ 2024-02-26 486/week @ 2024-03-04 305/week @ 2024-03-11 367/week @ 2024-03-18 395/week @ 2024-03-25 507/week @ 2024-04-01 509/week @ 2024-04-08 447/week @ 2024-04-15 180/week @ 2024-04-22 312/week @ 2024-04-29 150/week @ 2024-05-06 219/week @ 2024-05-13

886 downloads per month

MIT license

225KB
4.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 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"

User guide

https://yutannihilation.github.io/savvy/guide/

Contributing

CONTRIBUTING.md

Examples

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

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.4–1.2MB
~27K SLoC