What is Rcpp in R programming

Japhilko / RInterfaces

Jan-Philipp Kolb
9 May 2017

Why the integration of c ++

Robert Gentleman, in R Programming for Bioinformatics, 2008, about R's built-in C interfaces:

Since R is not compiled, in some situations its performance can be substantially improved by writing code in a compiled language. There are also reasons not to write code in other languages, and in particular we caution against premature optimization, prototyping in R is often cost effective. And in our experience very few routines need to be implemented in other languages ​​for effiiency reasons. Another substantial reason not to use an implementation in some other language is increased complexity. The use of another language almost always results in higher maintenance costs and less stability. In addition, any extensions or enhancements of the code will require someone that is proficient in both R and the other language.

  • Rcpp does make some of the above caution statements slightly less critical.

Why and when

  • Why? - R becomes slow or has problems with memory management: for example with loops that cannot be vectorized.

  • When? - if you can't do it better with Rcode and you have identified the slow code.

Compiler requirement

For Windows, Rtools

  1. http://cran.r-project.org/bin/windows/Rtools/
  2. http://cran.r-project.org/doc/manuals/R-admin.html#The-Windows-toolset

For Mac, Xcode

  1. http://cran.r-project.org/doc/manuals/R-admin.html#Installing-R-under-_0028Mac_0029-OS-X
  2. http://cran.r-project.org/doc/manuals/R-admin.html#Mac-OS-X

What we will use

We will use the following two packages:

  • and the one for writing Inline C code that is compiled on-the-fly (there is also one for C ++ code).

  • , and the use of the function

is the most downloaded package

The package

install.packages ("inline")
library (inline) citation ("inline")



  • R is written in C.
  • The use of the interface to C is obvious

The R package CPP

library (Rcpp) cppFunction ('int add (int x, int y, int z) {int sum = x + y + z; return sum;}') # add works like a regular R functionadd


Tutorial on Rcpp by Hadley Wickham

cppFunction ('int add (int x, int y, int z) {int sum = x + y + z; return sum;}')


install.packages ("microbenchmark")