Seamless R Extensions using Rcpp and RInside Dirk Eddelbuettel - - PowerPoint PPT Presentation

seamless r extensions using rcpp and rinside
SMART_READER_LITE
LIVE PREVIEW

Seamless R Extensions using Rcpp and RInside Dirk Eddelbuettel - - PowerPoint PPT Presentation

Extending R Rcpp Examples Summary Seamless R Extensions using Rcpp and RInside Dirk Eddelbuettel Debian & R Joint work with Romain Franois Presentation on March 30, 2010 to UCLA Department of Statistics (3pm) Los Angeles R Users


slide-1
SLIDE 1

Extending R Rcpp Examples Summary

Seamless R Extensions using Rcpp and RInside

Dirk Eddelbuettel

Debian & R

Joint work with Romain François

Presentation on March 30, 2010 to

UCLA Department of Statistics (3pm) Los Angeles R Users Group (6pm)

Dirk Eddelbuettel Seamless R Extensions using Rcpp + RInside

slide-2
SLIDE 2

Extending R Rcpp Examples Summary Why ? The standard API Inline

Outline

1

Extending R Why ? The standard API Inline

2

Rcpp Overview New API Examples

3

Rcpp Usage Examples RInside Others

4

Summary Key points Resources

Dirk Eddelbuettel Seamless R Extensions using Rcpp + RInside

slide-3
SLIDE 3

Extending R Rcpp Examples Summary Why ? The standard API Inline

Outline

1

Extending R Why ? The standard API Inline

2

Rcpp Overview New API Examples

3

Rcpp Usage Examples RInside Others

4

Summary Key points Resources

Dirk Eddelbuettel Seamless R Extensions using Rcpp + RInside

slide-4
SLIDE 4

Extending R Rcpp Examples Summary Why ? The standard API Inline

Motivation

  • Chambers. Software for

Data Analysis: Programming with R. Springer, 2008 Dirk Eddelbuettel Seamless R Extensions using Rcpp + RInside

slide-5
SLIDE 5

Extending R Rcpp Examples Summary Why ? The standard API Inline

Motivation

  • Chambers. Software for

Data Analysis: Programming with R. Springer, 2008

Chambers (2008) opens chapter 11 (Interfaces I: Using C and Fortran) with these words:

Dirk Eddelbuettel Seamless R Extensions using Rcpp + RInside

slide-6
SLIDE 6

Extending R Rcpp Examples Summary Why ? The standard API Inline

Motivation

  • Chambers. Software for

Data Analysis: Programming with R. Springer, 2008

Chambers (2008) opens chapter 11 (Interfaces I: Using C and Fortran) with these words:

Since the core of R is in fact a program written in the C language, it’s not surprising that the most direct interface to non-R software is for code written in C, or directly callable from C. All the same, including additional C code is a serious step, with some added dangers and often a substantial amount of programming and debugging

  • required. You should have a good reason.

Dirk Eddelbuettel Seamless R Extensions using Rcpp + RInside

slide-7
SLIDE 7

Extending R Rcpp Examples Summary Why ? The standard API Inline

Motivation

Chambers (2008) then proceeds with this rough map of the road ahead:

Dirk Eddelbuettel Seamless R Extensions using Rcpp + RInside

slide-8
SLIDE 8

Extending R Rcpp Examples Summary Why ? The standard API Inline

Motivation

Chambers (2008) then proceeds with this rough map of the road ahead: Against:

Dirk Eddelbuettel Seamless R Extensions using Rcpp + RInside

slide-9
SLIDE 9

Extending R Rcpp Examples Summary Why ? The standard API Inline

Motivation

Chambers (2008) then proceeds with this rough map of the road ahead: Against: It’s more work

Dirk Eddelbuettel Seamless R Extensions using Rcpp + RInside

slide-10
SLIDE 10

Extending R Rcpp Examples Summary Why ? The standard API Inline

Motivation

Chambers (2008) then proceeds with this rough map of the road ahead: Against: It’s more work Bugs will bite

Dirk Eddelbuettel Seamless R Extensions using Rcpp + RInside

slide-11
SLIDE 11

Extending R Rcpp Examples Summary Why ? The standard API Inline

Motivation

Chambers (2008) then proceeds with this rough map of the road ahead: Against: It’s more work Bugs will bite Potential platform dependency

Dirk Eddelbuettel Seamless R Extensions using Rcpp + RInside

slide-12
SLIDE 12

Extending R Rcpp Examples Summary Why ? The standard API Inline

Motivation

Chambers (2008) then proceeds with this rough map of the road ahead: Against: It’s more work Bugs will bite Potential platform dependency Less readable software

Dirk Eddelbuettel Seamless R Extensions using Rcpp + RInside

slide-13
SLIDE 13

Extending R Rcpp Examples Summary Why ? The standard API Inline

Motivation

Chambers (2008) then proceeds with this rough map of the road ahead: Against: It’s more work Bugs will bite Potential platform dependency Less readable software

Dirk Eddelbuettel Seamless R Extensions using Rcpp + RInside

slide-14
SLIDE 14

Extending R Rcpp Examples Summary Why ? The standard API Inline

Motivation

Chambers (2008) then proceeds with this rough map of the road ahead: Against: It’s more work Bugs will bite Potential platform dependency Less readable software In Favor: New and trusted computations

Dirk Eddelbuettel Seamless R Extensions using Rcpp + RInside

slide-15
SLIDE 15

Extending R Rcpp Examples Summary Why ? The standard API Inline

Motivation

Chambers (2008) then proceeds with this rough map of the road ahead: Against: It’s more work Bugs will bite Potential platform dependency Less readable software In Favor: New and trusted computations Speed

Dirk Eddelbuettel Seamless R Extensions using Rcpp + RInside

slide-16
SLIDE 16

Extending R Rcpp Examples Summary Why ? The standard API Inline

Motivation

Chambers (2008) then proceeds with this rough map of the road ahead: Against: It’s more work Bugs will bite Potential platform dependency Less readable software In Favor: New and trusted computations Speed Object references

Dirk Eddelbuettel Seamless R Extensions using Rcpp + RInside

slide-17
SLIDE 17

Extending R Rcpp Examples Summary Why ? The standard API Inline

Motivation

Chambers (2008) then proceeds with this rough map of the road ahead: Against: It’s more work Bugs will bite Potential platform dependency Less readable software In Favor: New and trusted computations Speed Object references

Dirk Eddelbuettel Seamless R Extensions using Rcpp + RInside

slide-18
SLIDE 18

Extending R Rcpp Examples Summary Why ? The standard API Inline

Motivation

Chambers (2008) then proceeds with this rough map of the road ahead: Against: It’s more work Bugs will bite Potential platform dependency Less readable software In Favor: New and trusted computations Speed Object references So is the deck stacked against us?

Dirk Eddelbuettel Seamless R Extensions using Rcpp + RInside

slide-19
SLIDE 19

Extending R Rcpp Examples Summary Why ? The standard API Inline

Outline

1

Extending R Why ? The standard API Inline

2

Rcpp Overview New API Examples

3

Rcpp Usage Examples RInside Others

4

Summary Key points Resources

Dirk Eddelbuettel Seamless R Extensions using Rcpp + RInside

slide-20
SLIDE 20

Extending R Rcpp Examples Summary Why ? The standard API Inline

Compiled Code: The Basics

R offers several functions to access compiled code: we focus

  • n .C and .Call here. (R Extensions, sections 5.2 and 5.9;

Software for Data Analysis).

Dirk Eddelbuettel Seamless R Extensions using Rcpp + RInside

