source_code: Source a block of text containing C code

Description Usage Arguments Value Examples

View source: R/code.R

Description

source_code() will parse through x looking for functions tagged with // [[ export() ]] and will compile the code block and export those functions to the R side.

Usage

1
source_code(x, includes = NULL, no_remap = TRUE, show = FALSE)

Arguments

x

[character(1)]

A block of C code to compile.

includes

[NULL / character()]

Extra includes to add manually. By default, R.h and Rinternals.h are included. Specify more includes with their file name. For example, to include #include <Rdefines.h> you just need to specify "Rdefines.h".

no_remap

[logical(1)]

Should #define R_NO_REMAP be defined?

show

[logical(1)]

Should the output of compiling the source code with R CMD SHLIB be shown?

Value

A named list containing the functions specified for export.

Examples

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
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);
  }
"

sourced <- source_code(code)

sourced$fn1(1)
sourced$fn2(1, 2)

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