Over the past several years the Rcpp package has become an indispensable tool for creating high-performance R code. Its power and ease of use have made C++ a natural second language for many R users. There are over 400 packages on CRAN and Bioconductor that depend on Rcpp and it is now the most downloaded R package.
In RStudio v0.99 we have added extensive additional tools to make working with Rcpp more pleasant, productive, and robust, these include:
Source diagnostics as you edit
Navigable list of compilation errors
Code navigation (go to definition)
We think these features will go a long way to helping even more R users succeed with Rcpp. You can try the new features out now by downloading the RStudio Preview Release.
RStudio v0.99 includes comprehensive code completion for C++ based on Clang (the same underlying engine used by XCode and many other C/C++ tools):
Completions are provided for the C++ language, Rcpp, and any other libraries you have imported.
As you edit C++ source files RStudio uses Clang to scan your code looking for errors, incomplete code, or other conditions worthy of warnings or informational notes. For example:
Diagnostics alert you to the possibility of subtle problems and flag outright incorrect code as early as possible, substantially reducing iteration/debugging time.
Rcpp includes some nifty tools to help make working with C++ code just as simple and straightforward as working with R code. You can “source” C++ code into R just like you’d source an R script (no need to deal with Makefiles or build systems). Here’s a Gibbs Sampler implemented with Rcpp:
We can make this function available to R by simply sourcing the C++ file (much like we’d source an R script):
sourceCpp("gibbs.cpp") gibbs(100, 10)
Thanks to the abstractions provided by Rcpp, the code implementing the Gibbs Sampler in C++ is nearly identical to the code you’d write in R, but runs 20 times faster. RStudio includes full support for Rcpp’s
sourceCpp via the Source button and Ctrl+Shift+Enter keyboard shortcut.
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:
Rcpp website: http://www.rcpp.org/
Rcpp book: http://www.rcpp.org/book/
Tutorial for users new to C++: http://adv-r.had.co.nz/Rcpp.html
Gallery of examples: http://gallery.rcpp.org/
Austin Chia, Founder of Any Instructor, describes his transition from studying biology to becoming a healthcare data analyst.
In this series, we walk through lesser-known tips and tricks to help you work more effectively and efficiently in R Markdown. The final post focuses on making your document look and work better.