Seamless R and C++ Integration: Rcpp and RInside Dirk Eddelbuettel - - PowerPoint PPT Presentation

seamless r and c integration rcpp and rinside
SMART_READER_LITE
LIVE PREVIEW

Seamless R and C++ Integration: Rcpp and RInside Dirk Eddelbuettel - - PowerPoint PPT Presentation

Extending R Rcpp Seamless R and C++ Integration: Rcpp and RInside Dirk Eddelbuettel Debian Project Joint work with Romain Franois Invited seminar presentation Institute for Statistics and Mathematics Wirtschaftsuniversitt Wien 20 May


slide-1
SLIDE 1

Extending R Rcpp

Seamless R and C++ Integration: Rcpp and RInside

Dirk Eddelbuettel

Debian Project

Joint work with Romain François Invited seminar presentation Institute for Statistics and Mathematics Wirtschaftsuniversität Wien 20 May 2010

Dirk Eddelbuettel Seamless R and C++ Integration @ WU Wien, May 2010

slide-2
SLIDE 2

Extending R Rcpp

Preliminaries

Dirk Eddelbuettel Seamless R and C++ Integration @ WU Wien, May 2010

slide-3
SLIDE 3

Extending R Rcpp

Preliminaries

We assume a recent version of R so that install.packages(c("Rcpp","RInside","inline")) gets us current versions of the packages

Dirk Eddelbuettel Seamless R and C++ Integration @ WU Wien, May 2010

slide-4
SLIDE 4

Extending R Rcpp

Preliminaries

We assume a recent version of R so that install.packages(c("Rcpp","RInside","inline")) gets us current versions of the packages All examples shown should work ’as is’ on Linux, OS X and Windows provided a complete R development environment

Dirk Eddelbuettel Seamless R and C++ Integration @ WU Wien, May 2010

slide-5
SLIDE 5

Extending R Rcpp

Preliminaries

We assume a recent version of R so that install.packages(c("Rcpp","RInside","inline")) gets us current versions of the packages All examples shown should work ’as is’ on Linux, OS X and Windows provided a complete R development environment The R Installation and Administration manual is an excellent start if you need to address the preceding point

Dirk Eddelbuettel Seamless R and C++ Integration @ WU Wien, May 2010

slide-6
SLIDE 6

Extending R Rcpp

Preliminaries

We assume a recent version of R so that install.packages(c("Rcpp","RInside","inline")) gets us current versions of the packages All examples shown should work ’as is’ on Linux, OS X and Windows provided a complete R development environment The R Installation and Administration manual is an excellent start if you need to address the preceding point In particular, one must use the same compilers used to build R in order to extend or embed the R engine

Dirk Eddelbuettel Seamless R and C++ Integration @ WU Wien, May 2010

slide-7
SLIDE 7

Extending R Rcpp

Preliminaries

We assume a recent version of R so that install.packages(c("Rcpp","RInside","inline")) gets us current versions of the packages All examples shown should work ’as is’ on Linux, OS X and Windows provided a complete R development environment The R Installation and Administration manual is an excellent start if you need to address the preceding point In particular, one must use the same compilers used to build R in order to extend or embed the R engine However, there is a known issue with the current RInside / Rcpp on Windows; but releases 0.2.1 and 0.7.1 do work

Dirk Eddelbuettel Seamless R and C++ Integration @ WU Wien, May 2010

slide-8
SLIDE 8

Extending R Rcpp Why ? The standard API Inline

Outline

1

Extending R Why ? The standard API Inline

2

Rcpp Overview New API Examples

Dirk Eddelbuettel Seamless R and C++ Integration @ WU Wien, May 2010

slide-9
SLIDE 9

Extending R Rcpp Why ? The standard API Inline

Motivation

  • Chambers. Software for

Data Analysis: Programming with R. Springer, 2008 Dirk Eddelbuettel Seamless R and C++ Integration @ WU Wien, May 2010

slide-10
SLIDE 10

Extending R Rcpp 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 and C++ Integration @ WU Wien, May 2010

slide-11
SLIDE 11

Extending R Rcpp 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 and C++ Integration @ WU Wien, May 2010

slide-12
SLIDE 12

Extending R Rcpp Why ? The standard API Inline

Motivation

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

Dirk Eddelbuettel Seamless R and C++ Integration @ WU Wien, May 2010

slide-13
SLIDE 13

Extending R Rcpp Why ? The standard API Inline

Motivation

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

Dirk Eddelbuettel Seamless R and C++ Integration @ WU Wien, May 2010

slide-14
SLIDE 14

Extending R Rcpp 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 and C++ Integration @ WU Wien, May 2010

slide-15
SLIDE 15

Extending R Rcpp 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 and C++ Integration @ WU Wien, May 2010

slide-16
SLIDE 16

Extending R Rcpp 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 and C++ Integration @ WU Wien, May 2010

slide-17
SLIDE 17

Extending R Rcpp 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 and C++ Integration @ WU Wien, May 2010

slide-18
SLIDE 18

Extending R Rcpp 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 and C++ Integration @ WU Wien, May 2010

slide-19
SLIDE 19

Extending R Rcpp 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 and C++ Integration @ WU Wien, May 2010

slide-20
SLIDE 20

Extending R Rcpp 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 and C++ Integration @ WU Wien, May 2010

slide-21
SLIDE 21

Extending R Rcpp 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 and C++ Integration @ WU Wien, May 2010

