Daniel Hanson is a full-time lecturer in the Computational Finance & Risk Management program within the Department of Applied Mathematics at the University of Washington. His appointment followed over 25 years of experience in private sector quantitative development in finance and data science.
One of the most time-consuming, tedious, and thankless tasks a quantitative developer frequently confronts is writing interfaces to C++ code from applications such as Excel, Python, and R. Fortunately, this process has been made far less painful when interfacing to a front-end in R, thanks to the
Rcpp was first developed by Dirk Eddelbeuttel and Romain Francois about ten years ago. It has since evolved significantly under Dirk’s direction in terms of rapid development, and build and documentation tools, which are now conveniently integrated into in RStudio.
Rcpp represents a major breakthrough in allowing a programmer to farm out computationally-intensive tasks to C++, return the results to R, and then use them as arguments to other R functions, including R’s powerful data visualization tools. Although there is quite a bit of documentation on using
Rcpp, there does not seem to be much written either in print, or online, regarding C++ best practices.
This post is the first in a series of posts in which I will address the best practice of developing Rcpp implementations that keep both reusable and standard C++ code separate from the R interface. Additionally, I will also cover a subtheme that does not seem to get a lot of press, namely using
Rcpp in a Windows environment. Linux of course is where the action is, and for good reason, but the reality remains that when a quant developer takes a new job, on the new hire’s desk will almost surely be a Windows notebook or desktop computer, and s/he will be expected to produce applications that can run on Windows machines used by managers and colleagues. So, we will first look at how to set up an
Rcpp development environment on Windows 10, including some of the peculiarities and limitations that are involved, but which will not significantly prevent a developer from being highly productive in writing R packages with integrated reusable and standard C++ code.
Setting up on Windows 10
As with an
Rcpp development environment on Linux or the Mac, in order to take advantage of the latest features in C++, as well as the convenient package build tools in RStudio, a Windows user will need the following:
- An R installation
- A C++ compiler
For Windows users, however, the R interface is not compatible with Microsoft’s Visual Studio C++ compiler, due to R itself being built with the GNU gcc compiler. Fortunately, there is a very easy way to make one’s Windows environment compatible. This is where
Rtools comes in.
Rtools contains a collection of utilities and libraries for building R packages on Windows. More specifically, for integrating C++ code with
Rcpp, beginning with version 4.0, Rtools now contains support for the gcc 8.3.0 compiler on Windows. The
Rtools installation provides reliable configuration of the gcc compiler that is quick and painless; however, it does come with a small compromise.
The gcc 8.3.0 compiler was released in February 2019, and it has since been superseded by its most recent stable release (9.3). As such, it is fully up to date with C++14 specifications, but it only supports a partial list of language and Standard Library features in C++17. Two specific items very useful for quant development are available:
- Special math functions (Bessel functions, Legendre polynomials, etc)
- New types
However, the gcc compiler lacks the big daddy of them all, parallel STL algorithms. A comprehensive list of C++17 features supported in gcc 8.3.0 can be found here.
Rtools saves Windows users from considerable time and effort in configuring one’s machine for not just the gcc compiler, but also integrating (mostly) modern C++ code in R packages, it is advised that newcomers stick to
Rtools and gcc 8.3.0.
It is very important to note that:
- You will need version 4.0.0 of R or above to install
- Unless there is a compelling reason otherwise, you should install the 64-bit version of Rtools:
Configuration in RStudio
After installing R and
Rtools, as well as RStudio (v 1.3.959 or later recommended), you can now complete the configuration of your development environment for integrated R/C++ package development.
In order to use the C++17 features available in
Rtools, as well as C++14, you need to change the settings in the
Makeconf file, located under your R installation (not Rtools), as follows:
- In Windows File Explorer, go to your R installation and click down to the
- Locate the
Makeconffile, and copy it to a backup file (always a prudent idea), e.g.
- Using a standard text editor (eg Notepad++), open the
- Locate the line
CXX = $(BINPREF)g++ -std=gnu++11 $(M_ARCH)
- Change the
CXX = $(BINPREF)g++ -std=gnu++17 $(M_ARCH)
- Save the updated file.
Install the Rcpp Package
Installing Rcpp is no different from any other typical R package. Either use the R command:
Or, use the
Tools/Install Packages... selection in RStudio.
After completing the above, you will now have a development environment ready to start designing and developing an R package containing standard and reusable C++ code. In the next post, we will first spend a little time examining design considerations, before we begin actual code implementation.
You may leave a comment below or discuss the post in the forum community.rstudio.com.