Interoperating with R

August 7, 2011 — February 13, 2023

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 this for Bayesian posterior simulation and a little bit of basic variational inference.

If you want a little more freedom but still want to have automatic differentiation and linear algebra done by magic, try TMB whose name and description are both awful but which manages pretty 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.

There are two alternative methods to connect R and Julia, XRJulia and RJulia.

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 both in R and other languages. The HDF5 example above is one such.