useR!2014 Rcpp11 tutorial

[This article was first published on R Enthusiast and R/C++ hero, 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.

We are getting close to useR!2014. I hope I’ll see some of you at my tutorial about Rcpp11 in the morning. There are some other pretty useful tutorials as well, so no hard feeling if you don’t come, but if you do want to know about modern R and C++, please join me. Personally I’m happy I’m doing a tutorial because it would have been hard to choose between @MattDowle‘s about data.table, @winston_chang‘s about ggvis and @xieyihui‘s about knitr.

R and C++ fans are lucky this year as you can also choose to learn more conservative R/C++ with @eddelbuettel‘s tutorial about Rcpp in the afternoon. You can even choose to attend both, but my personal recommendations for the afternoon are @hadleywickham‘s about dplyr, @StatGarrett‘s about shiny and @ramnath_vaidya about interactive documents. I wish I was also doing a tutorial in the afternoon so that I would not have to choose.

I’m in the process of preparing the materials. This will be an introductory tutorial so I will focus on simple things, simple enough problems to solve with C++ skills. I’ll walk you through all the way from R solutions to the problems to parallel C++ versions using facilities of C++11. I know, I’ve just said this will be simple and now I’m already talking about parallelism, etc … well that’s the point. This is simple.

To make the most of the tutorial, you’ll need a laptop with a few tools.

  • The most recent version of R: 3.1.0. If you have a least recent version of R, please update it.
  • A C++11 compiler. This is easy enough to get on most platforms.

    • On the mac, assuming you are running Mavericks or higher, you just need to install Xcode from the app store and its command line tools.
    • On Windows, you can install the last version of Rtools. This comes with a version of gcc that is close enough to supporting C++11.
    • On linux, please refer to documentation from your distribution of choice and install at least gcc 4.8.0.
    • On solaris, please talk to me, I’m definitely interested in a discussion including real users of this platform. The native compiler is not even close to support C++11, so your best shot is to install gcc.
  • Rcpp11, which you can install from CRAN

install.packages( "Rcpp11" )  

although I would recommend you to get the development version from github.

devtools::install_github( "Rcpp11/Rcpp11" )  

Rcpp11 has no R code or compiled C++ code, so don’t be surprised if you manage to easily and quickly install it. Installing Rcpp11 is not enough. Please keep reading.

  • attributes is the package we will use to source C++ files. attributes has not been released to CRAN yet, so you need to get it from github.
devtools::install_github( "Rcpp11/attributes")  

Once you have these tools installed, you can test that everything works together by running that :

writeLines( '  
#include <Rcpp11>

// [[export]]
IntegerVector foo(){  
  return {1, 2, 3} ;
', "test.cpp" )  
# [1] 1 2 3

If you get 1, 2, 3 you are ready for the tutorial.

Most of the time during the tutorial, we will be editing .cpp files and compiling them into R with attributes::sourceCpp. I’d recommend you have a text editor that understands C++ but anything will do.

For those of you who live on the console, you might like the scripts from this github repository. I will probably use Rcpp11Script from my console during the tutorial.

To leave a comment for the author, please follow the link and comment on their blog: R Enthusiast and R/C++ hero. offers daily e-mail updates about R news and tutorials about learning R and many other topics. Click here if you're looking to post or find an R/data-science job.
Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.

Never miss an update!
Subscribe to R-bloggers to receive
e-mails with the latest R posts.
(You will not see this message again.)

Click here to close (This popup will not appear again)