RcppArmadillo what a mess!

Ok lets try to setup your already under construction R package adding RcppArmadillo to it without the default RStudio project (as anyone suggests online).

Just in case you don’t know, Rcpp is an R package to import C/C++ code in R.
Armadillo is a C++ library for easy Linear Algebra handling.
RcppArmadillo is a package that depends on Rcpp package and enables to import armadillo data structures in R in a very easy way.

First of all, of corse, you have to install the RcppArmadillo package:

after a long installation, we can start to setup our R package.

Create a “src” folder in your package main directory.
Inside this folder you’ll put your C++ files structured as follow :

Each C++ function, to be exported in R, has to be preceded by an exporting declaration, so your cpp file will be something like:

Ok, now you want to document the exported function, but you don’t want to do it by hand (also because you’d lose your hand maded comments at each compilation), so we add some doxygen comments to our functions, in order to be automatically processed by roxigen2.
Note that inserting the “@keywords internal”, just as with roxygen2, you are imposing to not export this function in your R package NAMESPACE.

Ok, now the C++ compiler needs to be instructed to compile your code, Rcpp uses a file called MAKEVARS (for UNIX systems) and MAKEVARS.win for windows:

But when you want to test more than one Unix system, like Linux and OSX, you can fall in some troubles because it seems that this file depends on the system you are compiling your code.
(more in detail, clang++ (the c++ apple compiler), doesn’t support the -lopenmp flag) during the compilation phase)
To solve this problem I followed the instruction of another guy (hoping he will never delete this instructions, because I don’t want to copy and paste his work).
Of course, some instructions have to be changed if you are following my instructions, but you are a fucking coder, so it should be very easy.

I think there is an alternative way to do that, but I didn’t checked yet.
Anyway, I reccomend this CRAN link,  where it’s suggested to install the GNU fortran binaries in order to compile the package and avoid any compiling problem.

For sure following the instructions of that guy and installing the GNU binaries (without setting up in the “CRAN manner” the MAKEVARS file) everything works fine.

Now, if you have the R package “devtools”, using the “document()” or the “load_all()” functions, the code will be compiled and a new RcppExports.cpp file will appear in your src directory and, moreover, the RcppExports.R file will appear in your “R” main folder.
This files contain all the C++ exported functions, and the R file will have also your doxygen comments coverted in roxygen2 style.

At this point you can call your Cpp wrapped function in R with a call like:

The last and most important think to remember when working with matrices in C++ and R is about the indexes.

R has indexes starting from 1.
C/C++ has indexes starting from 0!

So, it’s a good idea, in case you are returning some indexes from C/C++ to R, to check them once they come back in R or before to return them back in R (I prefer the first one, because I like to leave the C/C++ code reusable in case I need it somewhere else).



Lascia un commento

Il tuo indirizzo email non sarà pubblicato. I campi obbligatori sono contrassegnati *