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 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
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
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" ) 
> add( 2L, 3L) 
[1] 5
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++){ 
res += x[i] ; 
} 
! 
return res ; 
}
List res = List::create( 
_["a"] = 1, 
_["b"] = "foo" 
); 
res.attr( "class" ) = "myclass" ; ! 
int a = res["a"] ; 
res["b"] = 42 ;
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){ 
// do something with d 
} 
C++11 C++98
auto 
std::vector<double> v = ... ; 
std::vector<double>::const_iterator it = v.begin() ; 
auto it = v.begin() ; 
C++98 
C++11
Uniform initialization {…} 
std::vector<std::string> x(2) ; 
x[0] = "foo"; x[1] = "bar" ; 
std::vector<std::string> x = {"foo", "bar"} ;
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)) ;
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){ 
return exp(d * 2) ; 
}); 
} 
x <- 1:3 
sapply( x, function(d){ 
exp(d*2) ; 
})
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)
Questions 
romain@r-enthusiasts.com 
@romain_francois

R/C++ talk at earl 2014

  • 1.
    R / C++ Romain François @romain_francois
  • 2.
    • Use Rsince 2002 • Consultant • Rcpp since 2010 • Rcpp11 since 2013 • Performance • Occasional comedy
  • 3.
  • 4.
  • 5.
  • 6.
  • 9.
    303 ABSSeq accelerometryacer 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
  • 10.
    dplyr dplyr dplyrdplyr 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
  • 11.
    int add( inta, int b){ return a + b ; }
  • 13.
    #include <Rcpp.h> ! // [[Rcpp::export]] int add( int a, int b){ return a + b ; }
  • 14.
    sourceCpp #include <Rcpp.h> ! // [[Rcpp::export]] int add( int a, int b){ return a + b ; } > sourceCpp( "add.cpp" ) > add( 2L, 3L) [1] 5
  • 15.
    R/c++ classes •NumericVector, IntegerVector, CharacterVector, … • List • Function • Environment …
  • 16.
    // [[Rcpp::export]] doublesum( NumericVector x){ int n = x.size() ; ! double res = 0.0 ; for( int i=0; i<n; i++){ res += x[i] ; } ! return res ; }
  • 17.
    List res =List::create( _["a"] = 1, _["b"] = "foo" ); res.attr( "class" ) = "myclass" ; ! int a = res["a"] ; res["b"] = 42 ;
  • 18.
    Function rnorm( "rnorm") ; ! NumericVector x = rnorm( 10, _["mean"] = 30, _["sd"] = 100 );
  • 19.
  • 22.
  • 23.
  • 24.
    Range based forloops 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
  • 25.
    auto std::vector<double> v= ... ; std::vector<double>::const_iterator it = v.begin() ; auto it = v.begin() ; C++98 C++11
  • 26.
    Uniform initialization {…} std::vector<std::string> x(2) ; x[0] = "foo"; x[1] = "bar" ; std::vector<std::string> x = {"foo", "bar"} ;
  • 27.
    C++98 typical functorbloat 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)) ;
  • 28.
    C++11 Lambda doubley = 2.0 ; std::count_if( v.begin(), v.end(), [=y](double x){ return x < y ; } ) ;
  • 29.
  • 30.
    Rcpp11 • C++= C++11 • Smaller code base (than Rcpp) • Header only
  • 32.
    Less code ->faster compiles #include <Rcpp.h> ! // [[Rcpp::export]] int foo(){ return 2 ; } Rcpp Rcpp11 0 1 2 3
  • 33.
    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) ; })
  • 34.
    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)
  • 35.