ABSTRACT

Chapter 25 shows how to improve the performance by rewriting R code in C++ via the Rcpp package.

Rcpp makes it very simple to connect C++ to R. While it is possible to write C or Fortran code for use in R, it will be painful by comparison. Rcpp provides a clean, approachable API that lets you write high-performance code, insulated from R’s complex C API.

Typical bottlenecks that C++ can address include:

Loops that can’t be easily vectorised because subsequent iterations depend on previous ones.

Recursive functions, or problems which involve calling functions millions of times. The overhead of calling a function in C++ is much lower than in R.

Problems that require advanced data structures and algorithms that R doesn’t provide. Through the standard template library (STL), C++ has efficient implementations of many important data structures, from ordered maps to double-ended queues.

“Getting started with C++” teaches you how to write C++ by converting simple R functions to their C++ equivalents. You’ll learn how C++ differs from R, and what the key scalar, vector, and matrix classes are called. The Subsection “Using sourceCpp” shows you how to use sourceCpp() to load a C++ file from disk in the same way you use source() to load a file of R code. “Other classes” discusses how to modify attributes from Rcpp, and mentions some of the other important classes. “Missing values” teaches you how to work with R’s missing values in C++. “Standard Template Library” shows you how to use some of the most important data structures and algorithms from the standard template library, or STL, built-in to C++. “Case studies” shows two real case studies where Rcpp was used to get considerable performance improvements. “Using Rcpp in a package” teaches you how to add C++ code to a package. “Learning more” concludes the chapter with pointers to more resources to help you learn Rcpp and C++.