The hardware and bandwidth for this mirror is donated by dogado GmbH, the Webhosting and Full Service-Cloud Provider. Check out our Wordpress Tutorial.
If you wish to report a bug, or if you are interested in having us mirror your free-software or open-source project, please feel free to contact us at mirror[@]dogado.de.
To install release version from CRAN, run:
install.packages("rextendr")
or use {remotes}
::install_cran("rextendr") remotes
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:
::install_github("extendr/rextendr") remotes
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.
A good first step is to check the status of Rust toolchain and
available targets using rust_sitrep()
. If everything is OK,
you should see something like this:
rust_sitrep()
# Rust infrastructure sitrep:
# ✔ "rustup": 1.26.0 (5af9b9484 2023-04-05)
# ✔ "cargo": 1.72.0 (103a7ff2e 2023-08-15)
# ℹ host: x86_64-pc-windows-msvc
# ℹ toolchain: stable-x86_64-pc-windows-msvc (default)
# ℹ target: x86_64-pc-windows-gnu
If, for instance, no toolchain is found, you will see something like this:
rust_sitrep()
# Rust infrastructure sitrep:
# ✔ "rustup": 1.26.0 (5af9b9484 2023-04-05)
# ✔ "cargo": 1.72.0 (103a7ff2e 2023-08-15)
# ℹ host: x86_64-pc-windows-msvc
# ! Toolchain stable-x86_64-pc-windows-msvc is required to be installed and set as default
# ℹ Run `rustup toolchain install stable-x86_64-pc-windows-msvc` to install it
# ℹ Run `rustup default stable-x86_64-pc-windows-msvc` to make it default
Finally, if you are missing the required target (on all platforms but
Windows {rextendr}
uses default target), the report will
resemble the following:
rust_sitrep()
# Rust infrastructure sitrep:
# ✔ "rustup": 1.26.0 (5af9b9484 2023-04-05)
# ✔ "cargo": 1.72.0 (103a7ff2e 2023-08-15)
# ℹ host: x86_64-pc-windows-msvc
# i toolchains: nightly-x86_64-pc-windows-msvc and stable-x86_64-pc-windows-msvc (default)
# i targets: x86_64-pc-windows-msvc and i686-pc-windows-msvc
# ! Target x86_64-pc-windows-gnu is required on this host machine
# i Run `rustup target add x86_64-pc-windows-gnu` to install it
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
)
<- 1:5
x <- c(1, 2, 3, 4, 5)
y
::tibble(
tibbleName = 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 #
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.
These binaries (installable software) and packages are in development.
They may not be fully stable and should be used with caution. We make no claims about them.
Health stats visible at Monitor.