slide-21
SLIDE 21

Extending R Rcpp Examples Summary Why ? The standard API Inline

Compiled Code: The Basics

R offers several functions to access compiled code: we focus

  • n .C and .Call here. (R Extensions, sections 5.2 and 5.9;

Software for Data Analysis). The canonical example is the convolution function:

1 void convolve ( double ∗a ,

int ∗na , double ∗b ,

2

int ∗nb , double ∗ab )

3 { 4

int i , j , nab = ∗na + ∗nb − 1;

5 6

for ( i = 0; i < nab ; i ++)

7

ab [ i ] = 0.0;

8

for ( i = 0; i < ∗na ; i ++)

9

for ( j = 0; j < ∗nb ; j ++)

10

ab [ i + j ] += a [ i ] ∗ b [ j ] ;

11 } Dirk Eddelbuettel Seamless R Extensions using Rcpp + RInside

slide-22
SLIDE 22

Extending R Rcpp Examples Summary Why ? The standard API Inline

Compiled Code: The Basics cont.

The convolution function is called from R by

Dirk Eddelbuettel Seamless R Extensions using Rcpp + RInside

slide-23
SLIDE 23

Extending R Rcpp Examples Summary Why ? The standard API Inline

Compiled Code: The Basics cont.

The convolution function is called from R by

1 conv <− function (a , b ) 2

.C( " convolve " ,

3

as . double ( a ) ,

4

as . integer ( length ( a ) ) ,

5

as . double ( b ) ,

6

as . integer ( length ( b ) ) ,

7

ab = double ( length ( a ) + length ( b ) − 1) ) $ab

As stated in the manual, one must take care to coerce all the arguments to the correct R storage mode before calling .C as mistakes in matching the types can lead to wrong results or hard-to-catch errors.

Dirk Eddelbuettel Seamless R Extensions using Rcpp + RInside

slide-24
SLIDE 24

Extending R Rcpp Examples Summary Why ? The standard API Inline

Compiled Code: The Basics cont.

Using .Call, the example becomes

Dirk Eddelbuettel Seamless R Extensions using Rcpp + RInside

slide-25
SLIDE 25

Extending R Rcpp Examples Summary Why ? The standard API Inline

Compiled Code: The Basics cont.

Using .Call, the example becomes

1 #include <R. h> 2 #include <Rdefines . h> 3 4 extern "C" SEXP convolve2 (SEXP a , SEXP b ) 5 { 6 int i , j , na , nb , nab ; 7 double ∗xa , ∗xb , ∗xab ; 8 SEXP ab ; 9 10 PROTECT( a = AS_NUMERIC( a ) ) ; 11 PROTECT( b = AS_NUMERIC( b ) ) ; 12 na = LENGTH( a ) ; nb = LENGTH( b ) ; nab = na + nb − 1; 13 PROTECT( ab = NEW_NUMERIC( nab ) ) ; 14 xa = NUMERIC_POINTER( a ) ; xb = NUMERIC_POINTER( b ) ; 15 xab = NUMERIC_POINTER( ab ) ; 16 for ( i = 0; i < nab ; i ++) xab [ i ] = 0.0; 17 for ( i = 0; i < na ; i ++) 18 for ( j = 0; j < nb ; j ++) xab [ i + j ] += xa [ i ] ∗ xb [ j ] ; 19 UNPROTECT(3) ; 20 return ( ab ) ; 21 } Dirk Eddelbuettel Seamless R Extensions using Rcpp + RInside

slide-26
SLIDE 26

Extending R Rcpp Examples Summary Why ? The standard API Inline

Compiled Code: The Basics cont.

Now the call simplifies to just the function name and the vector arguments—all other handling is done at the C/C++ level:

1 conv <− function (a , b )

. Call ( " convolve2 " , a , b )

Dirk Eddelbuettel Seamless R Extensions using Rcpp + RInside

slide-27
SLIDE 27

Extending R Rcpp Examples Summary Why ? The standard API Inline

Compiled Code: The Basics cont.

Now the call simplifies to just the function name and the vector arguments—all other handling is done at the C/C++ level:

1 conv <− function (a , b )

. Call ( " convolve2 " , a , b )

In summary, we see that there are different entry points

Dirk Eddelbuettel Seamless R Extensions using Rcpp + RInside

slide-28
SLIDE 28

Extending R Rcpp Examples Summary Why ? The standard API Inline

Compiled Code: The Basics cont.

Now the call simplifies to just the function name and the vector arguments—all other handling is done at the C/C++ level:

1 conv <− function (a , b )

. Call ( " convolve2 " , a , b )

In summary, we see that there are different entry points using different calling conventions

Dirk Eddelbuettel Seamless R Extensions using Rcpp + RInside

slide-29
SLIDE 29

Extending R Rcpp Examples Summary Why ? The standard API Inline

Compiled Code: The Basics cont.

Now the call simplifies to just the function name and the vector arguments—all other handling is done at the C/C++ level:

1 conv <− function (a , b )

. Call ( " convolve2 " , a , b )

In summary, we see that there are different entry points using different calling conventions leading to code that may need to do more work at the lower level.

Dirk Eddelbuettel Seamless R Extensions using Rcpp + RInside

slide-30
SLIDE 30

Extending R Rcpp Examples Summary Why ? The standard API Inline

Outline

1

Extending R Why ? The standard API Inline

2

Rcpp Overview New API Examples

3

Rcpp Usage Examples RInside Others

4

Summary Key points Resources

Dirk Eddelbuettel Seamless R Extensions using Rcpp + RInside

slide-31
SLIDE 31

Extending R Rcpp Examples Summary Why ? The standard API Inline

Compiled Code: inline

inline is a package by Oleg Sklyar et al that provides the function cfunction which can wrap Fortran, C or C++ code.

Dirk Eddelbuettel Seamless R Extensions using Rcpp + RInside

slide-32
SLIDE 32

Extending R Rcpp Examples Summary Why ? The standard API Inline

Compiled Code: inline

inline is a package by Oleg Sklyar et al that provides the function cfunction which can wrap Fortran, C or C++ code.

1 ## A simple

Fortran example

2 code <− " 3

integer i

4

do 1 i =1 , n (1)

5

1 x ( i ) = x ( i ) ∗∗3

6 " 7 cubefn <− cfunction ( signature ( n=" integer " , x=" numeric " ) , 8

code , convention=" . Fortran " )

9 x <− as . numeric (1:10) 10 n <− as . integer (10) 11 cubefn (n ,

x ) $x

Dirk Eddelbuettel Seamless R Extensions using Rcpp + RInside

slide-33
SLIDE 33

Extending R Rcpp Examples Summary Why ? The standard API Inline

Compiled Code: inline

inline is a package by Oleg Sklyar et al that provides the function cfunction which can wrap Fortran, C or C++ code.

1 ## A simple

Fortran example

2 code <− " 3

integer i

4

do 1 i =1 , n (1)

5

1 x ( i ) = x ( i ) ∗∗3

6 " 7 cubefn <− cfunction ( signature ( n=" integer " , x=" numeric " ) , 8

code , convention=" . Fortran " )

9 x <− as . numeric (1:10) 10 n <− as . integer (10) 11 cubefn (n ,

x ) $x

cfunction takes care of compiling, linking, loading, . . . by placing the resulting dynamically-loadable object code in the per-session temporary directory used by R.

Dirk Eddelbuettel Seamless R Extensions using Rcpp + RInside

slide-34
SLIDE 34

Extending R Rcpp Examples Summary Overview New API Examples

