Oct 11 2018
3 min

RStudio 1.2 Preview: C/C++ and Rcpp

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:

• Tight integration with the Rcpp package
• Code completion
• Source diagnostics as you edit
• Code snippets
• Auto-indentation
• Navigable list of compilation errors
• Code navigation (go to definition)

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:
{"extraction_info": {"found_math": true, "script_math_tex": 0, "script_math_asciimath": 0, "math_annotations": 0, "math_alttext": 0, "mathml": 0, "mathjax_tag": 0, "mathjax_inline_tex": 0, "mathjax_display_tex": 0, "mathjax_asciimath": 1, "img_math": 0, "codecogs_latex": 0, "wp_latex": 0, "mimetex.cgi": 0, "/images/math/codecogs": 0, "mathtex.cgi": 0, "katex": 0, "math-container": 0, "wp-katex-eq": 0, "align": 0, "equation": 0, "x-ck12": 0, "texerror": 0, "math_score": 0.1980670541524887, "perplexity": 7597.018560631705}, "config": {"markdown_headings": false, "markdown_code": true, "boilerplate_config": {"ratio_threshold": 0.18, "absolute_threshold": 10, "end_threshold": 15, "enable": true}, "remove_buttons": true, "remove_image_figures": true, "remove_link_clusters": true, "table_config": {"min_rows": 2, "min_cols": 3, "format": "plain"}, "remove_chinese": true, "remove_edit_buttons": true, "extract_latex": true}, "warc_path": "s3://commoncrawl/crawl-data/CC-MAIN-2022-40/segments/1664030331677.90/warc/CC-MAIN-20220924151538-20220924181538-00575.warc.gz"}