MyNixOS website logo
Description

Call Rust Code from R using the 'extendr' Crate.

Provides functions to compile and load Rust code from R, similar to how 'Rcpp' or 'cpp11' allow easy interfacing with C++ code. Also provides helper functions to create R packages that use Rust code. Under the hood, the Rust crate 'extendr' is used to do all the heavy lifting.

Call Rust code from R rextendr logo

CRANstatus rextendr statusbadge Lifecycle:stable R buildstatus codecov

Installation

To install release version from CRAN, run:

install.packages("rextendr")

or use {remotes}

remotes::install_cran("rextendr")

You can also install {rextendr} from r-universe:

install.packages('rextendr', repos = c('https://extendr.r-universe.dev', 'https://cloud.r-project.org'))

Latest development version can be installed from GitHub:

remotes::install_github("extendr/rextendr")

To execute Rust code, you will also need to set up a working Rust toolchain. See the installation instructions for libR-sys for help. If you can successfully build libR-sys you’re good.

Usage

Basic use example:

library(rextendr)

# create a Rust function
rust_function("fn add(a:f64, b:f64) -> f64 { a + b }")

# call it from R
add(2.5, 4.7)
#> [1] 7.2

Something more sophisticated:

library(rextendr)

# Rust function that computes a sum of integer or double vectors, preserving the type

rust_function(
  "fn get_sum(x : Either<Integers, Doubles>) -> Either<Rint, Rfloat> {
      match x {
          Either::Left(x) => Either::Left(x.iter().sum()),
          Either::Right(x) => Either::Right(x.iter().sum()),
      }
  }",
  use_dev_extendr = TRUE,                        # Use development version of extendr from GitHub
  features = "either",                           # Enable support for Either crate
  extendr_fn_options = list(use_try_from = TRUE) # Enable advanced type conversion
)

x <- 1:5
y <- c(1, 2, 3, 4, 5)

tibble::tibble(
  Name = c("x", "y"),
  Data = list(x, y),
  Types = purrr::map_chr(Data, typeof),
  Sum = purrr::map(Data, get_sum),
  SumRaw = purrr::flatten_dbl(Sum),
  ResultType = purrr::map_chr(Sum, typeof)
)
#> # A tibble: 2 × 6
#>   Name  Data      Types   Sum       SumRaw ResultType
#>   <chr> <list>    <chr>   <list>     <dbl> <chr>     
#> 1 x     <int [5]> integer <int [1]>     15 integer   
#> 2 y     <dbl [5]> double  <dbl [1]>     15 double

The package also enables a new chunk type for knitr, extendr, which compiles and evaluates Rust code. For example, a code chunk such as this one:

```{extendr}
rprintln!("Hello from Rust!");

let x = 5;
let y = 7;
let z = x*y;

z
```

would create the following output in the knitted document:

rprintln!("Hello from Rust!");

let x = 5;
let y = 7;
let z = x*y;

z
#> Hello from Rust!
#> [1] 35

See also


Please note that this project is released with a Contributor Code of Conduct. By participating in this project you agree to abide by its terms.

Metadata

Version

0.3.1

License

Unknown

Platforms (75)

    Darwin
    FreeBSD
    Genode
    GHCJS
    Linux
    MMIXware
    NetBSD
    none
    OpenBSD
    Redox
    Solaris
    WASI
    Windows
Show all
  • aarch64-darwin
  • aarch64-genode
  • aarch64-linux
  • aarch64-netbsd
  • aarch64-none
  • aarch64_be-none
  • arm-none
  • armv5tel-linux
  • armv6l-linux
  • armv6l-netbsd
  • armv6l-none
  • armv7a-darwin
  • armv7a-linux
  • armv7a-netbsd
  • armv7l-linux
  • armv7l-netbsd
  • avr-none
  • i686-cygwin
  • i686-darwin
  • i686-freebsd
  • i686-genode
  • i686-linux
  • i686-netbsd
  • i686-none
  • i686-openbsd
  • i686-windows
  • javascript-ghcjs
  • loongarch64-linux
  • m68k-linux
  • m68k-netbsd
  • m68k-none
  • microblaze-linux
  • microblaze-none
  • microblazeel-linux
  • microblazeel-none
  • mips-linux
  • mips-none
  • mips64-linux
  • mips64-none
  • mips64el-linux
  • mipsel-linux
  • mipsel-netbsd
  • mmix-mmixware
  • msp430-none
  • or1k-none
  • powerpc-netbsd
  • powerpc-none
  • powerpc64-linux
  • powerpc64le-linux
  • powerpcle-none
  • riscv32-linux
  • riscv32-netbsd
  • riscv32-none
  • riscv64-linux
  • riscv64-netbsd
  • riscv64-none
  • rx-none
  • s390-linux
  • s390-none
  • s390x-linux
  • s390x-none
  • vc4-none
  • wasm32-wasi
  • wasm64-wasi
  • x86_64-cygwin
  • x86_64-darwin
  • x86_64-freebsd
  • x86_64-genode
  • x86_64-linux
  • x86_64-netbsd
  • x86_64-none
  • x86_64-openbsd
  • x86_64-redox
  • x86_64-solaris
  • x86_64-windows