Outline

1

Extending R Why ? The standard API Inline

2

Rcpp Overview New API Examples

3

Rcpp Usage Examples RInside Others

4

Summary Key points Resources

Dirk Eddelbuettel Seamless R Extensions using Rcpp + RInside

slide-35
SLIDE 35

Extending R Rcpp Examples Summary Overview New API Examples

Outline

1

Extending R Why ? The standard API Inline

2

Rcpp Overview New API Examples

3

Rcpp Usage Examples RInside Others

4

Summary Key points Resources

Dirk Eddelbuettel Seamless R Extensions using Rcpp + RInside

slide-36
SLIDE 36

Extending R Rcpp Examples Summary Overview New API Examples

Compiled Code: Rcpp

In a nutshell:

Dirk Eddelbuettel Seamless R Extensions using Rcpp + RInside

slide-37
SLIDE 37

Extending R Rcpp Examples Summary Overview New API Examples

Compiled Code: Rcpp

In a nutshell: Rcpp makes it easier to interface C++ and R code.

Dirk Eddelbuettel Seamless R Extensions using Rcpp + RInside

slide-38
SLIDE 38

Extending R Rcpp Examples Summary Overview New API Examples

Compiled Code: Rcpp

In a nutshell: Rcpp makes it easier to interface C++ and R code. Using the .Call interface, we can use features of the C++ language to automate the tedious bits of the macro-based C-level interface to R.

Dirk Eddelbuettel Seamless R Extensions using Rcpp + RInside

slide-39
SLIDE 39

Extending R Rcpp Examples Summary Overview New API Examples

Compiled Code: Rcpp

In a nutshell: Rcpp makes it easier to interface C++ and R code. Using the .Call interface, we can use features of the C++ language to automate the tedious bits of the macro-based C-level interface to R. One major advantage of using .Call is that richer R

  • bjects (vectors, matrices, lists, . . . in fact most SEXP

types incl functions, environments etc) can be passed directly between R and C++ without the need for explicit passing of dimension arguments.

Dirk Eddelbuettel Seamless R Extensions using Rcpp + RInside

slide-40
SLIDE 40

Extending R Rcpp Examples Summary Overview New API Examples

Compiled Code: Rcpp

In a nutshell: Rcpp makes it easier to interface C++ and R code. Using the .Call interface, we can use features of the C++ language to automate the tedious bits of the macro-based C-level interface to R. One major advantage of using .Call is that richer R

  • bjects (vectors, matrices, lists, . . . in fact most SEXP

types incl functions, environments etc) can be passed directly between R and C++ without the need for explicit passing of dimension arguments. By using the C++ class layers, we do not need to manipulate the SEXP objects using any of the old-school C macros.

Dirk Eddelbuettel Seamless R Extensions using Rcpp + RInside

slide-41
SLIDE 41

Extending R Rcpp Examples Summary Overview New API Examples

Compiled Code: Rcpp

In a nutshell: Rcpp makes it easier to interface C++ and R code. Using the .Call interface, we can use features of the C++ language to automate the tedious bits of the macro-based C-level interface to R. One major advantage of using .Call is that richer R

  • bjects (vectors, matrices, lists, . . . in fact most SEXP

types incl functions, environments etc) can be passed directly between R and C++ without the need for explicit passing of dimension arguments. By using the C++ class layers, we do not need to manipulate the SEXP objects using any of the old-school C macros. inline eases usage, development and testing.

Dirk Eddelbuettel Seamless R Extensions using Rcpp + RInside

slide-42
SLIDE 42

Extending R Rcpp Examples Summary Overview New API Examples

Rcpp example

The convolution example can be rewritten in the ’Classic API’:

Dirk Eddelbuettel Seamless R Extensions using Rcpp + RInside

slide-43
SLIDE 43

Extending R Rcpp Examples Summary Overview New API Examples

Rcpp example

The convolution example can be rewritten in the ’Classic API’:

1 #include <Rcpp . h> 2 3 RcppExport SEXP convolve_cpp (SEXP a , SEXP b ) 4 { 5 RcppVector<double> xa ( a ) ; 6 RcppVector<double> xb ( b ) ; 7 8 int nab = xa . size ( ) + xb . size ( ) − 1; 9 10 RcppVector<double> xab ( nab ) ; 11 for ( int i = 0; i < nab ; i ++) xab ( i ) = 0.0; 12 13 for ( int i = 0; i < xa . size ( ) ; i ++) 14 for ( int j = 0; j < xb . size ( ) ; j ++) 15 xab ( i + j ) += xa ( i ) ∗ xb ( j ) ; 16 17 RcppResultSet rs ; 18 rs . add ( " ab " , xab ) ; 19 return rs . getReturnList ( ) ; 20 } Dirk Eddelbuettel Seamless R Extensions using Rcpp + RInside

slide-44
SLIDE 44

Extending R Rcpp Examples Summary Overview New API Examples

Outline

1

Extending R Why ? The standard API Inline

2

Rcpp Overview New API Examples

3

Rcpp Usage Examples RInside Others

4

Summary Key points Resources

Dirk Eddelbuettel Seamless R Extensions using Rcpp + RInside

slide-45
SLIDE 45

Extending R Rcpp Examples Summary Overview New API Examples

Rcpp: The ’New API’

Rcpp was significantly extended over the last few months to permit more natural expressions. Consider this comparison between the R API and the new Rcpp API:

Dirk Eddelbuettel Seamless R Extensions using Rcpp + RInside

slide-46
SLIDE 46

Extending R Rcpp Examples Summary Overview New API Examples

Rcpp: The ’New API’

Rcpp was significantly extended over the last few months to permit more natural expressions. Consider this comparison between the R API and the new Rcpp API:

1 SEXP ab ; 2 PROTECT( ab = allocVector (STRSXP, 2) ) ; 3 SET_STRING_ELT( ab , 0 , mkChar( " foo " ) ) ; 4 SET_STRING_ELT( ab , 1 , mkChar( " bar " ) ) ; 5 UNPROTECT(1) ; Dirk Eddelbuettel Seamless R Extensions using Rcpp + RInside

slide-47
SLIDE 47

Extending R Rcpp Examples Summary Overview New API Examples

Rcpp: The ’New API’

Rcpp was significantly extended over the last few months to permit more natural expressions. Consider this comparison between the R API and the new Rcpp API:

1 SEXP ab ; 2 PROTECT( ab = allocVector (STRSXP, 2) ) ; 3 SET_STRING_ELT( ab , 0 , mkChar( " foo " ) ) ; 4 SET_STRING_ELT( ab , 1 , mkChar( " bar " ) ) ; 5 UNPROTECT(1) ; 1 CharacterVector ab (2) ; 2 ab [ 0 ] = " foo " ; 3 ab [ 1 ] = " bar " ; Dirk Eddelbuettel Seamless R Extensions using Rcpp + RInside

slide-48
SLIDE 48

Extending R Rcpp Examples Summary Overview New API Examples

Rcpp: The ’New API’

Rcpp was significantly extended over the last few months to permit more natural expressions. Consider this comparison between the R API and the new Rcpp API:

1 SEXP ab ; 2 PROTECT( ab = allocVector (STRSXP, 2) ) ; 3 SET_STRING_ELT( ab , 0 , mkChar( " foo " ) ) ; 4 SET_STRING_ELT( ab , 1 , mkChar( " bar " ) ) ; 5 UNPROTECT(1) ; 1 CharacterVector ab (2) ; 2 ab [ 0 ] = " foo " ; 3 ab [ 1 ] = " bar " ;

Data types, including STL containers and iterators, can be

  • nested. and other niceties. Implicit converters allow us to

combine types:

Dirk Eddelbuettel Seamless R Extensions using Rcpp + RInside

slide-49
SLIDE 49

Extending R Rcpp Examples Summary Overview New API Examples

Rcpp: The ’New API’

Rcpp was significantly extended over the last few months to permit more natural expressions. Consider this comparison between the R API and the new Rcpp API:

1 SEXP ab ; 2 PROTECT( ab = allocVector (STRSXP, 2) ) ; 3 SET_STRING_ELT( ab , 0 , mkChar( " foo " ) ) ; 4 SET_STRING_ELT( ab , 1 , mkChar( " bar " ) ) ; 5 UNPROTECT(1) ; 1 CharacterVector ab (2) ; 2 ab [ 0 ] = " foo " ; 3 ab [ 1 ] = " bar " ;

Data types, including STL containers and iterators, can be

  • nested. and other niceties. Implicit converters allow us to

combine types:

1 std : : vector <double> vec ; 2 [ . . . ] 3 L i s t x (3) ; 4 x [ 0 ] = vec ; 5 x [ 1 ] = "some t e x t " ; 6 x [ 2 ] = 42; Dirk Eddelbuettel Seamless R Extensions using Rcpp + RInside

slide-50
SLIDE 50

Extending R Rcpp Examples Summary Overview New API Examples

Rcpp: The ’New API’

Rcpp was significantly extended over the last few months to permit more natural expressions. Consider this comparison between the R API and the new Rcpp API:

1 SEXP ab ; 2 PROTECT( ab = allocVector (STRSXP, 2) ) ; 3 SET_STRING_ELT( ab , 0 , mkChar( " foo " ) ) ; 4 SET_STRING_ELT( ab , 1 , mkChar( " bar " ) ) ; 5 UNPROTECT(1) ; 1 CharacterVector ab (2) ; 2 ab [ 0 ] = " foo " ; 3 ab [ 1 ] = " bar " ;

Data types, including STL containers and iterators, can be

  • nested. and other niceties. Implicit converters allow us to

combine types:

1 std : : vector <double> vec ; 2 [ . . . ] 3 L i s t x (3) ; 4 x [ 0 ] = vec ; 5 x [ 1 ] = "some t e x t " ; 6 x [ 2 ] = 42; 1 / / With Rcpp 0.7.11 or l a t e r we can do : 2 std : : vector <double> vec ; 3 [ . . . ] 4 L i s t x = L i s t : : create ( vec , 5 "some t e x t " , 6 42) ; Dirk Eddelbuettel Seamless R Extensions using Rcpp + RInside

slide-51
SLIDE 51

Extending R Rcpp Examples Summary Overview New API Examples

Functional programming in both languages

In R, functional programming is easy:

Dirk Eddelbuettel Seamless R Extensions using Rcpp + RInside

slide-52
SLIDE 52

Extending R Rcpp Examples Summary Overview New API Examples

Functional programming in both languages

In R, functional programming is easy:

1 R> data ( f a i t h f u l ) ; lapply ( f a i t h f u l , summary) 2 $ eruptions 3 Min . 1 st Qu. Median Mean 3rd Qu. Max. 4 1.60 2.16 4.00 3.49 4.45 5.10 5 6 $ waiting 7 Min . 1 st Qu. Median Mean 3rd Qu. Max. 8 43.0 58.0 76.0 70.9 82.0 96.0 Dirk Eddelbuettel Seamless R Extensions using Rcpp + RInside

slide-53
SLIDE 53

Extending R Rcpp Examples Summary Overview New API Examples

Functional programming in both languages

In R, functional programming is easy:

1 R> data ( f a i t h f u l ) ; lapply ( f a i t h f u l , summary) 2 $ eruptions 3 Min . 1 st Qu. Median Mean 3rd Qu. Max. 4 1.60 2.16 4.00 3.49 4.45 5.10 5 6 $ waiting 7 Min . 1 st Qu. Median Mean 3rd Qu. Max. 8 43.0 58.0 76.0 70.9 82.0 96.0

We can do that in C++ as well and pass the R function down to the data elements we let the STL iterate over:

1 src <− ’Rcpp : : L i s t input ( data ) ; 2 Rcpp : : Function f ( fun ) ; 3 Rcpp : : L i s t

  • utput ( input . size ( ) ) ;

4 std : : transform ( input . begin ( ) , input . end ( ) ,

  • utput . begin ( ) ,

f ) ; 5

  • utput . names ( ) = input . names ( ) ;

6 return

  • utput ;

’ 7 cpp_ lapply <− cfunction ( signature ( data=" l i s t " , fun = " function " ) , src , Rcpp = TRUE ) Dirk Eddelbuettel Seamless R Extensions using Rcpp + RInside

slide-54
SLIDE 54

Extending R Rcpp Examples Summary Overview New API Examples

Exception handling

Automatic catching and conversion of C++ exceptions:

Dirk Eddelbuettel Seamless R Extensions using Rcpp + RInside

slide-55
SLIDE 55

Extending R Rcpp Examples Summary Overview New API Examples

Exception handling

Automatic catching and conversion of C++ exceptions:

R> library(Rcpp); library(inline) R> cpp <- ’ + Rcpp::NumericVector x(xs); // automatic conversion from SEXP + for (int i=0; i<x.size(); i++) { + if (x[i] < 0) + throw std::range_error("Non-negative values required"); + x[i] = log(x[i]); + } + return x; // automatic conversion to SEXP + ’ R> fun <- cfunction(signature(xs="numeric"), cpp, Rcpp=TRUE) R> fun( seq(2, 5) ) [1] 0.6931 1.0986 1.3863 1.6094 R> fun( seq(5, -2) ) Error in fun(seq(5, -2)) : Non-negative values required R> fun( LETTERS[1:5] ) Error in fun(LETTERS[1:5]) : not compatible with INTSXP R>

Dirk Eddelbuettel Seamless R Extensions using Rcpp + RInside

slide-56
SLIDE 56

Extending R Rcpp Examples Summary Overview New API Examples

Outline

1

Extending R Why ? The standard API Inline

2

Rcpp Overview New API Examples

3

Rcpp Usage Examples RInside Others

4

Summary Key points Resources

Dirk Eddelbuettel Seamless R Extensions using Rcpp + RInside

slide-57
SLIDE 57

Extending R Rcpp Examples Summary Overview New API Examples

Rcpp example

The convolution example can be rewritten in the new API:

Dirk Eddelbuettel Seamless R Extensions using Rcpp + RInside

slide-58
SLIDE 58

Extending R Rcpp Examples Summary Overview New API Examples

Rcpp example

The convolution example can be rewritten in the new API:

1 #include <Rcpp . h> 2 3 RcppExport SEXP convolve_cpp (SEXP a , SEXP b ) { 4 Rcpp : : NumericVector xa ( a ) ; / / automatic conversion from SEXP 5 Rcpp : : NumericVector xb ( b ) ; 6 7 int n_xa = xa . size ( ) ; 8 int n_xb = xb . size ( ) ; 9 int nab = n_xa + n_xb − 1; 10 11 Rcpp : : NumericVector xab ( nab ) ; 12 13 for ( int i = 0; i < n_xa ; i ++) 14 for ( int j = 0; j < n_xb ; j ++) 15 xab [ i + j ] += xa [ i ] ∗ xb [ j ] ; 16 17 return xab ; / / automatic conversion to SEXP 18 } Dirk Eddelbuettel Seamless R Extensions using Rcpp + RInside

