Thoughts or tips on learning C++ for Rcpp


Hi wonderful rstudio community!

I’m considering learning more about C++ as a means to better understand Rcpp and its subsequent use in R. I’d like to know what happens ‘under the hood’ in packages like dplyr and see a lot of benefit to knowing C++ to tackle the computationally heavy things that R doesn’t need to do, e.g., RcppEigen.

Any thoughts from those who have gone down this road already? Tips? Resources?

I will likely rely heavily on this:

It was really helpful when I was learning Python (wish there was an equivalent in R)

Initially asked here:

Edit: :point_down:t2:

I also received this as a resource for learning for C++

I also found and may use this as a reference as well


For Rcpp I would (besides the Rcpp homepage and it`s gallery) strongly recommend the Rcpp chapter in Advanced R (and also solve the exercises), which focuses especially on c++ for R usecases.

On our last meetup I also saw a great Rcpp introduction (unfortunately the sound is not perfect), where Marvin also mentions a lot of resources in the end (you can also find them here).

Edit: Copy pasted the resources:


I recently posted a similar question to the rOpensci discuss forum and got some good tips from @noamross :


Thank you for copy/pasting the resources! I’m going to spend some time reading through–it’s also becoming exceedingly apparent that I spend some quality time with Advanced R. I think some exercises on R specific use cases would be a great start!

edit: OH, Rcpp quick reference using pinp; looking nice :ok_hand:t2:


Oh perfect! Yeah, 14 days apart! Looks like I’ll start with Advanced R for now. If you’re on the rOpenSci Slack maybe we can egg each other on to stick with it?


Great idea! Yes, I am on the rOpenSci slack. :grinning:


Here is my biased opinion!

Use the C++ library Armadillo with the R package RcppArmadillo.

Don’t try putting everything into C++, just look for the bottlenecks (try profvis). Do as much as you can in R.

If you have for loops in R that

  • do things that can’t be parallelized and
  • do things that are computationally fast individually (there’s just a lot of loops!),

then try doing those things in C++. In other words, write the for loop in C++ and call it from R.


Thanks @ace :+1:t2:

I didn’t know about profvis, I’m going to use that a lot more now that I know about it! Thanks for the very practical advice.


Rcpp for everyone is a concise introduction and reference of Rcpp for those who use R but do not have much experience with C++ itself.


great @nakamichi :+1:t2:

Thanks; really nice that it’s in bookdown as well


Now that recent C++ standards support more a more functional style, an intro to functional C++ from an Rcpp point of view would be very nice.


That’s excellent advice–as an aside, functional C++ sounds like a really great band name :metal:t2: