Successfully reported this slideshow.
Upcoming SlideShare
×

# R/C++

R / C++ talk at eRum2016

• Full Name
Comment goes here.

Are you sure you want to Yes No
• Be the first to comment

• Be the first to like this

### R/C++

1. 1. romain@purrple.cat @romain_francois R / C++Romain FRANÇOIS #eRum2016
5. 5. *.h 94390 *.cpp 5575 99965 *.R 3134
6. 6. motivation
7. 7. int add( int a, int b ){ return a + b ; }
8. 8. C/R API
9. 9. int add( int a, int b ){ return a + b ; } #include <R.h> #include <Rinternals.h> SEXP add_fun( SEXP a_, SEXP b_ ){ int a = INTEGER(a_)[0], b = INTEGER(b_)[0] ; int res = add( a, b ) ; SEXP result = PROTECT(allocVector(INTSXP, 1) ) ; INTEGER(result)[0] = res ; UNPROTECT(1) ; return result ; }
11. 11. Rcpp
12. 12. #include <Rcpp.h> // [[Rcpp::export]] int add( int a, int b ){ return a + b ; } > sourceCpp( "add.cpp" ) > add( 17L, 25L ) [1] 42 > add( 17, 25 ) [1] 42
13. 13. #include <Rcpp.h> using namespace Rcpp // [[Rcpp::export]] double sum_cpp( NumericVector x){ int n = x.size() ; double res = 0.0 ; for( int i=0; i<n; i++){ res += x[i] ; } return res ; }
14. 14. sugar
15. 15. pdistR <- function(x, ys) { sqrt((x - ys) ^ 2) }
16. 16. sugar NumericVector pdistCpp2( double x, NumericVector ys){ return sqrt( pow( x - ys, 2 ) ) ; } NumericVector pdistCpp( double x, NumericVector ys) { int n = ys.size() ; NumericVector res(n) ; for(int i=0; i<n; i++){ res[i] = sqrt( pow( x-ys[i], 2.0 ) ; } return res ; }
17. 17. modules
18. 18. class Kittens { public: Kittens( int n_ ) : n(n_) ; void meow() { ... }; void purr() { ... }; int n ; } ; RCPP_MODULE(Cats){ class_<Kittens>( "Kittens" ) .constructor<int>() .field( "n", &Kittens::n ) .method( "meow", &Kittens::meow ) .method( "purr", &Kittens::purr ) ; } kittens <- new( Kittens, 10 ) kittens\$n kittens\$meow() kittens\$purr()
19. 19. dependency problem
20. 20. Rcpp n + 2 core sugar modules core modules mypkg developper Rcpp n +1 core sugar modules core modules Rcpp n core sugar modules core modules cran mypkg
21. 21. Rcpp n + 2 core sugar modules core modules mypkg developper Rcpp n +1 core sugar modules core modules Rcpp n core sugar modules core modules cran user1 mypkg 🔥
22. 22. Rcpp n + 2 core sugar modules core modules mypkg developper Rcpp n +1 core sugar modules core modules Rcpp n core sugar modules core modules cran user1 user2 mypkg 🔥 ✔
23. 23. Rcpp n + 2 core sugar modules core modules mypkg developper Rcpp n +1 core sugar modules core modules Rcpp n core sugar modules core modules cran user1 user2 mypkg user3 🔥 ✔ 🔥
24. 24. proposal
25. 25. • C++ = C++11 • header only / no runtime dependency • lightweight & modular Rcpp11
26. 26. Rcpp11 Rcpplightweight
27. 27. Rcpp11 sugar modules ... ... modular
28. 28. Rcpp11 sugar modules Rcpp11 sugar modules Rcpp11 sugar modules mypkg2 Rcpp11 mypkg1 Rcpp11 sugardev1 dev2
29. 29. why C++11
30. 30. uniform initialization NumericVector x = { 1.0, 2.3, 3.3 } ;
31. 31. for double sum( NumericVector x ){ double res = 0.0 ; for( double d: x) res += d ; return res ; }
32. 32. lambdas NumericVector pdist( double x, NumericVector ys ){ return sapply( ys, [&x]( double y ){ return sqrt( pow(x-y, 2.0) ) ; }) ; }
33. 33. auto NumericVector pdist( double x, NumericVector ys ){ auto fun = [&x]( double y ){ return sqrt( pow(x-y, 2.0) ) ; } ; return sapply( ys, fun) ; }
34. 34. romain@purrple.cat @romain_francois ?