slide-59
SLIDE 59

Extending R Rcpp Examples Summary Overview New API Examples

Speed comparison

In a recently-submitted paper, the following table summarises the performance of convolution examples:

Dirk Eddelbuettel Seamless R Extensions using Rcpp + RInside

slide-60
SLIDE 60

Extending R Rcpp Examples Summary Overview New API Examples

Speed comparison

In a recently-submitted paper, the following table summarises the performance of convolution examples:

Implementation Time in Relative millisec to R API R API (as benchmark) 32 RcppVector<double> 354 11.1 NumericVector::operator[] 52 1.6 NumericVector::begin 33 1.0

Table 1: Performance for convolution example

Dirk Eddelbuettel Seamless R Extensions using Rcpp + RInside

slide-61
SLIDE 61

Extending R Rcpp Examples Summary Overview New API Examples

Speed comparison

In a recently-submitted paper, the following table summarises the performance of convolution examples:

Implementation Time in Relative millisec to R API R API (as benchmark) 32 RcppVector<double> 354 11.1 NumericVector::operator[] 52 1.6 NumericVector::begin 33 1.0

Table 1: Performance for convolution example We averaged 1000 replications with two 100-element vectors – see examples/ConvolveBenchmarks/ in Rcpp for details.

Dirk Eddelbuettel Seamless R Extensions using Rcpp + RInside

slide-62
SLIDE 62

Extending R Rcpp Examples Summary Overview New API Examples

Another Speed Comparison Example

Regression is a key component of many studies. In simulations, we often want to run a very large number of regressions.

Dirk Eddelbuettel Seamless R Extensions using Rcpp + RInside

slide-63
SLIDE 63

Extending R Rcpp Examples Summary Overview New API Examples

Another Speed Comparison Example

Regression is a key component of many studies. In simulations, we often want to run a very large number of regressions. R has lm() as the general purposes function. It is very powerful and returns a rich object—but it is not lightweight.

Dirk Eddelbuettel Seamless R Extensions using Rcpp + RInside

slide-64
SLIDE 64

Extending R Rcpp Examples Summary Overview New API Examples

Another Speed Comparison Example

Regression is a key component of many studies. In simulations, we often want to run a very large number of regressions. R has lm() as the general purposes function. It is very powerful and returns a rich object—but it is not lightweight. For this purpose, R has lm.fit(). But, this does not provide all relevant auxiliary data as e.g. the standard error

  • f the estimate.

Dirk Eddelbuettel Seamless R Extensions using Rcpp + RInside

slide-65
SLIDE 65

Extending R Rcpp Examples Summary Overview New API Examples

Another Speed Comparison Example

Regression is a key component of many studies. In simulations, we often want to run a very large number of regressions. R has lm() as the general purposes function. It is very powerful and returns a rich object—but it is not lightweight. For this purpose, R has lm.fit(). But, this does not provide all relevant auxiliary data as e.g. the standard error

  • f the estimate.

For the most recent Introduction to High-Performance Computing with R tutorial, I had written a hybrid R/C/C++ solution using the GNU GSL.

Dirk Eddelbuettel Seamless R Extensions using Rcpp + RInside

slide-66
SLIDE 66

Extending R Rcpp Examples Summary Overview New API Examples

Another Speed Comparison Example

Regression is a key component of many studies. In simulations, we often want to run a very large number of regressions. R has lm() as the general purposes function. It is very powerful and returns a rich object—but it is not lightweight. For this purpose, R has lm.fit(). But, this does not provide all relevant auxiliary data as e.g. the standard error

  • f the estimate.

For the most recent Introduction to High-Performance Computing with R tutorial, I had written a hybrid R/C/C++ solution using the GNU GSL. We complement this with a new C++ implementation around the Armadillo linear algebra classes.

Dirk Eddelbuettel Seamless R Extensions using Rcpp + RInside

slide-67
SLIDE 67

Extending R Rcpp Examples Summary Overview New API Examples

Linear regression via GSL: lmGSL()