slide-22
SLIDE 22

Extending R Rcpp 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 and C++ Integration @ WU Wien, May 2010

slide-23
SLIDE 23

Extending R Rcpp 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 and C++ Integration @ WU Wien, May 2010

slide-24
SLIDE 24

Extending R Rcpp 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 and C++ Integration @ WU Wien, May 2010

slide-25
SLIDE 25

Extending R Rcpp Why ? The standard API Inline

Outline

1

Extending R Why ? The standard API Inline

2

Rcpp Overview New API Examples

Dirk Eddelbuettel Seamless R and C++ Integration @ WU Wien, May 2010

slide-26
SLIDE 26

Extending R Rcpp 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 and C++ Integration @ WU Wien, May 2010

slide-27
SLIDE 27

Extending R Rcpp 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 and C++ Integration @ WU Wien, May 2010

slide-28
SLIDE 28

Extending R Rcpp Why ? The standard API Inline

Compiled Code: The Basics cont.

The convolution function is called from R by

Dirk Eddelbuettel Seamless R and C++ Integration @ WU Wien, May 2010

slide-29
SLIDE 29

Extending R Rcpp 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 and C++ Integration @ WU Wien, May 2010

slide-30
SLIDE 30

Extending R Rcpp Why ? The standard API Inline

Example: Running the convolution code via .C

All these files are at http://dirk.eddelbuettel.com/code/rcppTut

Turn the C source file into a dynamic library using

R CMD SHLIB convolve.C.c

Dirk Eddelbuettel Seamless R and C++ Integration @ WU Wien, May 2010

slide-31
SLIDE 31

Extending R Rcpp Why ? The standard API Inline

Example: Running the convolution code via .C

All these files are at http://dirk.eddelbuettel.com/code/rcppTut

Turn the C source file into a dynamic library using

R CMD SHLIB convolve.C.c

Load it inside an R script or session using

dyn.load("convolve.C.so")

Dirk Eddelbuettel Seamless R and C++ Integration @ WU Wien, May 2010

slide-32
SLIDE 32

Extending R Rcpp Why ? The standard API Inline

Example: Running the convolution code via .C

All these files are at http://dirk.eddelbuettel.com/code/rcppTut

Turn the C source file into a dynamic library using

R CMD SHLIB convolve.C.c

Load it inside an R script or session using

dyn.load("convolve.C.so")

Use it via the .C() interface as shown on previous slide

Dirk Eddelbuettel Seamless R and C++ Integration @ WU Wien, May 2010

slide-33
SLIDE 33

Extending R Rcpp Why ? The standard API Inline

Example: Running the convolution code via .C

All these files are at http://dirk.eddelbuettel.com/code/rcppTut

Turn the C source file into a dynamic library using

R CMD SHLIB convolve.C.c

Load it inside an R script or session using

dyn.load("convolve.C.so")

Use it via the .C() interface as shown on previous slide All together in a helper file convolve.C.sh

#!/bin/sh R CMD SHLIB convolve.C.c cat convolve.C.call.R | R --no-save

Dirk Eddelbuettel Seamless R and C++ Integration @ WU Wien, May 2010

slide-34
SLIDE 34

Extending R Rcpp Why ? The standard API Inline

Compiled Code: The Basics cont.

Using .Call, the example becomes

Dirk Eddelbuettel Seamless R and C++ Integration @ WU Wien, May 2010

slide-35
SLIDE 35

Extending R Rcpp 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 and C++ Integration @ WU Wien, May 2010

slide-36
SLIDE 36

Extending R Rcpp 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 and C++ Integration @ WU Wien, May 2010

slide-37
SLIDE 37

Extending R Rcpp 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 and C++ Integration @ WU Wien, May 2010

slide-38
SLIDE 38

Extending R Rcpp 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 and C++ Integration @ WU Wien, May 2010

slide-39
SLIDE 39

Extending R Rcpp 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 and C++ Integration @ WU Wien, May 2010

slide-40
SLIDE 40

Extending R Rcpp Why ? The standard API Inline

Example: Running the convolution code via .Call

Turn the C source file into a dynamic library using

R CMD SHLIB convolve.Call.c

Dirk Eddelbuettel Seamless R and C++ Integration @ WU Wien, May 2010

slide-41
SLIDE 41

Extending R Rcpp Why ? The standard API Inline

Example: Running the convolution code via .Call

Turn the C source file into a dynamic library using

R CMD SHLIB convolve.Call.c

Load it inside an R script or session using

dyn.load("convolve.Call.so")

Dirk Eddelbuettel Seamless R and C++ Integration @ WU Wien, May 2010

slide-42
SLIDE 42

Extending R Rcpp Why ? The standard API Inline

Example: Running the convolution code via .Call

Turn the C source file into a dynamic library using

R CMD SHLIB convolve.Call.c

Load it inside an R script or session using

dyn.load("convolve.Call.so")

Use it via the .Call() interface as shown previously

Dirk Eddelbuettel Seamless R and C++ Integration @ WU Wien, May 2010

slide-43
SLIDE 43

Extending R Rcpp Why ? The standard API Inline

Example: Running the convolution code via .Call

Turn the C source file into a dynamic library using

R CMD SHLIB convolve.Call.c

Load it inside an R script or session using

dyn.load("convolve.Call.so")

Use it via the .Call() interface as shown previously All together in a helper file convolve.Call.sh

