Interoperating with R

August 7, 2011 — February 13, 2023

computers are awful
number crunching
R
statistics

Connecting R to other things.

1 C++

Rcpp seems to be how everyone invokes their favoured compiled C++ code.

There are higher-level tools that do this under the hood —

rstan compiles an inner loop for Bayesian posterior simulation and a bit of basic variational inference.

If you want more freedom but still want automatic differentiation and linear algebra done by magic, try TMB. Its name and description are both awful, but it manages neat reduced rank matrix and optimization tricks.

2 Julia

Julia is a nice language that can attain high performance, especially on numerical work, and we can use it from R.

JuliaCall is the actively developed method to invoke Julia from R. Package README is the recommended way to invoke Julia. Source at Non-Contradiction/JuliaCall: Embed Julia in R.

3 Rust

4 Python

reticulate can invoke Python from R to, e.g. save and load numpy’s .npz files. It is slow.

Also spotted in the wild: rccpcnpy (pronounced Arrsipeekernoppy), which is a numpy-matrix-loader for R.

I have not tested the above for passing sparse matrices recently. Last time I tried to pass sparse matrices, circa 2014, it was painful. I developed a hdf5 hack to do it eventually. Counter-intuitively, this FS-backed method was a couple of orders of magnitude faster than rpy2 when the arrays totalled more than a few MB of data.

Python-specific R-data reading is available from the Python side via

5 Use a generic data format

These days one might use some other filesystem store such as arrow; many of these bindings are available both in R and other languages. The HDF5 example above is one such.