1 lmGSL <− function ( ) { 2 src <− ’ 3 4 RcppVectorView<double > Yr ( Ysexp ) ; 5 RcppMatrixView <double > Xr ( Xsexp ) ; 6 7 i n t i , j , n = Xr . dim1 ( ) , k = Xr . dim2 ( ) ; 8 double chi2 ; 9 10 gsl _matrix ∗X = gsl _matrix_ a l l o c (n , k ) ; 11 gsl _vector ∗y = gsl _vector_ a l l o c ( n ) ; 12 gsl _vector ∗c = gsl _vector_ a l l o c ( k ) ; 13 gsl _matrix ∗cov = gsl _matrix_ a l l o c ( k , k ) ; 14 15 f o r ( i = 0; i < n ; i ++) { 16 f o r ( j = 0; j < k ; j ++) { 17 gsl _matrix_set (X, i , j , Xr ( i , j ) ) ; 18 } 19 gsl _vector_set ( y , i , Yr ( i ) ) ; 20 } 21 22 gsl _ m u l t i f i t _ l i n e a r _workspace ∗wk = 23 gsl _ m u l t i f i t _ l i n e a r _ a l l o c (n , k ) ; 24 gsl _ m u l t i f i t _ l i n e a r (X, y , c , cov ,&chi2 , wk) ; 25 gsl _ m u l t i f i t _ l i n e a r _ free (wk) ; 26 RcppVector<double > StdErr ( k ) ; 27 RcppVector<double > Coef ( k ) ; 28 for ( i = 0; i < k ; i ++) { 29 Coef ( i ) = gsl _vector_get (c , i ) ; 30 StdErr ( i ) = 31 sqrt ( gsl _matrix_get (cov , i , i ) ) ; 32 } 33 34 gsl _matrix_ free (X) ; 35 gsl _vector_ free ( y ) ; 36 gsl _vector_ free ( c ) ; 37 gsl _matrix_ free ( cov ) ; 38 39 RcppResultSet rs ; 40 rs . add( " coef " , Coef ) ; 41 rs . add( " stderr " , StdErr ) ; 42 43 return = rs . getReturnList ( ) ; 44 ’ 45 ## turn i n t o a function that R can c a l l 46 ## args redundant on Debian / Ubuntu 47 fun <− 48 cfunction ( signature ( Ysexp=" numeric " , 49 Xsexp=" numeric " ) , src , 50 includes= 51 "# include <gsl / gsl _ m u l t i f i t . h>" , 52 Rcpp=TRUE, 53 cppargs="−I / usr / include " , 54 l i b a r g s="− l g s l −l g s l c b l a s " ) 55 } Dirk Eddelbuettel Seamless R Extensions using Rcpp + RInside

slide-68
SLIDE 68

Extending R Rcpp Examples Summary Overview New API Examples

Linear regression via Armadillo: lmArmadillo example

1 lmArmadillo <− function ( ) { 2 src <− ’ 3 Rcpp : : NumericVector yr ( Ysexp ) ; 4 Rcpp : : NumericVector Xr ( Xsexp ) ; / / a c t u a l l y an n x k matrix 5 std : : vector < int > dims = Xr . a t t r ( " dim " ) ; 6 i n t n = dims [ 0 ] , k = dims [ 1 ] ; 7 arma : : mat X( Xr . begin ( ) , n , k , false ) ; / / use advanced armadillo constructors 8 arma : : colvec y ( yr . begin ( ) , yr . size ( ) ) ; 9 arma : : colvec coef = solve (X, y ) ; / / model f i t 10 arma : : colvec resid = y − X∗coef ; / / to comp . std . e r r r

  • f

the c o e f f i c i e n t s 11 arma : : mat covmat = trans ( resid )∗resid / (n −k ) ∗ arma : : inv ( arma : : trans (X)∗X) ; 12 13 Rcpp : : NumericVector coefr ( k ) , s t d e r r e s t r ( k ) ; 14 f o r ( i n t i =0; i <k ; i ++) { / / with RcppArmadillo template converters 15 coefr [ i ] = coef [ i ] ; / / t h i s would not be needed but we only 16 s t d e r r e s t r [ i ] = sqrt ( covmat ( i , i ) ) ; / / have Rcpp . h here 17 } 18 19 return Rcpp : : L i s t : : create ( Rcpp : : Named( " c o e f f i c i e n t s " , coefr ) , / / Rcpp 0.7.11 20 Rcpp : : Named( " stderr " , s t d e r r e s t r ) ) ; 21 ’ 22 23 ## turn i n t o a function that R can c a l l 24 fun <− cfunction ( signature ( Ysexp=" numeric " , Xsexp=" numeric " ) , 25 src , includes="# include <armadillo >" , Rcpp=TRUE, 26 cppargs="−I / usr / include " , l i b a r g s ="−l a r m a d i l l o " ) 27 } Dirk Eddelbuettel Seamless R Extensions using Rcpp + RInside

slide-69
SLIDE 69

Extending R Rcpp Examples Summary Overview New API Examples

Linear regression via Armadillo: RcppArmadillo

fastLm in the new RcppArmadillo does even better:

1 #include <RcppArmadillo . h> 2 3 extern "C" SEXP fastLm (SEXP ys , SEXP Xs) { 4 Rcpp : : NumericVector yr ( ys ) ; / / creates Rcpp vector from SEXP 5 Rcpp : : NumericMatrix Xr (Xs) ; / / creates Rcpp matrix from SEXP 6 int n = Xr . nrow ( ) , k = Xr . ncol ( ) ; 7 8 arma : : mat X( Xr . begin ( ) , n , k , false ) ; / / reuses memory and avoids extra copy 9 arma : : colvec y ( yr . begin ( ) , yr . size ( ) , false ) ; 10 11 arma : : colvec coef = arma : : solve (X, y ) ; / / f i t model y ~ X 12 arma : : colvec resid = y − X∗coef ; / / residuals 13 14 double sig2 = arma : : as_ scalar ( arma : : trans ( resid )∗resid / (n −k ) ) ; / / std . err est 15 arma : : colvec sdest = arma : : sqrt ( sig2∗arma : : diagvec ( arma : : inv ( arma : : trans (X)∗X) ) ) ; 16 17 return Rcpp : : L i s t : : create ( / / requires Rcpp 0.7.11 18 Rcpp : : Named( " c o e f f i c i e n t s " ) = coef , 19 Rcpp : : Named( " stderr " ) = sdest 20 ) ; 21 } Dirk Eddelbuettel Seamless R Extensions using Rcpp + RInside

slide-70
SLIDE 70

Extending R Rcpp Examples Summary Overview New API Examples

Rcpp Example: Regression timings

lm lm.fit lmGSL lmArmadillo

Comparison of R and linear model fit routines

time in milliseconds 50 100 150 200 250 longley (16 x 7 obs) simulated (10000 x 3)

Source: Our calculations, see examples/FastLM/ in Rcpp.

The small longley example exhibits less variability between methods, but the larger data set shows the gains more clearly. For the small data set, all three appear to improve similarly on lm.

Dirk Eddelbuettel Seamless R Extensions using Rcpp + RInside

slide-71
SLIDE 71

Extending R Rcpp Examples Summary Overview New API Examples

Another Rcpp example (cont.)

lm lm.fit lmGSL lmArmadillo

Comparison of R and linear model fit routines

ratio to lm() baseline 10 20 30 40 longley (16 x 7 obs) simulated (10000 x 3)

Source: Our calculations, see examples/FastLM/ in Rcpp.

By dividing the lm time by the respective times, we

  • btain the ’possible gains’

from switching. One caveat, measurements depends critically on the size of the data as well as the cpu and libraries that are used.

Dirk Eddelbuettel Seamless R Extensions using Rcpp + RInside

slide-72
SLIDE 72

Extending R Rcpp Examples Summary RInside Others

Outline

1

Extending R Why ? The standard API Inline

2

Rcpp Overview New API Examples

3

Rcpp Usage Examples RInside Others

4

Summary Key points Resources

Dirk Eddelbuettel Seamless R Extensions using Rcpp + RInside

slide-73
SLIDE 73

Extending R Rcpp Examples Summary RInside Others

Outline

1

Extending R Why ? The standard API Inline

2

Rcpp Overview New API Examples

3

Rcpp Usage Examples RInside Others

4

Summary Key points Resources

Dirk Eddelbuettel Seamless R Extensions using Rcpp + RInside

slide-74
SLIDE 74

Extending R Rcpp Examples Summary RInside Others

From RApache to littler to RInside

Jeff Horner’s work on RApache lead to joint work in littler, a scripting / cmdline front-end. As it embeds R and simply ’feeds’ the REPL loop, the next step was to embed R in proper C++ classes: RInside.

Dirk Eddelbuettel Seamless R Extensions using Rcpp + RInside

slide-75
SLIDE 75

Extending R Rcpp Examples Summary RInside Others

From RApache to littler to RInside

Jeff Horner’s work on RApache lead to joint work in littler, a scripting / cmdline front-end. As it embeds R and simply ’feeds’ the REPL loop, the next step was to embed R in proper C++ classes: RInside.

1 #include <RInside . h> / / for the embedded R via RInside 2 3 int main ( int argc , char ∗argv [ ] ) { 4 5 RInside R( argc , argv ) ; / / create an embedded R instance 6 7 R[ " t x t " ] = " Hello , world ! \ n" ; / / assign a char∗ ( s t r i n g ) to ’ t x t ’ 8 9

  • R. parseEvalQ ( " cat ( t x t ) " ) ;

/ / eval the i n i t string , ignoring any returns 10 11 e x i t (0) ; 12 } Dirk Eddelbuettel Seamless R Extensions using Rcpp + RInside

slide-76
SLIDE 76

Extending R Rcpp Examples Summary RInside Others

Another simple example

This example shows some of the new assignment and converter code:

1 2 #include <RInside . h> / / for the embedded R via RInside 3 4 int main ( int argc , char ∗argv [ ] ) { 5 6 RInside R( argc , argv ) ; / / create an embedded R instance 7 8 R[ " x " ] = 10 ; 9 R[ " y " ] = 20 ; 10 11

  • R. parseEvalQ ( " z <− x + y " )

; 12 13 int sum = R[ " z " ] ; 14 15 std : : cout << " 10 + 20 = " << sum << std : : endl ; 16 e x i t (0) ; 17 } Dirk Eddelbuettel Seamless R Extensions using Rcpp + RInside

slide-77
SLIDE 77

Extending R Rcpp Examples Summary RInside Others

And another parallel example

1 / / MPI C++ API version

  • f

f i l e contributed by Jianping Hua 2 3 #include <mpi . h> / / mpi header 4 #include <RInside . h> / / for the embedded R via RInside 5 6 int main ( int argc , char ∗argv [ ] ) { 7 8 MPI : : I n i t ( argc , argv ) ; / / mpi i n i t i a l i z a t i o n 9 int myrank = MPI : :C O M M_WORLD. Get_rank ( ) ; / /

  • btain

