r/c++ talk at earl 2014

35
R / C++ Romain François @romain_francois

Upload: romain-francois

Post on 25-Jun-2015

562 views

Category:

Technology


2 download

DESCRIPTION

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

TRANSCRIPT

Page 1: R/C++ talk at earl 2014

R / C++

Romain François @romain_francois

Page 2: R/C++ talk at earl 2014

• Use R since 2002

• Consultant

• Rcpp since 2010

• Rcpp11 since 2013

• Performance

• Occasional comedy

Page 3: R/C++ talk at earl 2014

useR2015 propaganda

Please

Page 4: R/C++ talk at earl 2014

useR2015 propaganda

Page 5: R/C++ talk at earl 2014

C/R API

Page 6: R/C++ talk at earl 2014

Rcpp

Page 7: R/C++ talk at earl 2014
Page 8: R/C++ talk at earl 2014
Page 9: R/C++ talk at earl 2014

ABSSeq accelerometry acer AdaptiveSparsity ALKr Amelia apcluster BalancedSampling BayesComm 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

wordcloud wsrf XBRL zic

303

Page 10: R/C++ talk at earl 2014

dplyrdplyr

dplyrdplyr

dplyr

dplyr

dplyr

dplyr

dplyr dplyr

dplyr

dplyr

dplyr

dplyr

dplyr

dplyr

dplyr

dplyr dplyrdplyr

dplyr

dplyrdplyr

dplyr

dplyr

dplyr

dplyr

dplyr

dplyrdplyr

dplyr

dplyrdplyr

dplyrdplyrdplyr

dplyr

dplyr

dplyrdplyr

dplyr

dplyrdplyr

dplyr

dplyr

dplyrdplyr

dplyrdplyr

dplyr

dplyr

dplyr dplyr

dplyr

dplyr

dplyrdplyrdplyr

#rcatladiesdplyrdplyr

dplyr

dplyrdplyr

dplyr

dplyrdplyr

dplyrdplyrdplyr

dplyrdplyr

dplyrdplyrdplyr

dplyrdplyr

dplyr

Page 11: R/C++ talk at earl 2014

int add( int a, int b){ return a + b ; }

Page 12: R/C++ talk at earl 2014
Page 13: R/C++ talk at earl 2014

#include <Rcpp.h> !

// [[Rcpp::export]] int add( int a, int b){ return a + b ; }

Page 14: R/C++ talk at earl 2014

sourceCpp#include <Rcpp.h> !// [[Rcpp::export]] int add( int a, int b){ return a + b ; }

> sourceCpp( "add.cpp" ) > add( 2L, 3L) [1] 5

Page 15: R/C++ talk at earl 2014

R/c++ classes

• NumericVector, IntegerVector, CharacterVector, …

• List

• Function

• Environment …

Page 16: R/C++ talk at earl 2014

// [[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 ; }

Page 17: R/C++ talk at earl 2014

List res = List::create( _["a"] = 1, _["b"] = "foo" );

res.attr( "class" ) = "myclass" ; !int a = res["a"] ;res["b"] = 42 ;

Page 18: R/C++ talk at earl 2014

Function rnorm( "rnorm" ) ; !

NumericVector x = rnorm( 10, _["mean"] = 30, _["sd"] = 100 );

Page 19: R/C++ talk at earl 2014

1998

Page 20: R/C++ talk at earl 2014
Page 21: R/C++ talk at earl 2014
Page 22: R/C++ talk at earl 2014

C++1 14

Page 23: R/C++ talk at earl 2014

C++

Page 24: R/C++ talk at earl 2014

Range based for loopsfor( int i=0; i<x.size(); i++){ double d = x[i] ; // do something with d }

for( double d: x){ // do something with d }

C++

98C

++11

Page 25: R/C++ talk at earl 2014

autostd::vector<double> v = ... ;

std::vector<double>::const_iterator it = v.begin() ;

auto it = v.begin() ;

C++98

C++11

Page 26: R/C++ talk at earl 2014

Uniform initialization {…}

std::vector<std::string> x(2) ; x[0] = "foo"; x[1] = "bar" ;

std::vector<std::string> x = {"foo", "bar"} ;

Page 27: R/C++ talk at earl 2014

C++98 typical functor bloatclass 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)) ;

Page 28: R/C++ talk at earl 2014

C++11 Lambda

double y = 2.0 ; std::count_if( v.begin(), v.end(), [=y](double x){ return x < y ; } ) ;

Page 29: R/C++ talk at earl 2014

Rcpp11

Page 30: R/C++ talk at earl 2014

Rcpp11

• C++ = C++11

• Smaller code base (than Rcpp)

• Header only

Page 31: R/C++ talk at earl 2014
Page 32: R/C++ talk at earl 2014

#include <Rcpp.h> !// [[Rcpp::export]] int foo(){ return 2 ; }

Rcpp

Rcpp11

0 1 2 3

Less code -> faster compiles

Page 33: R/C++ talk at earl 2014

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) ; })

Page 34: R/C++ talk at earl 2014

Rcpp11 release cycle

• Available from CRAN:

!

• Evolves quickly. Get it from github

!

• Current Version: 3.1.1 (same as R version)

> install.packages( "Rcpp11" )

> install_github( "Rcpp11/Rcpp11" )

Page 35: R/C++ talk at earl 2014

Questions

[email protected]@romain_francois