Examples of C, C++ functions that can be called from R.
Mainly oriented to the use of C++ Armadillo.
- inline
- Rcpp
- RcppArmadillo
- rbenchmark
- compiler
-
Use notebooks to:
- call
C++
code withsourceCpp
- show and run
C
code withinline
- write directly C++ code in R chunks using
engine='Rcpp'
- run inline
C++
code with thecxxfunction
function
- call
-
Use C, C++ files:
- when these codes will be called from a notebook
- the C++ files can have R calls
-
C++ code located under
./src
folder containR
that run the functions in the same script -
R
code cannot be run from within theC
file. It must be compiled first and called from a notebook or from anR
script
Example: to run the function
bessel
:
system("R CMD SHLIB ../src/bessel.c") # compile
dyn.load(paste("../src/bessel", .Platform$dynlib.ext, sep = "")) # load
There is a notebook, Kalman.Rmd
, that compares different implementations of R code vs a single C++ program. The example uses C++ classes.
Other examples will have a description benchmark
when performing a comparison between R functions and C++ functions.
-
RcppArmadillo
is easier to understand and program than pure C++. It contains a types abstracton that makes it similar to Matlab/Octave. -
Rcpp
can achieve the same results but would take mode development time thanRcppArmadillo
-
RcppArmadillo
has a 3D type calledcube
which is a tensor of the third-level. Not all the matrix functions are implemented for thecube
type. -
RcppArmadillo
allows to write variables directly to files. It is also easy to print to the console usingcout <<
notation -
It is better to code the algorithm first in
R
and the translate it to C++ -
Rcpp
andRcppArmadillo
types can be mixed but some do not carry over exactly well
-
Keep always in mind the array/matrix/vector indices; in C and C++ the index starts at
0
-
C code is called with the function .C()
-
arma-classes-simple-array.Rmd
-
arma-cxxfunction-classes-kalman.Rmd
-
arma-engine-pass_by_ref_by_value.Rmd
-
arma-engine-pass_by_ref_by_value.Rmd
-
arma-engine-simulate_multivariate.Rmd
C++ 11 being used as well in couple of examples.