current node rank 10 int nodesize = MPI : :C O M M_WORLD. Get_size ( ) ; / /

  • btain

t o t a l nodes running . 11 12 RInside R( argc , argv ) ; / / create an embedded R instance 13 14 std : : stringstream t x t ; 15 t x t << " Hello from node " << myrank / / node information 16 << "

  • f

" << nodesize << " nodes ! " << std : : endl ; 17

  • R. assign (

t x t . s t r ( ) , " t x t " ) ; / / assign s t r i n g to R variable ’ t x t ’ 18 19 std : : s t r i n g e v a l s t r = " cat ( t x t ) " ; / / show node information 20

  • R. parseEvalQ ( e v a l s t r ) ;

/ / eval the string , ign . any returns 21 22 MPI : : F i n a l i z e ( ) ; / / mpi f i n a l i z a t i o n 23 24 e x i t (0) ; 25 } Dirk Eddelbuettel Seamless R Extensions using Rcpp + RInside

slide-78
SLIDE 78

Extending R Rcpp Examples Summary RInside Others

RInside workflow

C++ programs compute, gather or aggregate raw data.

Dirk Eddelbuettel Seamless R Extensions using Rcpp + RInside

slide-79
SLIDE 79

Extending R Rcpp Examples Summary RInside Others

RInside workflow

C++ programs compute, gather or aggregate raw data. Data is saved and analysed before a new ’run’ is launched.

Dirk Eddelbuettel Seamless R Extensions using Rcpp + RInside

slide-80
SLIDE 80

Extending R Rcpp Examples Summary RInside Others

RInside workflow

C++ programs compute, gather or aggregate raw data. Data is saved and analysed before a new ’run’ is launched. With RInside we now skip a step:

Dirk Eddelbuettel Seamless R Extensions using Rcpp + RInside

slide-81
SLIDE 81

Extending R Rcpp Examples Summary RInside Others

RInside workflow

C++ programs compute, gather or aggregate raw data. Data is saved and analysed before a new ’run’ is launched. With RInside we now skip a step:

collect data in a vector or matrix

Dirk Eddelbuettel Seamless R Extensions using Rcpp + RInside

slide-82
SLIDE 82

Extending R Rcpp Examples Summary RInside Others

RInside workflow

C++ programs compute, gather or aggregate raw data. Data is saved and analysed before a new ’run’ is launched. With RInside we now skip a step:

collect data in a vector or matrix pass data to R — easy thanks to Rcpp wrappers

Dirk Eddelbuettel Seamless R Extensions using Rcpp + RInside

slide-83
SLIDE 83

Extending R Rcpp Examples Summary RInside Others

RInside workflow

C++ programs compute, gather or aggregate raw data. Data is saved and analysed before a new ’run’ is launched. With RInside we now skip a step:

collect data in a vector or matrix pass data to R — easy thanks to Rcpp wrappers pass one or more short ’scripts’ as strings to R to evaluate

Dirk Eddelbuettel Seamless R Extensions using Rcpp + RInside

slide-84
SLIDE 84

Extending R Rcpp Examples Summary RInside Others

RInside workflow

C++ programs compute, gather or aggregate raw data. Data is saved and analysed before a new ’run’ is launched. With RInside we now skip a step:

collect data in a vector or matrix pass data to R — easy thanks to Rcpp wrappers pass one or more short ’scripts’ as strings to R to evaluate pass data back to C++ programm — easy thanks to Rcpp converters

Dirk Eddelbuettel Seamless R Extensions using Rcpp + RInside

slide-85
SLIDE 85

Extending R Rcpp Examples Summary RInside Others

RInside workflow

C++ programs compute, gather or aggregate raw data. Data is saved and analysed before a new ’run’ is launched. With RInside we now skip a step:

collect data in a vector or matrix pass data to R — easy thanks to Rcpp wrappers pass one or more short ’scripts’ as strings to R to evaluate pass data back to C++ programm — easy thanks to Rcpp converters resume main execution based on new results

Dirk Eddelbuettel Seamless R Extensions using Rcpp + RInside

slide-86
SLIDE 86

Extending R Rcpp Examples Summary RInside Others

RInside workflow

C++ programs compute, gather or aggregate raw data. Data is saved and analysed before a new ’run’ is launched. With RInside we now skip a step:

collect data in a vector or matrix pass data to R — easy thanks to Rcpp wrappers pass one or more short ’scripts’ as strings to R to evaluate pass data back to C++ programm — easy thanks to Rcpp converters resume main execution based on new results

A number of simple examples ship with RInside

Dirk Eddelbuettel Seamless R Extensions using Rcpp + RInside

slide-87
SLIDE 87

Extending R Rcpp Examples Summary RInside Others

Outline

1

Extending R Why ? The standard API Inline

2

Rcpp Overview New API Examples

3

Rcpp Usage Examples RInside Others

4

Summary Key points Resources

Dirk Eddelbuettel Seamless R Extensions using Rcpp + RInside

slide-88
SLIDE 88

Extending R Rcpp Examples Summary RInside Others

Users of Rcpp

RInside uses Rcpp for object transfer and more

Dirk Eddelbuettel Seamless R Extensions using Rcpp + RInside

slide-89
SLIDE 89

Extending R Rcpp Examples Summary RInside Others

Users of Rcpp

RInside uses Rcpp for object transfer and more RcppArmadillo (which contains fastLM())

Dirk Eddelbuettel Seamless R Extensions using Rcpp + RInside

slide-90
SLIDE 90

Extending R Rcpp Examples Summary RInside Others

Users of Rcpp

RInside uses Rcpp for object transfer and more RcppArmadillo (which contains fastLM()) RcppExamples is a ’this is how you can do it’ stanza

Dirk Eddelbuettel Seamless R Extensions using Rcpp + RInside

slide-91
SLIDE 91

Extending R Rcpp Examples Summary RInside Others

Users of Rcpp

RInside uses Rcpp for object transfer and more RcppArmadillo (which contains fastLM()) RcppExamples is a ’this is how you can do it’ stanza RProtoBuf is what got Romain and me here, it may get rewritten to take more advantage of Rcpp

Dirk Eddelbuettel Seamless R Extensions using Rcpp + RInside

slide-92
SLIDE 92

Extending R Rcpp Examples Summary RInside Others

Users of Rcpp

RInside uses Rcpp for object transfer and more RcppArmadillo (which contains fastLM()) RcppExamples is a ’this is how you can do it’ stanza RProtoBuf is what got Romain and me here, it may get rewritten to take more advantage of Rcpp RQuantLib is where Rcpp orginally started

Dirk Eddelbuettel Seamless R Extensions using Rcpp + RInside

slide-93
SLIDE 93

Extending R Rcpp Examples Summary RInside Others

Users of Rcpp

RInside uses Rcpp for object transfer and more RcppArmadillo (which contains fastLM()) RcppExamples is a ’this is how you can do it’ stanza RProtoBuf is what got Romain and me here, it may get rewritten to take more advantage of Rcpp RQuantLib is where Rcpp orginally started highlight is Romain’s first re-use of Rcpp

Dirk Eddelbuettel Seamless R Extensions using Rcpp + RInside

slide-94
SLIDE 94

Extending R Rcpp Examples Summary RInside Others

Users of Rcpp

RInside uses Rcpp for object transfer and more RcppArmadillo (which contains fastLM()) RcppExamples is a ’this is how you can do it’ stanza RProtoBuf is what got Romain and me here, it may get rewritten to take more advantage of Rcpp RQuantLib is where Rcpp orginally started highlight is Romain’s first re-use of Rcpp mvabund, sdcTable, bifactorial, minqa are truly external users which are all on CRAN