#!/bin/sh R CMD SHLIB convolve.Call.c cat convolve.Call.call.R | R --no-save

Dirk Eddelbuettel Seamless R and C++ Integration @ WU Wien, May 2010

slide-44
SLIDE 44

Extending R Rcpp Why ? The standard API Inline

Outline

1

Extending R Why ? The standard API Inline

2

Rcpp Overview New API Examples

Dirk Eddelbuettel Seamless R and C++ Integration @ WU Wien, May 2010

slide-45
SLIDE 45

Extending R Rcpp 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 and C++ Integration @ WU Wien, May 2010

slide-46
SLIDE 46

Extending R Rcpp 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 and C++ Integration @ WU Wien, May 2010

slide-47
SLIDE 47

Extending R Rcpp 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 and C++ Integration @ WU Wien, May 2010

slide-48
SLIDE 48

Extending R Rcpp Why ? The standard API Inline

Example: Convolution via .C with inline

Using the file convolve.C.inline.R

1 require ( i n l i n e ) 2 3 code <− " i n t i , j , nab = ∗na + ∗nb − 1; 4 5 f o r ( i = 0; i < nab ; i ++) 6 ab [ i ] = 0.0; 7 8 f o r ( i = 0; i < ∗na ; i ++) { 9 f o r ( j = 0; j < ∗nb ; j ++) 10 ab [ i + j ] += a [ i ] ∗ b [ j ] ; 11 } 12 " 13 14 fun <− cfunction ( signature ( a=" numeric " , na=" numeric " , 15 b=" numeric " , nb=" numeric " , 16 ab=" numeric " ) , 17 code , language="C" , convention=" .C" ) 18 19 fun (1:10 , 10 , 10:1 , 10 , numeric (19) ) $ab Dirk Eddelbuettel Seamless R and C++ Integration @ WU Wien, May 2010

slide-49
SLIDE 49

Extending R Rcpp Why ? The standard API Inline

Example: Convolution via .Call with inline

Using the file convolve.Call.inline.R

1 require ( i n l i n e ) 2 code <− " i n t i , j , na , nb , nab ; 3 double ∗xa , ∗xb , ∗xab ; 4 SEXP ab ; 5 6 PROTECT( a = AS_NUMERIC( a ) ) ; PROTECT( b = AS_NUMERIC( b ) ) ; 7 na = LENGTH( a ) ; nb = LENGTH( b ) ; nab = na + nb − 1; 8 PROTECT( ab = NEW_NUMERIC( nab ) ) ; 9 10 xa = NUMERIC_POINTER( a ) ; xb = NUMERIC_POINTER( b ) ; 11 xab = NUMERIC_POINTER( ab ) ; 12 f o r ( i = 0; i < nab ; i ++) xab [ i ] = 0.0; 13 14 f o r ( i = 0; i < na ; i ++) 15 f o r ( j = 0; j < nb ; j ++) 16 xab [ i + j ] += xa [ i ] ∗ xb [ j ] ; 17 18 UNPROTECT(3) ; 19 return ( ab ) ; " 20 21 fun <− cfunction ( signature ( a=" numeric " , b=" numeric " ) , 22 code , language="C" ) 23 24 fun (1:10 , 10:1) Dirk Eddelbuettel Seamless R and C++ Integration @ WU Wien, May 2010

slide-50
SLIDE 50

Extending R Rcpp Overview New API Examples

Outline

1

Extending R Why ? The standard API Inline

2

Rcpp Overview New API Examples

Dirk Eddelbuettel Seamless R and C++ Integration @ WU Wien, May 2010

slide-51
SLIDE 51

Extending R Rcpp Overview New API Examples

Compiled Code: Rcpp

In a nutshell:

Dirk Eddelbuettel Seamless R and C++ Integration @ WU Wien, May 2010

slide-52
SLIDE 52

Extending R Rcpp Overview New API Examples

Compiled Code: Rcpp

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

Dirk Eddelbuettel Seamless R and C++ Integration @ WU Wien, May 2010

slide-53
SLIDE 53

Extending R Rcpp 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 and C++ Integration @ WU Wien, May 2010

slide-54
SLIDE 54

Extending R Rcpp 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 and C++ Integration @ WU Wien, May 2010

slide-55
SLIDE 55

Extending R Rcpp 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 and C++ Integration @ WU Wien, May 2010

slide-56
SLIDE 56

Extending R Rcpp 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 and C++ Integration @ WU Wien, May 2010

slide-57
SLIDE 57

Extending R Rcpp Overview New API Examples

Example: Convolution using classic Rcpp

Using the file convolve.Call.Rcpp.classic.R

1 require ( i n l i n e ) 2 code <− ’ 3 RcppVector<double > xa ( a ) ; 4 RcppVector<double > xb ( b ) ; 5 6 i n t nab = xa . size ( ) + xb . size ( ) − 1; 7 RcppVector<double > xab ( nab ) ; 8 f o r ( i n t i = 0; i < nab ; i ++) xab ( i ) = 0.0; 9 10 f o r ( i n t i = 0; i < xa . size ( ) ; i ++) 11 f o r ( i n t j = 0; j < xb . size ( ) ; j ++) 12 xab ( i + j ) += xa ( i ) ∗ xb ( j ) ; 13 14 RcppResultSet rs ; 15 rs . add ( " ab " , xab ) ; 16 return rs . getReturnList ( ) ; 17 ’ 18 19 fun <− cppfunction ( signature ( a=" numeric " , b=" numeric " ) , code ) 20 fun (1:10 , 10:1) Dirk Eddelbuettel Seamless R and C++ Integration @ WU Wien, May 2010

