Press "Enter" to skip to content

R Package Integration with Modern Reusable C++ Code Using Rcpp

[This article was first published on R Views, and kindly contributed to R-bloggers]. (You can report issue about the content on this page here)


Want to share your content on R-bloggers? click here if you have a blog, or here if you don’t.

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 package.

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

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

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 std::variant, std::optional, and std::any

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.

Still, as 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 Rtools 4.0
  • Unless there is a compelling reason otherwise, you should install the 64-bit version of Rtools: rtools40-x86_64.exe

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.

Enable C++17

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 .../R-4.0.0/etc/x64 subdirectory
  • Locate the Makeconf file, and copy it to a backup file (always a prudent idea), e.g. Makeconf.bak
  • Using a standard text editor (eg Notepad++), open the Makeconf file
  • Locate the line CXX = $(BINPREF)g++ -std=gnu++11 $(M_ARCH)
  • Change the 11 to 17; viz, CXX = $(BINPREF)g++ -std=gnu++17 $(M_ARCH)
  • Save the updated file.

Setting for C++17 in the Makeconfig file

Setting for C++17 in the Makeconfig file

Install the Rcpp Package

Installing Rcpp is no different from any other typical R package. Either use the R command:

install.packages("Rcpp")

Or, use the Tools/Install Packages... selection in RStudio.

Next Steps

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.

8 Comments

  1. supreme clothing July 14, 2020

    My spouse and i got now lucky Edward managed to conclude his reports from the precious recommendations he made from your weblog. It’s not at all simplistic to simply choose to be making a gift of information and facts men and women might have been trying to sell. We realize we’ve got the blog owner to give thanks to for this. All of the illustrations you made, the easy blog menu, the relationships you can assist to promote – it is all excellent, and it’s really assisting our son and us understand that situation is thrilling, and that is rather essential. Thanks for everything!

  2. golden goose sneakers July 17, 2020

    I am only commenting to make you know what a terrific encounter my princess gained checking yuor web blog. She realized such a lot of pieces, including what it’s like to have an ideal helping spirit to let other people clearly have an understanding of specified hard to do things. You actually did more than our own expected results. Many thanks for rendering such warm and friendly, dependable, edifying as well as unique tips about that topic to Janet.

  3. supreme clothing July 21, 2020

    I must show some thanks to you just for rescuing me from this particular setting. Just after searching throughout the the web and seeing opinions which are not powerful, I believed my life was well over. Being alive devoid of the approaches to the problems you have sorted out through the article is a critical case, and ones that would have badly damaged my entire career if I had not encountered the blog. That natural talent and kindness in dealing with almost everything was invaluable. I don’t know what I would’ve done if I had not come upon such a thing like this. I’m able to at this moment look ahead to my future. Thanks a lot very much for your impressive and result oriented guide. I will not think twice to suggest your site to anybody who ought to have recommendations about this situation.

  4. nike shoes July 27, 2020

    I precisely wanted to thank you very much again. I’m not certain what I would have gone through without the type of suggestions shared by you relating to my theme. This was a distressing setting in my circumstances, however , discovering this well-written approach you dealt with that took me to jump over contentment. I am happy for the support and then believe you realize what a great job you’re undertaking instructing the mediocre ones through the use of your web site. I am sure you haven’t met any of us.

  5. birkin bag July 30, 2020

    I would like to get across my appreciation for your generosity supporting persons who must have help with this particular matter. Your personal dedication to passing the solution across was exceptionally useful and has usually made ladies just like me to realize their ambitions. Your entire informative tips and hints indicates much to me and substantially more to my fellow workers. Thank you; from everyone of us.

  6. jordan shoes August 2, 2020

    A lot of thanks for all your valuable labor on this blog. Kim really likes managing investigation and it’s really easy to understand why. Most of us notice all of the compelling mode you render invaluable tactics through the web blog and inspire contribution from people on the point and my child is without a doubt understanding a whole lot. Take pleasure in the rest of the new year. Your carrying out a very good job.

  7. supreme hoodie August 5, 2020

    Thank you so much for giving everyone an extraordinarily breathtaking possiblity to read from this web site. It is always so sweet plus full of amusement for me personally and my office acquaintances to search the blog more than three times a week to find out the new items you have got. And of course, we are actually satisfied concerning the dazzling advice you serve. Some 3 facts in this post are without a doubt the very best I’ve ever had.

  8. jordan 1 off white August 7, 2020

    I must express my affection for your kindness supporting those people that absolutely need help on the situation. Your personal dedication to getting the message up and down has been remarkably informative and have regularly made employees much like me to realize their ambitions. Your entire important publication indicates a great deal to me and especially to my fellow workers. Best wishes; from everyone of us.

Leave a Reply

Your email address will not be published.