RCPP Extending
RCPP Extending
This note provides an overview of the steps programmers should follow to # [1] 10
extend Rcpp (Eddelbuettel et al., 2017a; Eddelbuettel and François, 2011)
for use with their own classes. This document is based on our expe- The Rcpp converter function Rcpp::as and Rcpp::wrap have
rience in extending Rcpp to work with the Armadillo (Sanderson, 2010) been designed to be extensible to user-defined types and third-party
classes, available in the separate package RcppArmadillo (Eddelbuettel types.
et al., 2017b). This document assumes knowledge of Rcpp as well as
some knowledge of C++ templates (Abrahams and Gurtovoy, 2004). 2. Extending Rcpp::wrap
Rcpp | extending | R | C++ The Rcpp::wrap converter is extensible in essentially two ways :
intrusive and non-intrusive.
1. Introduction 2.1. Intrusive extension. When extending Rcpp with your own
Rcpp facilitates data interchange between R and C++ through the data type, the recommended way is to implement a conversion to
templated functions Rcpp::as (for conversion of objects from R SEXP. This lets Rcpp::wrap know about the new data type. The
to C++) and Rcpp::wrap (for conversion from C++ to R). In other template meta programming (or TMP) dispatch is able to recognize
words, we convert between the so-called S-expression pointers (in that a type is convertible to a SEXP and Rcpp::wrap will use that
type SEXP) to a templated C++ type, and vice versa. The corre- conversion.
sponding function declarations are as follows: The caveat is that the type must be declared before the main
header file Rcpp.h is included.
// conversion from R to C++
template <typename T> T as(SEXP x); #include <RcppCommon.h>