slide-58
SLIDE 58

Extending R Rcpp Overview New API Examples

Outline

1

Extending R Why ? The standard API Inline

2

Rcpp Overview New API Examples

Dirk Eddelbuettel Seamless R and C++ Integration @ WU Wien, May 2010

slide-59
SLIDE 59

Extending R Rcpp 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 and C++ Integration @ WU Wien, May 2010

slide-60
SLIDE 60

Extending R Rcpp 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 and C++ Integration @ WU Wien, May 2010

slide-61
SLIDE 61

Extending R Rcpp 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 and C++ Integration @ WU Wien, May 2010

slide-62
SLIDE 62

Extending R Rcpp 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 and C++ Integration @ WU Wien, May 2010

slide-63
SLIDE 63

Extending R Rcpp 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 and C++ Integration @ WU Wien, May 2010

slide-64
SLIDE 64

Extending R Rcpp 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 and C++ Integration @ WU Wien, May 2010

slide-65
SLIDE 65

Extending R Rcpp Overview New API Examples

Functional programming in both languages

In R, functional programming is easy:

Dirk Eddelbuettel Seamless R and C++ Integration @ WU Wien, May 2010

slide-66
SLIDE 66

Extending R Rcpp 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 and C++ Integration @ WU Wien, May 2010

slide-67
SLIDE 67

Extending R Rcpp 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 that we let the STL transform function 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 <− cppfunction ( signature ( data=" l i s t " , fun = " function " ) , src ) Dirk Eddelbuettel Seamless R and C++ Integration @ WU Wien, May 2010

slide-68
SLIDE 68

Extending R Rcpp Overview New API Examples

Exception handling

Automatic catching and conversion of C++ exceptions:

Dirk Eddelbuettel Seamless R and C++ Integration @ WU Wien, May 2010

slide-69
SLIDE 69

Extending R Rcpp 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 <- cppfunction(signature(xs="numeric"), cpp) 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 and C++ Integration @ WU Wien, May 2010

slide-70
SLIDE 70

Extending R Rcpp Overview New API Examples

Exception handling: Usage

We attempted to automate forwarding of exceptions from the C++ layer to the R layer.

Dirk Eddelbuettel Seamless R and C++ Integration @ WU Wien, May 2010

slide-71
SLIDE 71

Extending R Rcpp Overview New API Examples

Exception handling: Usage

We attempted to automate forwarding of exceptions from the C++ layer to the R layer. This works (thanks to some gcc magic) on operating system with an X in their name, but not on Windows.

Dirk Eddelbuettel Seamless R and C++ Integration @ WU Wien, May 2010

slide-72
SLIDE 72

Extending R Rcpp Overview New API Examples

Exception handling: Usage

We attempted to automate forwarding of exceptions from the C++ layer to the R layer. This works (thanks to some gcc magic) on operating system with an X in their name, but not on Windows. We therefore once again recommend to wrap code with

try {

and

} catch( std::exception &ex) { forward_exception_to_r(ex); } catch(...) { ::Rf_error("c++ exception (unknown reason)"); }

Dirk Eddelbuettel Seamless R and C++ Integration @ WU Wien, May 2010

slide-73
SLIDE 73

Extending R Rcpp Overview New API Examples

Exception handling: Usage

We attempted to automate forwarding of exceptions from the C++ layer to the R layer. This works (thanks to some gcc magic) on operating system with an X in their name, but not on Windows. We therefore once again recommend to wrap code with

try {

and

} catch( std::exception &ex) { forward_exception_to_r(ex); } catch(...) { ::Rf_error("c++ exception (unknown reason)"); }

Because this is invariant, we provide macros BEGIN_RCPP and END_RCPP.

Dirk Eddelbuettel Seamless R and C++ Integration @ WU Wien, May 2010

slide-74
SLIDE 74

Extending R Rcpp Overview New API Examples

Exception handling: Usage

We attempted to automate forwarding of exceptions from the C++ layer to the R layer. This works (thanks to some gcc magic) on operating system with an X in their name, but not on Windows. We therefore once again recommend to wrap code with

try {

and

} catch( std::exception &ex) { forward_exception_to_r(ex); } catch(...) { ::Rf_error("c++ exception (unknown reason)"); }

Because this is invariant, we provide macros BEGIN_RCPP and END_RCPP. We provide a variant cppfunction of inline::cfunction which automatically inserts these at the beginning and end of the code snippets.

Dirk Eddelbuettel Seamless R and C++ Integration @ WU Wien, May 2010

slide-75
SLIDE 75

Extending R Rcpp Overview New API Examples

Outline

1

Extending R Why ? The standard API Inline

2

Rcpp Overview New API Examples

Dirk Eddelbuettel Seamless R and C++ Integration @ WU Wien, May 2010

slide-76
SLIDE 76

Extending R Rcpp Overview New API Examples

Example: Convolution using new Rcpp

Using the file convolve.Call.Rcpp.new.R

