First Steps are Easy!

Rcpp makes it easy to create a first example. Just use cppFunction("...") and supply a valid C++ function in the argument string.

Here is a simple example from the introductory vignette:


library("Rcpp")
cppFunction("bool isOddCpp(int num = 10) {
   bool result = (num % 2 == 1);
   return result;
}")
isOddCpp(42L)
  

Of course, that function could be written in R (and we show an R variant in that vignette) but this provides a nice and simple stepping stone.

Recursive Functions Are Easy Too!

Rcpp makes it equally easy to source code from a file via sourcCpp("...") and supply a path to a file as the argument.

The well-known Fibonacci sequence can set-up this way:


#include "Rcpp.h"

// [[Rcpp::export]]
int fibonacci(const int x) {
   if (x < 2) return(x);
   return (fibonacci(x - 1)) + fibonacci(x - 2);
}

/*** R
system.time(print(fibonacci(30)))
*/
  

This shows another nice trick: We can include an R example use in our C++ file by placing it behind a marker of /*** R. Try it! On my computer, this call is still so fast that system.time() can barely measure it!.

Rcpp Packages

Rcpp is used by over 2500 packages on CRAN alone. Below is a small selection of some of the key packages.

The Gallery Has Numerous Examples!

The related website gallery.rcpp.org contains over one hundred distinct examples, ranging from simple first steps on how to create certain data types for features to more complex applications examples. All examples are fully tested and should be runnable.

Best of all, it is open website to which you could contribute the next article! See gallery.rcpp.org for more.

Ten PDF Vignettes

The Rcpp package comes with ten pdf vignettes documenting both first steps and more advanced use cases.

The rcpp-devel mailing can help with additional user- or developer questions.

The Rcpp Book

The book, published in the Springer useR! series, is available from the publisher as both a soft-cover version (ISBN 978-1-4614-6867-7) and an e-book (ISBN 978-1-4614-6868-4). Institutions with a Springer Link subscription can also access it. Springer provides electronic previews.

The book is also available from Amazon and other on-line booksellers.