Description Usage Arguments Details Knitr Options
This provides a cbuild
engine for knitr
, which is used for rendering
C chunks. It is automatically registered when cbuild is loaded with
library(cbuild)
, so there is no need to set it manually.
To use it, create a standard R Markdown chunk, and put cbuild
as the
engine name, like this:
1 2 3 4 5 6 | ```{cbuild}
// [[ export() ]]
SEXP fn(SEXP x) {
return x;
}
```
|
The engine is powered by source_code()
.
By default, the engine will assign any functions that have been marked
with // [[ export() ]]
into the knitr::knit_global()
environment, but
this can be controlled with the knitr option, cbuild.env
, see below.
1 |
options |
Knitr options. |
eng_cbuild()
is an alternative to the default C chunk renderer, which
loads the dynamic library but does not bind the C pointers to any R
functions.
There are a number of knitr options that can be set for the cbuild engine.
cbuild.includes
: Passed on to the includes
option of source_code()
.
cbuild.no_remap
: Passed on to the no_remap
option of source_code()
.
cbuild.env
: A single character string specifying the name of a
pre-existing environment object to create the functions in. If cbuild.env
is left unset, the default is to assign in the knitr::knit_global()
environment.
This is an example of how you could customize all 3 options:
First create an R chunk with an alternative environment:
1 2 3 | ```{r}
env <- new.env()
```
|
Now create our C chunk:
1 2 3 4 5 6 7 8 9 10 | ```{cbuild, cbuild.includes = "Rdefines.h", cbuild.env = env, cbuild.no_remap = FALSE}
// [[ export() ]]
SEXP fn(SEXP x) {
// `NUMERIC_POINTER()` is only available in `Rdefines.h`
double* p_x = NUMERIC_POINTER(x);
// Would be `Rf_ScalarReal()` if no remap was done
return ScalarReal(p_x[0]);
}
```
|
After running the C chunk, we can do:
1 2 3 4 | ```{r}
env$fn(2)
# [1] 2
```
|
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.