1 require ( i n l i n e ) 2 3 code <− ’ 4 Rcpp : : NumericVector xa ( a ) ; / / automatic conversion from SEXP 5 Rcpp : : NumericVector xb ( b ) ; 6 7 i n t n_xa = xa . size ( ) ; 8 i n t n_xb = xb . size ( ) ; 9 i n t nab = n_xa + n_xb − 1; 10 11 Rcpp : : NumericVector xab ( nab ) ; 12 13 f o r ( i n t i = 0; i < n_xa ; i ++) 14 f o r ( i n t j = 0; j < n_xb ; j ++) 15 xab [ i + j ] += xa [ i ] ∗ xb [ j ] ; 16 17 return xab ; / / automatic conversion to SEXP 18 ’ 19 20 fun <− cppfunction ( signature ( a=" numeric " , b=" numeric " ) , code ) 21 22 fun (1:10 , 10:1) Dirk Eddelbuettel Seamless R and C++ Integration @ WU Wien, May 2010

slide-77
SLIDE 77

Extending R Rcpp Overview New API Examples

Speed comparison

See the directory Rcpp/examples/ConvolveBenchmarks

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

Dirk Eddelbuettel Seamless R and C++ Integration @ WU Wien, May 2010

slide-78
SLIDE 78

Extending R Rcpp Overview New API Examples

Speed comparison

See the directory Rcpp/examples/ConvolveBenchmarks

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 and C++ Integration @ WU Wien, May 2010

slide-79
SLIDE 79

Extending R Rcpp Overview New API Examples

Speed comparison

See the directory Rcpp/examples/ConvolveBenchmarks

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 and C++ Integration @ WU Wien, May 2010

slide-80
SLIDE 80

Extending R Rcpp 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 and C++ Integration @ WU Wien, May 2010

slide-81
SLIDE 81

Extending R Rcpp 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 and C++ Integration @ WU Wien, May 2010

slide-82
SLIDE 82

Extending R Rcpp 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 and C++ Integration @ WU Wien, May 2010

slide-83
SLIDE 83

Extending R Rcpp 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 and C++ Integration @ WU Wien, May 2010

slide-84
SLIDE 84

Extending R Rcpp 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 and C++ Integration @ WU Wien, May 2010

slide-85
SLIDE 85

Extending R Rcpp Overview New API Examples

Linear regression via GSL: lmGSL()

See the directory Rcpp/examples/FastLM

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 cppfunction ( signature ( Ysexp=" numeric " , 49 Xsexp=" numeric " ) , src , 50 includes= 51 "# include <gsl / gsl _ m u l t i f i t . h>" , 52 cppargs="−I / usr / include " , 53 l i b a r g s="− l g s l −l g s l c b l a s " ) 54 } Dirk Eddelbuettel Seamless R and C++ Integration @ WU Wien, May 2010

slide-86
SLIDE 86

Extending R Rcpp Overview New API Examples

Linear regression via Armadillo: lmArmadillo example

Also see the directory Rcpp/examples/FastLM

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 <− cppfunction ( signature ( Ysexp=" numeric " , Xsexp=" numeric " ) , 25 src , includes="# include <armadillo >" , 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 and C++ Integration @ WU Wien, May 2010

slide-87
SLIDE 87

Extending R Rcpp Overview New API Examples

Linear regression via Armadillo: RcppArmadillo

See fastLm in the RcppArmadillo package

fastLm in the new RcppArmadillo release does even better:

1 #include <RcppArmadillo . h> 2 extern "C" SEXP fastLm (SEXP ys , SEXP Xs) { 3 try { 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 res = y − X∗coef ; / / residuals 13 14 double s2 = std : : inner _product ( res . begin ( ) , res . end ( ) , res . begin ( ) , double ( ) ) / (n −k ) ; 15 / / std . errors

  • f

c o e f f i c i e n t s 16 arma : : colvec stderr = arma : : sqrt ( s2∗arma : : diagvec ( arma : : inv ( arma : : trans (X)∗X) ) ) ; 17 18 return Rcpp : : L i s t : : create (Rcpp : : Named( " c o e f f i c i e n t s " ) = coef , 19 Rcpp : : Named( " stderr " ) = stderr , 20 Rcpp : : Named( " df " ) = n − k ) ; 21 } catch ( std : : exception &ex ) { 22 forward_exception_to_ r ( ex ) ; 23 } catch ( . . . ) { 24 : : Rf_ error ( " c++ exception ( unknown reason ) " ) ; 25 } 26 return R_NilValue ; / / −Wall 27 } Dirk Eddelbuettel Seamless R and C++ Integration @ WU Wien, May 2010

slide-88
SLIDE 88

Extending R Rcpp Overview New API Examples

Linear regression via GNU GSL: RcppGSL

See fastLm in the RcppGSL package (on R-Forge)

We also wrote fastLm in a new package RcppGSL:

