Rcpp: Seamless R and C++ Romain François
[email protected] Joint work with Dirk Eddelbuettel
RMetrics, June 2010, Meielsalp.
Fine for Indiana Jones
Le viaduc de Millau
Background API Sugar Modules
Plat du jour
1
Appetizers : Some background on R and C++
2
Main course : The Rcpp API
3
Desert : Rcpp sugar
4
Coffee : Rcpp modules
Romain François
[email protected]
Seamless R anc C++ integrations, RMetrics 2010
Background API Sugar Modules
R support for C/C++
R is a C program R supports C++ out of the box, just use a .cpp file extension R exposes a API based on low level C functions and MACROS. R provides several calling conventions to invoke compiled code. SEXP foo( SEXP x1, SEXP x2 ){ ...
} > .Call( "foo", 1:10, rnorm(10) )
Romain François
[email protected]
Seamless R anc C++ integrations, RMetrics 2010
Background API Sugar Modules
.Call example #include #include extern "C" SEXP convolve2(SEXP a, SEXP b){ int i, j, na, nb, nab; double *xa, *xb, *xab; SEXP ab; PROTECT(a = AS_NUMERIC(a)); PROTECT(b = AS_NUMERIC(b)); na=LENGTH(a); nb=LENGTH(b); nab=na+nb-1; PROTECT(ab = NEW_NUMERIC(nab)); xa=NUMERIC_POINTER(a); xb=NUMERIC_POINTER(b); xab=NUMERIC_POINTER(ab); for(i=0; iset( "" ) ;
} Romain François
[email protected]
Seamless R anc C++ integrations, RMetrics 2010
Background API Sugar Modules
Modules: expose C++ classes to R
RCPP_MODULE(yada){ using namespace Rcpp ; class_( "World" ) .method( "greet", &World::greet ) .method( "set", &World::set ) .method( "clear", &clearWorld ) ;
}
Romain François
[email protected]
Seamless R anc C++ integrations, RMetrics 2010
Background API Sugar Modules
Modules: on the R side
> w w$greet() [1] "hello" > w$set( "hello world") > w$greet() [1] "hello world" > w$clear() > w$greet() [1] ""
Romain François
[email protected]
Seamless R anc C++ integrations, RMetrics 2010
Want to learn more ? Check the vignettes Questions on the Rcpp-devel mailing list Hands-on training courses Commercial support
Romain François Dirk Eddelbuettel
[email protected] [email protected]