25.1 Introduction

In this chapter we’ll learn how to rewrite R code in C++ to make it faster using the Rcpp package. The Rcpp package makes it simple to connect C++ to R! With C++ you can fix:

  • 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 queue

Like how?