1 extern "C" SEXP fastLm (SEXP ys , SEXP Xs) { 2 BEGIN_RCPP 3 RcppGSL : : vector <double> y = ys ; / / create gsl data structures from SEXP 4 RcppGSL : : matrix <double> X = Xs ; 5 int n = X. nrow ( ) , k = X. ncol ( ) ; 6 double chisq ; 7 RcppGSL : : vector <double> coef ( k ) ; / / to hold the c o e f f i c i e n t vector 8 RcppGSL : : matrix <double> cov ( k , k ) ; / / and the covariance matrix 9 / / the actual f i t requires working memory we a l l o c a t e and free 10 gsl _ m u l t i f i t _ l i n e a r _workspace ∗work = gsl _ m u l t i f i t _ l i n e a r _ a l l o c (n , k ) ; 11 gsl _ m u l t i f i t _ l i n e a r (X, y , coef , cov , &chisq , work ) ; 12 gsl _ m u l t i f i t _ l i n e a r _ free ( work ) ; 13 / / e x t rac t the diagonal as a vector view 14 gsl _vector_view diag = gsl _matrix_diagonal ( cov ) ; 15 / / c u r r e n t l y there i s not a more d i r e c t i n t e r f a c e in Rcpp : : NumericVector 16 / / that takes advantage of wrap , so we have to do i t in two steps 17 Rcpp : : NumericVector stderr ; stderr = diag ; 18 std : : transform ( stderr . begin ( ) , stderr . end ( ) , stderr . begin ( ) , sqrt ) ; 19 Rcpp : : L i s t res = Rcpp : : L i s t : : create (Rcpp : : Named( " c o e f f i c i e n t s " ) = coef , 20 Rcpp : : Named( " stderr " ) = stderr , 21 Rcpp : : Named( " df " ) = n − k ) ; 22 / / free a l l the GSL vectors and matrices − − as these are r e a l l y C data structures 23 / / we cannot take advantage of automatic memory management 24 coef . free ( ) ; cov . free ( ) ; y . free ( ) ; X. free ( ) ; 25 return res ; / / return the r e s u l t l i s t to R 26 END_RCPP 27 } Dirk Eddelbuettel Seamless R and C++ Integration @ WU Wien, May 2010

slide-89
SLIDE 89

Extending R Rcpp 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 and C++ Integration @ WU Wien, May 2010

slide-90
SLIDE 90

Extending R Rcpp 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 and C++ Integration @ WU Wien, May 2010

slide-91
SLIDE 91

Extending R Rcpp Overview New API Examples

Possible gains from template meta-programming

Armadillo uses delayed evaluation (via recursive template and template meta-programming) to combine several operations into one expression reducing / eliminating temporary objects.

Dirk Eddelbuettel Seamless R and C++ Integration @ WU Wien, May 2010

slide-92
SLIDE 92

Extending R Rcpp Overview New API Examples

Possible gains from template meta-programming

Armadillo uses delayed evaluation (via recursive template and template meta-programming) to combine several operations into one expression reducing / eliminating temporary objects.

Operation Relative performance improvement for small matrices medium to large IT++ Newmat IT++ Newmat A + B 15.0 10.0 3.5 1.0 A + B + C + D 15.0 10.0 6.0 1.5 A * B * C * D 2.5 10.0 2.5 20.0 B.row(size-1) = A.row(0) 16.0 44.0 2.0 4.5 trans(p)*inv(diagmat(q))*r 77.0 23.0 1086.0 5.0

Table 2: Gains from C++ template programming

See http://arma.sourceforge.net/speed.html for details. Dirk Eddelbuettel Seamless R and C++ Integration @ WU Wien, May 2010

slide-93
SLIDE 93

Extending R Rcpp

Outline

1

Extending R Why ? The standard API Inline

2

Rcpp Overview New API Examples

Dirk Eddelbuettel Seamless R and C++ Integration @ WU Wien, May 2010

slide-94
SLIDE 94

Extending R Rcpp

From RApache to littler to RInside

See the file RInside/standard/rinside_sample0.cpp

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 and C++ Integration @ WU Wien, May 2010

slide-95
SLIDE 95

Extending R Rcpp

From RApache to littler to RInside

See the file RInside/standard/rinside_sample0.cpp

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 and C++ Integration @ WU Wien, May 2010

slide-96
SLIDE 96

Extending R Rcpp

Another simple example

See RInside/standard/rinside_sample8.cpp (in SVN, older version in pkg)

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 and C++ Integration @ WU Wien, May 2010

slide-97
SLIDE 97

Extending R Rcpp

A finance example

See the file RInside/standard/rinside_sample4.cpp (edited)

1 #include <RInside . h> / / for the embedded R via RInside 2 #include <iomanip > 3 int main ( int argc , char ∗argv [ ] ) { 4 RInside R( argc , argv ) ; / / create an embedded R instance 5 SEXP ans ; 6

  • R. parseEvalQ ( " suppressMessages ( l i b r a r y ( f P o r t f o l i o ) ) " ) ;

7 t x t = " lppData <− 100 ∗ LPP2005 .RET[ , 1 : 6 ] ; " 8 "ewSpec <− portfolioSpec ( ) ; nAssets <− ncol ( lppData ) ; " ; 9

  • R. parseEval ( txt ,

ans ) ; / / prepare problem 10 const double dvec [ 6 ] = { 0.1 , 0.1 , 0.1 , 0.1 , 0.3 , 0.3 } ; / / weights 11 const std : : vector <double> w( dvec , &dvec [ 6 ] ) ; 12

  • R. assign ( w,

" weightsvec " ) ; / / assign STL vec to R ’ s ’ weightsvec ’ 13 14

  • R. parseEvalQ ( " setWeights (ewSpec) <− weightsvec " ) ;

