Get startedGet started for free

Welcome to the course

1. Welcome to the course

Welcome to this course, my name is Romain François, I am one of the authors of the Rcpp package and I will be your instructor.

2. R vs C++

R is a great language, but sometimes it is slow. That is because R is an interpreted language. R expressions are parsed and interpreted at runtime. On the other hand, C++ is a compiled language. The C++ compilers do a great job of translating C++ statements into machine code that runs much faster.

3. Motivation

In this course, you will learn how to use the Rcpp package to make your code faster, by writing critical parts in C++. You don't need to know everything there is to know about C++, but only a small subset. It has always been possible to use C or C++ in your R code, but Rcpp makes the process very easy. You only have to focus on writing simple C++ code, and Rcpp takes care of converting R objects to C++ and vice versa.

4. Course structure

The course is divided into 4 chapters that will gradually equip you with skills to take advantage of C++ in your R code. The first chapter covers basic C++ syntax. The second chapter is about C++ functions. You will learn how to write them, and how to make them available to R. Most of the time, using Rcpp consists of manipulating R vectors, you will learn in the third chapter on how to access existing R vectors and create new ones. Finally, in the last chapter, you'll practice your new skills in mini case studies.

5. Measure performance

When you use Rcpp, you typically want to keep the main logic of your code in R, and resort to C++ for data-intensive work or when your R code cannot be easily expressed with vectorized functions. The microbenchmark package will help you to compare the performance of two or more versions of your code. For example, the initial R code and the supposedly optimized C++ version. + As illustrated here, microbenchmark takes any number of R expressions, evaluates them 100 times each and then summarises the information. We typically only look at the median column.

6. More information about benchmarking and profiling

For more information about benchmarking and profiling, that is finding the actual bottlenecks in your code, please refer to take the DataCamp course, "Writing efficient R code", by Colin Gillespie.

7. Evaluating simple C++ expressions with evalCpp

As a compiled language, C++ does not offer a console for interactive use, but Rcpp has a few utilities to mimic interactive use. The evalCpp function takes a string of valid C++ code, compiles it under the hood and evaluates it. This is often used to check if a machine has been properly set up to use Rcpp.

8. Basic number types

C++ has a rich set of basic number types, you will only use integers (of type int) and floating point numbers (of type double) because those are the types used in R.

9. Basic number types

There is a difference between R and C++ about what are literal numbers. In R, literal integer numbers are doubles by default, and you have to convert them explicitly to integers using either "as.integer()" or with the capital L suffix. In C++ literal integer numbers are the integer and you can suffix them with ".0" to force them to be a double.

10. Casting

To convert explicitly to a double, you can also use casting, with the syntax presented here, the name of the target type, here double, between parentheses before the object being cast. Casting to a double is sometimes necessary, for example, C++ uses integer division when both operands are int, whereas R promotes automatically to doubles when needed.

11. Let's practice!

Now it's your turn to write some basic C++ code.