Successfully reported this slideshow.
We use your LinkedIn profile and activity data to personalize ads and to show you more relevant ads. You can change your ad preferences anytime.
R / C++ 
Romain François 
@romain_francois
• Use R since 2002 
• Consultant 
• Rcpp since 2010 
• Rcpp11 since 2013 
• Performance 
• Occasional comedy
useR2015 propaganda 
Please
useR2015 propaganda
C/R API
Rcpp
303 
ABSSeq accelerometry acer AdaptiveSparsity ALKr Amelia apcluster BalancedSampling BayesComm 
wordcloud wsrf XBRL zic ...
dplyr 
dplyr 
dplyr dplyr 
dplyr dplyr 
dplyr 
dplyr 
dplyr 
dplyr 
dplyr 
dplyr 
dplyr 
dplyr 
dplyr 
dplyr 
dplyr 
dplyr...
int add( int a, int b){ 
return a + b ; 
}
#include <Rcpp.h> 
! 
// [[Rcpp::export]] 
int add( int a, int b){ 
return a + b ; 
}
sourceCpp 
#include <Rcpp.h> 
! 
// [[Rcpp::export]] 
int add( int a, int b){ 
return a + b ; 
} 
> sourceCpp( "add.cpp" )...
R/c++ classes 
• NumericVector, IntegerVector, 
CharacterVector, … 
• List 
• Function 
• Environment …
// [[Rcpp::export]] 
double sum( NumericVector x){ 
int n = x.size() ; 
! 
double res = 0.0 ; 
for( int i=0; i<n; i++){ 
r...
List res = List::create( 
_["a"] = 1, 
_["b"] = "foo" 
); 
res.attr( "class" ) = "myclass" ; ! 
int a = res["a"] ; 
res["b...
Function rnorm( "rnorm" ) ; 
! 
NumericVector x = rnorm( 
10, 
_["mean"] = 30, 
_["sd"] = 100 
);
1998
C++114
C++
Range based for loops 
for( int i=0; i<x.size(); i++){ 
double d = x[i] ; 
// do something with d 
} 
for( double d: x){ 
...
auto 
std::vector<double> v = ... ; 
std::vector<double>::const_iterator it = v.begin() ; 
auto it = v.begin() ; 
C++98 
C...
Uniform initialization {…} 
std::vector<std::string> x(2) ; 
x[0] = "foo"; x[1] = "bar" ; 
std::vector<std::string> x = {"...
C++98 typical functor bloat 
class Predicate { 
public: 
Predicate( double y_ ) : y(y_){} 
! 
inline bool operator()(doubl...
C++11 Lambda 
double y = 2.0 ; 
std::count_if( v.begin(), v.end(), 
[=y](double x){ 
return x < y ; 
} 
) ;
Rcpp11
Rcpp11 
• C++ = C++11 
• Smaller code base (than Rcpp) 
• Header only
Less code -> faster compiles 
#include <Rcpp.h> 
! 
// [[Rcpp::export]] 
int foo(){ 
return 2 ; 
} 
Rcpp 
Rcpp11 
0 1 2 3
sapply + lambda 
#include <Rcpp11> 
! 
// [[export]] 
NumericVector foo( NumericVector x ){ 
return sapply(x, [](double d)...
Rcpp11 release cycle 
• Available from CRAN: 
! 
> install.packages( "Rcpp11" ) 
• Evolves quickly. Get it from github 
! ...
Questions 
romain@r-enthusiasts.com 
@romain_francois
R/C++ talk at earl 2014
R/C++ talk at earl 2014
R/C++ talk at earl 2014
R/C++ talk at earl 2014
R/C++ talk at earl 2014
R/C++ talk at earl 2014
Upcoming SlideShare
Loading in …5
×

R/C++ talk at earl 2014

R/C++ talk at EARL 2014. London.

  • Be the first to comment

  • Be the first to like this

R/C++ talk at earl 2014

  1. 1. R / C++ Romain François @romain_francois
  2. 2. • Use R since 2002 • Consultant • Rcpp since 2010 • Rcpp11 since 2013 • Performance • Occasional comedy
  3. 3. useR2015 propaganda Please
  4. 4. useR2015 propaganda
  5. 5. C/R API
  6. 6. Rcpp
  7. 7. 303 ABSSeq accelerometry acer AdaptiveSparsity ALKr Amelia apcluster BalancedSampling BayesComm wordcloud wsrf XBRL zic bayou bcp bcpa bfa bfp bifactorial BIFIEsurvey BIPOD blockcluster btf bvarsv CARBayes CARBayesST ccaPP cda CDM cgAUC CIDnetworks cladoRcpp classify clere climdex.pcic clipper clogitL1 clusteval ClustVarLV CMF COMPASS CompGLM ConConPiWiFun coneproj copCAR CorReg Crossover cxxfunplus dbmss ddgraph Delaporte dendextendRcpp DepthProc DESeq2 devtools dils disclapmix diversitree diveRsity DNAprofiles DNAtools dplyr dslice DStree ecp EDDA eive EpiContactTrace ESGtoolkit EWGoF fastGHQuad FastHCS fastM FastPCS FastRCS FBFsearch fdaMixed FDRreg FisHiCal flip flowMatch flowType flowWorkspace FLSSS forecast fugeR Funclustering fwsim geiger GeneNetworkBuilder GeneticTools geoCount glcm GMCM gMWT GOSemSim GOSim GOsummaries GPvam gRbase GRENITS gRim growcurves growfunctions GSE GUILDS GUTS GxM hawkes HDPenReg HLMdiag hsphase httpuv HUM hyperSpec hypervolume iBATCGH IBHM icamix icensmis inarmix inline IsingSampler iterpc jaatha JAGUAR KernSmoothIRT kmc Kmisc LaF lbfgs lm.br lme4 lsgl marked maxent mcIRT messina metafolio metaSeq mets mice miceadds MIMOSA minqa mirt miscF miscset mkde mmand Morpho mosaics Mposterior MPTinR msgl MSstats MTS mvabund MVB mvnfast mwaved mzR nabor ndl NetSim networkBMA ngspatial NPBayesImpute oem openair openxlsx orQA PBC pcalg pcaMethods PedCNV PerMallows phom phylobase planar plotSEMM plyr polywog PP PReMiuM pROC pRoloc propagate prospectr protViz pryr psgp QRM quadrupen qVarSel randomUniformForest Rankcluster rARPACK Rchemcpp Rclusterpp RcppArmadillo RcppBDT rcppbugs RcppClassic RcppClassicExamples RcppCNPy RcppDE RcppEigen RcppExamples RcppGSL RcppMLPACK RcppOctave RcppParallel RcppProgress RcppRedis RcppRoll RcppSMC RcppXts RcppZiggurat Rdisop recosystem remote ReorderCluster resemble reshape2 revealedPrefs rexpokit rforensicbatwing rgam RInside ripa Risa rkvo rlme RLRsim Rmalschains RMassBank rmgarch Rmixmod RmixmodCombi robustgam robustHD rococo rotations roxygen2 rpg rplexos rPref RProtoBuf RQuantLib RSNNS RSNPset RSofia Rsomoclu rTANDEM Rtsne Ruchardet rugarch Rvcg RVowpalWabbit sapFinder SBSA scrypt sdcMicro sdcTable SelvarMix SeqArray sequences sglOptim simFrame sirt slfm snplist SocialNetworks SOD spacodiR spaMM sparseHessianFD sparseLTSEigen SpatialEpi SpatialTools spBayesSurv StereoMorph stochvol stosim strum survAccuracyMeasures surveillance survSNP synlik TAM TAQMNGR tbart termstrc TFMPvalue treeclim trustOptim tvd unmarked VideoComparison VIM waffect WideLM
  8. 8. dplyr dplyr dplyr dplyr dplyr dplyr dplyr dplyr dplyr dplyr dplyr dplyr dplyr dplyr dplyr dplyr dplyr dplyr dplyr dplyr dplyr dplyr dplyr dplyr dplyr dplyr dplyr dplyr dplyr dplyr dplyr dplyr dplyr dplyr dplyr dplyr dplyr dplyr dplyr dplyr dplyr dplyr dplyr dplyr dplyr dplyr dplyr dplyr dplyr dplyr dplyr dplyr dplyr dplyr dplyr dplyr dplyr dplyr dplyr dplyr dplyr dplyr dplyr dplyr #rcatladies dplyr dplyr dplyr dplyr dplyr dplyr dplyr dplyr dplyr dplyr dplyr dplyr dplyr
  9. 9. int add( int a, int b){ return a + b ; }
  10. 10. #include <Rcpp.h> ! // [[Rcpp::export]] int add( int a, int b){ return a + b ; }
  11. 11. sourceCpp #include <Rcpp.h> ! // [[Rcpp::export]] int add( int a, int b){ return a + b ; } > sourceCpp( "add.cpp" ) > add( 2L, 3L) [1] 5
  12. 12. R/c++ classes • NumericVector, IntegerVector, CharacterVector, … • List • Function • Environment …
  13. 13. // [[Rcpp::export]] double sum( NumericVector x){ int n = x.size() ; ! double res = 0.0 ; for( int i=0; i<n; i++){ res += x[i] ; } ! return res ; }
  14. 14. List res = List::create( _["a"] = 1, _["b"] = "foo" ); res.attr( "class" ) = "myclass" ; ! int a = res["a"] ; res["b"] = 42 ;
  15. 15. Function rnorm( "rnorm" ) ; ! NumericVector x = rnorm( 10, _["mean"] = 30, _["sd"] = 100 );
  16. 16. 1998
  17. 17. C++114
  18. 18. C++
  19. 19. Range based for loops for( int i=0; i<x.size(); i++){ double d = x[i] ; // do something with d } for( double d: x){ // do something with d } C++11 C++98
  20. 20. auto std::vector<double> v = ... ; std::vector<double>::const_iterator it = v.begin() ; auto it = v.begin() ; C++98 C++11
  21. 21. Uniform initialization {…} std::vector<std::string> x(2) ; x[0] = "foo"; x[1] = "bar" ; std::vector<std::string> x = {"foo", "bar"} ;
  22. 22. C++98 typical functor bloat class Predicate { public: Predicate( double y_ ) : y(y_){} ! inline bool operator()(double x){ return x < y ; } ! private: double y ; } ; ... ! double y = 2.0 ; std::count_if( v.begin(), v.end(), Predicate(y)) ;
  23. 23. C++11 Lambda double y = 2.0 ; std::count_if( v.begin(), v.end(), [=y](double x){ return x < y ; } ) ;
  24. 24. Rcpp11
  25. 25. Rcpp11 • C++ = C++11 • Smaller code base (than Rcpp) • Header only
  26. 26. Less code -> faster compiles #include <Rcpp.h> ! // [[Rcpp::export]] int foo(){ return 2 ; } Rcpp Rcpp11 0 1 2 3
  27. 27. sapply + lambda #include <Rcpp11> ! // [[export]] NumericVector foo( NumericVector x ){ return sapply(x, [](double d){ return exp(d * 2) ; }); } x <- 1:3 sapply( x, function(d){ exp(d*2) ; })
  28. 28. Rcpp11 release cycle • Available from CRAN: ! > install.packages( "Rcpp11" ) • Evolves quickly. Get it from github ! > install_github( "Rcpp11/Rcpp11" ) • Current Version: 3.1.1 (same as R version)
  29. 29. Questions romain@r-enthusiasts.com @romain_francois

×