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:
-
Provide a way to interactively source C code into your R session. See
source_function()
andsource_code()
to get started. -
Provide an automatic registration system for R package developers that use C, see
process_attributes()
. It can automatically generate theinit.c
file for you, using a comment system similar toRcpp::export
. For example, the following would generate an entry for the C functionfn()
ininit.c
, and generate the glue code to export it as an R routine namedfn
, which you could call from the R side with.Call(fn, 1)
:// [[ export() ]] SEXP fn(SEXP x) { return x; }
You can install the development version from GitHub with:
# install.packages("devtools")
devtools::install_github("DavisVaughan/cbuild")
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)
#> [1] 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)
#> [1] 1
fns$fn2(1, 2)
#> [1] 3
If you have a full file to source, you can use source_file()
.