This Week on the STOR-i Programme: Rcpp

This week I thought I would write a slightly different blog post. Instead of discussing an area of Statistics or Operational Research I have recently learnt about, I will be discussing a useful tool for Statistics or Operational Research I have recently learnt about. This tool is an R package, Rcpp.

What does Rcpp do?

Rcpp is a package which allows us to connect R and C++ code. In particular I will discuss using it to develop functions in C++ which can then be used in R.

Why do we want to do this?

If you have ever used C++ and are like me you will think that it is quite painful to code in. Writing in C++ can be a lot more time consuming than writing in R. R is also full of lots of packages and functions which can make whatever maths you are doing a lot nicer. You may be wondering if R is easier to write in why are we developing our functions in C++. The answer is relatively simple: speed. R tends to be used in an interpreter whereas C++ is compiled. Interpreters run each line of code at a time whereas a complier processes all the code at once. If you are doing a large task in R interpreting one line at a time may take a while. Creating a function in C++ that is compiled then able to use in R can speed up the program by more than 100 times faster.

How do we do this?

There may be different things to consider depending on what you are doing with your function. But for now lets just talk about the basics. There are two ways to go about this, one is to source your C++ file and another is to create an R package of your C++. Either way we need to add a few lines to our C++ code.

At the begin of your C++ code include:

The next line of code should be added just before the function that you wish to use. This is so Rcpp knows what function to include.

Sourcing your C++

Sourcing C++ in R is simple. Just open R in the directory you have saved your C++ file in. Install and library the Rcpp package, then source your file.

One issue is you have to repeat this every time you want to use it so if it’s a function you will be using multiple times it may be easier to make a package. This way you can also send it to other people to use a lot easier.

Making a Package

First, in R you need to library Rcpp and build the skeleton of your package.

Then in your terminal build and install the package.

You then have a package you can use in R.

Things to think about

Rcpp is good at converting common data structures. However, if you have defined your own data structures then you will bump into some issues. One way to remedy this is to have an input and output of your function be a common data type and convert to your defined data structure within your function.

Adding to your package

You may find that you want to edit or add to your package which you can do with the following simple steps:

Edit the file in your package or add a new file to your package folder.

Open R with your package as the directory library Rcpp and run the following line:

Rebuild your package and you are good to go.

I hope you find this useful! To learn more about the Rcpp package look here or here.