Crate libR_sys

source ·
Expand description

A low-level libR binding library which is kept deliberately minimal.

In particular, it has no external dependencies other that libR installed on the target.


The libR-sys crate is a low level bindgen wrapper for the R programming language. The intention is to allow one or more extension mechanisms to be implemented for rust.

Effort to make the extension libraries platform-independent can be concentrated here.


use libR_sys::{Rf_initialize_R, R_CStackLimit, setup_Rmainloop};
use std::os::raw;

unsafe {
  std::env::set_var("R_HOME", "/usr/lib/R");
  let arg0 = "R\0".as_ptr() as *mut raw::c_char;
  Rf_initialize_R(1, [arg0].as_mut_ptr());
  R_CStackLimit = usize::max_value();

§Conditional compilation depending on R installation

The libR-sys crate provides these environmental variables that you can use in

  • DEP_R_R_VERSION_MAJOR: The major part of the R version (e.g. 4 in version 4.1.0)
  • DEP_R_R_VERSION_MINOR: The minor part of the R version (e.g. 1 in version 4.1.0)
  • DEP_R_R_VERSION_PATCH: The patch part of the R version (e.g. 0 in version 4.1.0)
  • DEP_R_R_VERSION_DEVEL: true if the R is a development version, otherwise false
  • DEP_R_R_VERSION_STRING: The full version string (e.g. R version 4.1.0 (2021-05-18))
  • DEP_R_R_HOME: The R home directory


use std::env;

fn main() {
    // Set R_HOME envvar, and refer to it on compile time by env!("R_HOME")
    let r_home = env::var("DEP_R_R_HOME").unwrap();
    println!("cargo:rustc-env=R_HOME={}", r_home);

    // Enable cfg setting to conditionally compile a code using a feature
    // available only on newer versions of R
    let major = env::var("DEP_R_R_VERSION_MAJOR").unwrap();
    let minor = env::var("DEP_R_R_VERSION_MINOR").unwrap();
    if &*major >= "4" && &*minor >= "1" {



  • The graphics engine will only accept locations and dimensions in native device coordinates, but it provides the following functions for converting between a couple of simple alternative coordinate systems and device coordinates: DEVICE = native units of the device NDC = Normalised device coordinates INCHES = inches (!) CM = centimetres (!!)
  • Different kinds of “N(0,1)” generators :
  • PARSE_NULL will not be returned by R_ParseVector
  • Some line end/join constants
  • These give the indices of some known keys
  • These are the three possible mouse events
  • Startup Actions
  • —— enum_SEXPTYPE —–
  • Different ways to generate discrete uniform samples
  • ALTREP sorting support
  • cetype_t is an identifier reseved by POSIX, but it is well established as public. Could remap by a #define though
  • ../main/character.c :




Type Aliases§