15 t x t = " ewPortfolio <− f e a s i b l e P o r t f o l i o ( data = lppData , spec = ewSpec , " 16 " constraints = \ " LongOnly \ " ) ; " 17 " p r i n t ( ewPortfolio ) ; " 18 " vec <− getCovRiskBudgets ( ewPortfolio@portfolio ) " ; 19 ans = R. parseEval ( t x t ) ; / / assign covRiskBudget weights to ans 20 Rcpp : : NumericVector V( ans ) ; / / convert SEXP variable to an RcppVector 21 22 ans = R. parseEval ( " names( vec ) " ) ; / / assign columns names to ans 23 Rcpp : : CharacterVector n ( ans ) ; 24 25 f o r ( i n t i =0; i <names . size ( ) ; i ++) { 26 std : : cout << std : : setw (16) << n [ i ] << " \ t " << std : : setw (11) << V[ i ] << " \ n " ; 27 } 28 e x i t (0) ; 29 } Dirk Eddelbuettel Seamless R and C++ Integration @ WU Wien, May 2010

slide-98
SLIDE 98

Extending R Rcpp

And another parallel example

See the file RInside/mpi/rinside_mpi_sample2.cpp

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 and C++ Integration @ WU Wien, May 2010

slide-99
SLIDE 99

Extending R Rcpp

RInside workflow

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

Dirk Eddelbuettel Seamless R and C++ Integration @ WU Wien, May 2010

slide-100
SLIDE 100

Extending R Rcpp

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 and C++ Integration @ WU Wien, May 2010

slide-101
SLIDE 101

Extending R Rcpp

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 and C++ Integration @ WU Wien, May 2010

slide-102
SLIDE 102

Extending R Rcpp

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 and C++ Integration @ WU Wien, May 2010

slide-103
SLIDE 103

Extending R Rcpp

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 and C++ Integration @ WU Wien, May 2010

slide-104
SLIDE 104

Extending R Rcpp

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 and C++ Integration @ WU Wien, May 2010

slide-105
SLIDE 105

Extending R Rcpp

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 and C++ Integration @ WU Wien, May 2010

slide-106
SLIDE 106

Extending R Rcpp

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 and C++ Integration @ WU Wien, May 2010

slide-107
SLIDE 107

Extending R Rcpp

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 and C++ Integration @ WU Wien, May 2010

slide-108
SLIDE 108

Extending R Rcpp

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

nine different examples in examples/standard

Dirk Eddelbuettel Seamless R and C++ Integration @ WU Wien, May 2010

slide-109
SLIDE 109

Extending R Rcpp

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

nine different examples in examples/standard four different examples in examples/mpi

Dirk Eddelbuettel Seamless R and C++ Integration @ WU Wien, May 2010

slide-110
SLIDE 110

Extending R Rcpp

Outline

1

Extending R Why ? The standard API Inline

2

Rcpp Overview New API Examples

Dirk Eddelbuettel Seamless R and C++ Integration @ WU Wien, May 2010

slide-111
SLIDE 111

Extending R Rcpp

About Google ProtoBuf

Quoting from the page at Google Code:

Protocol buffers are a flexible, efficient, automated mechanism for serializing structured data—think XML, but smaller, faster, and simpler.

Dirk Eddelbuettel Seamless R and C++ Integration @ WU Wien, May 2010

slide-112
SLIDE 112

Extending R Rcpp

About Google ProtoBuf

Quoting from the page at Google Code:

Protocol buffers are a flexible, efficient, automated mechanism for serializing structured data—think XML, but smaller, faster, and simpler. You define how you want your data to be structured once, then you can use special generated source code to easily write and read your structured data to and from a variety of data streams and using a variety of languages.

Dirk Eddelbuettel Seamless R and C++ Integration @ WU Wien, May 2010

slide-113
SLIDE 113

Extending R Rcpp

About Google ProtoBuf

Quoting from the page at Google Code:

Protocol buffers are a flexible, efficient, automated mechanism for serializing structured data—think XML, but smaller, faster, and simpler. You define how you want your data to be structured once, then you can use special generated source code to easily write and read your structured data to and from a variety of data streams and using a variety of languages. You can even update your data structure without breaking deployed programs that are compiled against the "old" format.

Google provides native bindings for C++, Java and Python.

Dirk Eddelbuettel Seamless R and C++ Integration @ WU Wien, May 2010

slide-114
SLIDE 114

Extending R Rcpp

Google ProtoBuf

1 R> library ( RProtoBuf ) ## load the package 2 R> readProtoFiles ( " addressbook . proto " ) ## acquire protobuf information 3 R> bob <− new( t u t o r i a l . Person , ## create new object 4 + email = "bob@example .com" , 5 + name = "Bob" , 6 + id = 123 ) 7 R> writeLines ( bob$ t o St ri n g ( ) ) ## s e r i a l i z e to stdout 8 name: "Bob" 9 id : 123 10 email : "bob@example .com" 11 12 R> bob$email ## access and / or

  • verride

13 [ 1 ] "bob@example .com" 14 R> bob$ id <− 5 15 R> bob$ id 16 [ 1 ] 5 17 18 R> s e r i a l i z e ( bob , " person . pb " ) ## s e r i a l i z e to compact binary format

Under the hood, Rcpp is used and works very well in conjunction with the rich C++ API provided by Google.

Dirk Eddelbuettel Seamless R and C++ Integration @ WU Wien, May 2010

slide-115
SLIDE 115

Extending R Rcpp

Outline

1

Extending R Why ? The standard API Inline

2

Rcpp Overview New API Examples

Dirk Eddelbuettel Seamless R and C++ Integration @ WU Wien, May 2010

slide-116
SLIDE 116

Extending R Rcpp

Users of Rcpp

RInside uses Rcpp for object transfer and more

Dirk Eddelbuettel Seamless R and C++ Integration @ WU Wien, May 2010