Dirk Eddelbuettel Seamless R Extensions using Rcpp + RInside

slide-95
SLIDE 95

Extending R Rcpp Examples Summary RInside Others

Users of Rcpp

RInside uses Rcpp for object transfer and more RcppArmadillo (which contains fastLM()) RcppExamples is a ’this is how you can do it’ stanza RProtoBuf is what got Romain and me here, it may get rewritten to take more advantage of Rcpp RQuantLib is where Rcpp orginally started highlight is Romain’s first re-use of Rcpp mvabund, sdcTable, bifactorial, minqa are truly external users which are all on CRAN upcoming: pcaMethods (BioC), phylobase, possibly lme4

Dirk Eddelbuettel Seamless R Extensions using Rcpp + RInside

slide-96
SLIDE 96

Extending R Rcpp Examples Summary RInside Others

Users of Rcpp

RInside uses Rcpp for object transfer and more RcppArmadillo (which contains fastLM()) RcppExamples is a ’this is how you can do it’ stanza RProtoBuf is what got Romain and me here, it may get rewritten to take more advantage of Rcpp RQuantLib is where Rcpp orginally started highlight is Romain’s first re-use of Rcpp mvabund, sdcTable, bifactorial, minqa are truly external users which are all on CRAN upcoming: pcaMethods (BioC), phylobase, possibly lme4 Your package here next?

Dirk Eddelbuettel Seamless R Extensions using Rcpp + RInside

slide-97
SLIDE 97

Extending R Rcpp Examples Summary Key points Resources

Outline

1

Extending R Why ? The standard API Inline

2

Rcpp Overview New API Examples

3

Rcpp Usage Examples RInside Others

4

Summary Key points Resources

Dirk Eddelbuettel Seamless R Extensions using Rcpp + RInside

slide-98
SLIDE 98

Extending R Rcpp Examples Summary Key points Resources

Outline

1

Extending R Why ? The standard API Inline

2

Rcpp Overview New API Examples

3

Rcpp Usage Examples RInside Others

4

Summary Key points Resources

Dirk Eddelbuettel Seamless R Extensions using Rcpp + RInside

slide-99
SLIDE 99

Extending R Rcpp Examples Summary Key points Resources

Wrapping up

This presentation has tried to convince you that While the deck way be stacked against you (when adding C/C++ to R), you can still pick where to play

Dirk Eddelbuettel Seamless R Extensions using Rcpp + RInside

slide-100
SLIDE 100

Extending R Rcpp Examples Summary Key points Resources

Wrapping up

This presentation has tried to convince you that While the deck way be stacked against you (when adding C/C++ to R), you can still pick where to play R can be extended in many ways; we focus on something that allows us write extensions

Dirk Eddelbuettel Seamless R Extensions using Rcpp + RInside

slide-101
SLIDE 101

Extending R Rcpp Examples Summary Key points Resources

Wrapping up

This presentation has tried to convince you that While the deck way be stacked against you (when adding C/C++ to R), you can still pick where to play R can be extended in many ways; we focus on something that allows us write extensions

that are efficient: we want speed and features

Dirk Eddelbuettel Seamless R Extensions using Rcpp + RInside

slide-102
SLIDE 102

Extending R Rcpp Examples Summary Key points Resources

Wrapping up

This presentation has tried to convince you that While the deck way be stacked against you (when adding C/C++ to R), you can still pick where to play R can be extended in many ways; we focus on something that allows us write extensions

that are efficient: we want speed and features that correspond to the R object model

Dirk Eddelbuettel Seamless R Extensions using Rcpp + RInside

slide-103
SLIDE 103

Extending R Rcpp Examples Summary Key points Resources

Wrapping up

This presentation has tried to convince you that While the deck way be stacked against you (when adding C/C++ to R), you can still pick where to play R can be extended in many ways; we focus on something that allows us write extensions

that are efficient: we want speed and features that correspond to the R object model that also allow us to embed R inside C++

Dirk Eddelbuettel Seamless R Extensions using Rcpp + RInside

slide-104
SLIDE 104

Extending R Rcpp Examples Summary Key points Resources

Wrapping up

This presentation has tried to convince you that While the deck way be stacked against you (when adding C/C++ to R), you can still pick where to play R can be extended in many ways; we focus on something that allows us write extensions

that are efficient: we want speed and features that correspond to the R object model that also allow us to embed R inside C++

And all this while retaining ’high-level’ STL-alike semantics, templates and other goodies in C++

Dirk Eddelbuettel Seamless R Extensions using Rcpp + RInside

slide-105
SLIDE 105

Extending R Rcpp Examples Summary Key points Resources

Wrapping up

This presentation has tried to convince you that While the deck way be stacked against you (when adding C/C++ to R), you can still pick where to play R can be extended in many ways; we focus on something that allows us write extensions

that are efficient: we want speed and features that correspond to the R object model that also allow us to embed R inside C++

And all this while retaining ’high-level’ STL-alike semantics, templates and other goodies in C++ Using C++ abstractions wisely can keep the code both clean and readable – yet very efficient

Dirk Eddelbuettel Seamless R Extensions using Rcpp + RInside

slide-106
SLIDE 106

Extending R Rcpp Examples Summary Key points Resources

Outline

1

Extending R Why ? The standard API Inline

2

Rcpp Overview New API Examples

3

Rcpp Usage Examples RInside Others

4

Summary Key points Resources

Dirk Eddelbuettel Seamless R Extensions using Rcpp + RInside

slide-107
SLIDE 107

Extending R Rcpp Examples Summary Key points Resources

Some pointers

http://dirk.eddelbuettel.com/code/rcpp.html

Dirk Eddelbuettel Seamless R Extensions using Rcpp + RInside

slide-108
SLIDE 108

Extending R Rcpp Examples Summary Key points Resources

Some pointers

http://dirk.eddelbuettel.com/code/rcpp.html http://romainfrancois.blog.free.fr/index.php? category/R-package/Rcpp

Dirk Eddelbuettel Seamless R Extensions using Rcpp + RInside

slide-109
SLIDE 109

Extending R Rcpp Examples Summary Key points Resources

Some pointers

http://dirk.eddelbuettel.com/code/rcpp.html http://romainfrancois.blog.free.fr/index.php? category/R-package/Rcpp http://cran.r-project.org/package=Rcpp

Dirk Eddelbuettel Seamless R Extensions using Rcpp + RInside

slide-110
SLIDE 110

Extending R Rcpp Examples Summary Key points Resources

Some pointers

http://dirk.eddelbuettel.com/code/rcpp.html http://romainfrancois.blog.free.fr/index.php? category/R-package/Rcpp http://cran.r-project.org/package=Rcpp http://r-forge.r-project.org/projects/rcpp/

Dirk Eddelbuettel Seamless R Extensions using Rcpp + RInside

slide-111
SLIDE 111

Extending R Rcpp Examples Summary Key points Resources

Some pointers

http://dirk.eddelbuettel.com/code/rcpp.html http://romainfrancois.blog.free.fr/index.php? category/R-package/Rcpp http://cran.r-project.org/package=Rcpp http://r-forge.r-project.org/projects/rcpp/ and likewise for RInside, RProtoBuf and more.

Dirk Eddelbuettel Seamless R Extensions using Rcpp + RInside

slide-112
SLIDE 112

Extending R Rcpp Examples Summary Key points Resources

The end Thank you!

Dirk Eddelbuettel Seamless R Extensions using Rcpp + RInside