We’ve now discussed the improved support in RStudio v1.2 for SQL, D3, and Python. Today, we’ll talk about IDE support for C/C++ and Rcpp.

The IDE has had excellent support for C/C++ since RStudio v0.99, including:

The major new C/C++ feature in RStudio v1.2 is an upgrade to libclang (our underlying completion and diagnostics engine). The update improves performance as well as adds compatibility with modern C++ 17 language features.

Rcpp

RStudio integrates closely with Rcpp, which allows you to easily write performant C++ code and use that code in your R session. For example, the following chunk defines a simple Gibbs sampler:

#include <Rcpp.h>
using namespace Rcpp;

// [[Rcpp::export]]
NumericMatrix gibbs(int N, int thin) {

   NumericMatrix mat(N, 2);
   double x = 0, y = 0;

   for(int i = 0; i < N; i++) {
      for(int j = 0; j < thin; j++) {
         x = R::rgamma(3.0, 1.0 / (y * y + 4));
         y = R::rnorm(1.0 / (x + 1), 1.0 / sqrt(2 * x + 2));
      }
      mat(i, 0) = x;
      mat(i, 1) = y;
   }

   return(mat);
}

Such C++ code can be used both in standalone files (e.g. when used as part of an R package, or when prototyping locally) or within an R Markdown document (within an Rcpp chunk). In each case, we use Rcpp::sourceCpp() to compile and link the code – after this, any exported functions can be called like any other R function in your session.

gibbs(10, 10)
##         [,1]   [,2]
##  [1,] 0.3488 0.9850
##  [2,] 0.9290 0.8519
##  [3,] 2.0505 0.8685
##  [4,] 0.5318 1.2941
##  [5,] 0.6710 0.8434
##  [6,] 0.1064 0.8212
##  [7,] 0.5903 0.7238
##  [8,] 0.6834 0.7078
##  [9,] 0.5379 0.5887
## [10,] 0.1863 0.9741

Thanks to the abstractions provided by Rcpp, the code implementing gibbs() in C++ is nearly identical to the code you’d write in R, but runs 20 times faster.

Code Completion

RStudio provides autocompletion support in C++ source files, and can autocomplete symbols used from R’s C API, Rcpp, and any other libraries you may have imported.

We also now provide autocompletion results for the headers you’d like to use in your program.

Diagnostics

RStudio also provides code diagnostics, alerting you to any issues that might exist in your code.

Updated Libclang

On Windows and macOS, we’ve updated the bundled version of libclang from 3.5.0 to 5.0.2. With this, RStudio gains improved support for modern C++: all standards from C++ 11, C++ 14 and C++ 17 are now supported.

On Linux, we now default to the version of libclang provided by your package manager, so that RStudio can make use of new and improved C++ tooling as it becomes available on your system. (Currently, Ubuntu 18.04 provides libclang 6.0.0)

Try it Out

If you are new to C++ or Rcpp, you might be surprised at how easy it is to get started. There are lots of great resources available, including:

You can download the RStudio 1.2 Preview Release at https://www.rstudio.com/products/rstudio/download/preview/. If you have any questions or comments, please get in touch with us on the community forums.