slide-117
SLIDE 117

Extending R Rcpp

Users of Rcpp

RInside uses Rcpp for object transfer and more RcppArmadillo and RcppGSL (which contain fastLm())

Dirk Eddelbuettel Seamless R and C++ Integration @ WU Wien, May 2010

slide-118
SLIDE 118

Extending R Rcpp

Users of Rcpp

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

Dirk Eddelbuettel Seamless R and C++ Integration @ WU Wien, May 2010

slide-119
SLIDE 119

Extending R Rcpp

Users of Rcpp

RInside uses Rcpp for object transfer and more RcppArmadillo and RcppGSL (which contain 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 and C++ Integration @ WU Wien, May 2010

slide-120
SLIDE 120

Extending R Rcpp

Users of Rcpp

RInside uses Rcpp for object transfer and more RcppArmadillo and RcppGSL (which contain 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 and C++ Integration @ WU Wien, May 2010

slide-121
SLIDE 121

Extending R Rcpp

Users of Rcpp

RInside uses Rcpp for object transfer and more RcppArmadillo and RcppGSL (which contain 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 and C++ Integration @ WU Wien, May 2010

slide-122
SLIDE 122

Extending R Rcpp

Users of Rcpp

RInside uses Rcpp for object transfer and more RcppArmadillo and RcppGSL (which contain 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, pcaMethods (BioC), phylobase are truly external users which are all on CRAN

Dirk Eddelbuettel Seamless R and C++ Integration @ WU Wien, May 2010

slide-123
SLIDE 123

Extending R Rcpp

Users of Rcpp

RInside uses Rcpp for object transfer and more RcppArmadillo and RcppGSL (which contain 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, pcaMethods (BioC), phylobase are truly external users which are all on CRAN upcoming: possibly lme4a

Dirk Eddelbuettel Seamless R and C++ Integration @ WU Wien, May 2010

slide-124
SLIDE 124

Extending R Rcpp

Users of Rcpp

RInside uses Rcpp for object transfer and more RcppArmadillo and RcppGSL (which contain 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, pcaMethods (BioC), phylobase are truly external users which are all on CRAN upcoming: possibly lme4a Your package here next?

Dirk Eddelbuettel Seamless R and C++ Integration @ WU Wien, May 2010

slide-125
SLIDE 125

Extending R Rcpp

Outline

1

Extending R Why ? The standard API Inline

2

Rcpp Overview New API Examples

Dirk Eddelbuettel Seamless R and C++ Integration @ WU Wien, May 2010

slide-126
SLIDE 126

Extending R Rcpp

Wrapping up

This tutorial has tried to show you that

Dirk Eddelbuettel Seamless R and C++ Integration @ WU Wien, May 2010

slide-127
SLIDE 127

Extending R Rcpp

Wrapping up

This tutorial has tried to show 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 and C++ Integration @ WU Wien, May 2010

slide-128
SLIDE 128

Extending R Rcpp

Wrapping up

This tutorial has tried to show 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 and C++ Integration @ WU Wien, May 2010

slide-129
SLIDE 129

Extending R Rcpp

Wrapping up

This tutorial has tried to show 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 and C++ Integration @ WU Wien, May 2010

slide-130
SLIDE 130

Extending R Rcpp

Wrapping up

This tutorial has tried to show 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 and C++ Integration @ WU Wien, May 2010

slide-131
SLIDE 131

Extending R Rcpp

Wrapping up

This tutorial has tried to show 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 and C++ Integration @ WU Wien, May 2010

slide-132
SLIDE 132

Extending R Rcpp

Wrapping up

This tutorial has tried to show 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 and C++ Integration @ WU Wien, May 2010

slide-133
SLIDE 133

Extending R Rcpp

Wrapping up

This tutorial has tried to show 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 and C++ Integration @ WU Wien, May 2010

slide-134
SLIDE 134

Extending R Rcpp

Outline

1

Extending R Why ? The standard API Inline

2

Rcpp Overview New API Examples

Dirk Eddelbuettel Seamless R and C++ Integration @ WU Wien, May 2010

slide-135
SLIDE 135

Extending R Rcpp

Some pointers

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

Dirk Eddelbuettel Seamless R and C++ Integration @ WU Wien, May 2010

slide-136
SLIDE 136

Extending R Rcpp

Some pointers

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

Dirk Eddelbuettel Seamless R and C++ Integration @ WU Wien, May 2010

slide-137
SLIDE 137

Extending R Rcpp

Some pointers

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

Dirk Eddelbuettel Seamless R and C++ Integration @ WU Wien, May 2010

slide-138
SLIDE 138

Extending R Rcpp

Some pointers

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

Dirk Eddelbuettel Seamless R and C++ Integration @ WU Wien, May 2010

slide-139
SLIDE 139

Extending R Rcpp

Some pointers

http://dirk.eddelbuettel.com/code/rcpp.html http://dirk.eddelbuettel.com/code/rcppTut/ 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 and C++ Integration @ WU Wien, May 2010

slide-140
SLIDE 140

Extending R Rcpp

Some pointers

http://dirk.eddelbuettel.com/code/rcpp.html http://dirk.eddelbuettel.com/code/rcppTut/ 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 and C++ Integration @ WU Wien, May 2010

slide-141
SLIDE 141

Extending R Rcpp

The end Thank you!

Dirk Eddelbuettel Seamless R and C++ Integration @ WU Wien, May 2010