knitr::opts_chunk$set(
  collapse = TRUE,
  comment = "#>",
  fig.path = "man/figures/README-",
  out.width = "100%"
)

cbuild

R build status Travis build status Codecov test coverage

The goal of cbuild is to provide tools for working with C both interactively and when constructing an R package. The two broad goals are:

// [[ export() ]] SEXP fn(SEXP x) { return x; }

Installation

You can install the development version from GitHub with:

# install.packages("devtools")
devtools::install_github("DavisVaughan/cbuild")

Examples

library(cbuild)

The easiest way to get started is with source_function(), which allows you to source a C function from text. It automatically includes R.h and Rinternals.h for you to use.

fn <- source_function("
  SEXP fn(SEXP x) {
    return x;
  }
")

fn(1)

From there, you can use source_code() to source larger chunks of code. Tag functions that you want to export with // [[ export() ]].

fns <- source_code("
  static SEXP helper(SEXP x) {
    return x;
  }

  // [[ export() ]]
  SEXP fn1(SEXP x) {
    return helper(x);
  }

  // [[ export() ]]
  SEXP fn2(SEXP x, SEXP y) {
    double result = REAL(x)[0] + REAL(y)[0];
    return Rf_ScalarReal(result);
  }
")

fns$fn1(1)

fns$fn2(1, 2)

If you have a full file to source, you can use source_file().



DavisVaughan/cbuild documentation built on Dec. 25, 2019, 5:11 a.m.