<p><div><div><div><div data-show-faces="false" data-height="21" data-width="100" data-layout="button_count" data-send="true" data-href="https://www.r-bloggers.com/rstudio-1-2-preview-c-c-and-rcpp/"></div></div><div></div></div><div></div></div>
<div>
(This article was first published on <strong><a href="https://blog.rstudio.com/2018/10/11/rstudio-1-2-preview-cpp/"> RStudio Blog</a></strong>, and kindly contributed to <a rel="nofollow" href="https://www.r-bloggers.com/">R-bloggers)</a> </div>
<p>We’ve now discussed the improved support in RStudio v1.2 for <a rel="nofollow" href="https://blog.rstudio.com/2018/10/02/rstudio-1-2-preview-sql/" target="_blank">SQL</a>, <a rel="nofollow" href="https://blog.rstudio.com/2018/10/05/r2d3-r-interface-to-d3-visualizations/" target="_blank">D3</a>, and <a rel="nofollow" href="https://blog.rstudio.com/2018/10/09/rstudio-1-2-preview-reticulated-python/" target="_blank">Python</a>. Today, we’ll talk about IDE support for C/C++ and <a rel="nofollow" href="http://www.rcpp.org/" target="_blank">Rcpp</a>.</p>
<p>The IDE has had excellent support for C/C++ since RStudio v0.99, including:</p>
<ul>
<li>Tight integration with the <a rel="nofollow" href="http://www.rcpp.org/" target="_blank">Rcpp</a> package</li>
<li>Code completion</li>
<li>Source diagnostics as you edit</li>
<li>Code snippets</li>
<li>Auto-indentation</li>
<li>Navigable list of compilation errors</li>
<li>Code navigation (go to definition)</li>
</ul>
<p>The major new C/C++ feature in RStudio v1.2 is an upgrade to <a rel="nofollow" href="https://clang.llvm.org/docs/Tooling.html" target="_blank">libclang</a> (our underlying completion and diagnostics engine). The update improves performance as well as adds compatibility with modern <a rel="nofollow" href="https://en.wikipedia.org/wiki/C%2B%2B17" target="_blank">C++ 17</a> language features.</p>
<div>
<h2>Rcpp</h2>
<p>RStudio integrates closely with <a rel="nofollow" href="http://www.rcpp.org/" target="_blank">Rcpp</a>, 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:</p>
<pre><code>#include &lt;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 &lt; N; i++) {
      for(int j = 0; j &lt; 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);
}</code></pre>
<p>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 <code>Rcpp</code> chunk). In each case, we use <code>Rcpp::sourceCpp()</code> to compile and link the code – after this, any exported functions can be called like any other R function in your session.</p>
<pre><code>gibbs(10, 10)</code></pre>
<pre><code>##         [,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</code></pre>
<p>Thanks to the abstractions provided by Rcpp, the code implementing <code>gibbs()</code> in C++ is nearly identical to the code you’d write in R, but runs <a rel="nofollow" href="http://dirk.eddelbuettel.com/blog/2011/07/14/" target="_blank">20 times faster</a>.</p>
</div>
<div>
<h2>Code Completion</h2>
<p>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.</p>
<p><img src="https://i0.wp.com/blog.rstudio.com/images/2018-10-11-rstudio-preview-cpp-autocomplete.png?w=456&ssl=1" data-recalc-dims="1"></p>
<p>We also now provide autocompletion results for the headers you’d like to use in your program.</p>
<p><img src="https://i2.wp.com/blog.rstudio.com/images/2018-10-11-rstudio-preview-cpp-autocomplete-2.png?w=456&ssl=1" data-recalc-dims="1"></p>
</div>
<div>
<h2>Diagnostics</h2>
<p>RStudio also provides code diagnostics, alerting you to any issues that might exist in your code.</p>
<p><img src="https://i2.wp.com/blog.rstudio.com/images/2018-10-11-rstudio-preview-cpp-diagnostics.png?w=456&ssl=1" data-recalc-dims="1"></p>
</div>
<div>
<h2>Updated Libclang</h2>
<p>On Windows and macOS, we’ve updated the bundled version of <code>libclang</code> 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.</p>
<p>On Linux, we now default to the version of <code>libclang</code> 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 <code>libclang</code> 6.0.0)</p>
</div>
<div>
<h2>Try it Out</h2>
<p>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:</p>
<ul>
<li>
<p>Rcpp website: <a rel="nofollow" href="http://www.rcpp.org/" target="_blank">http://www.rcpp.org/</a></p>
</li>
<li>
<p>Rcpp book: <a rel="nofollow" href="http://www.rcpp.org/book/" target="_blank">http://www.rcpp.org/book/</a></p>
</li>
<li>
<p>Tutorial for users new to C++: <a rel="nofollow" href="http://adv-r.had.co.nz/Rcpp.html" target="_blank">http://adv-r.had.co.nz/Rcpp.html</a></p>
</li>
<li>
<p>Gallery of examples: <a rel="nofollow" href="http://gallery.rcpp.org/" target="_blank">http://gallery.rcpp.org/</a></p>
</li>
</ul>
<p>You can download the RStudio 1.2 Preview Release at <a rel="nofollow" href="https://www.rstudio.com/products/rstudio/download/preview/" target="_blank">https://www.rstudio.com/products/rstudio/download/preview/</a>. If you have any questions or comments, please get in touch with us on the <a rel="nofollow" href="https://community.rstudio.com/c/rstudio-ide" target="_blank">community forums</a>.</p>
</div>
	<p>
<div>
<div>To <strong>leave a comment</strong> for the author, please follow the link and comment on their blog: <strong><a href="https://blog.rstudio.com/2018/10/11/rstudio-1-2-preview-cpp/"> RStudio Blog</a></strong>.</div>
<hr>
<a rel="nofollow" href="https://www.r-bloggers.com/">R-bloggers.com</a> offers <strong><a rel="nofollow" href="https://feedburner.google.com/fb/a/mailverify?uri=RBloggers">daily e-mail updates</a></strong> about <a rel="nofollow" href="https://www.r-project.org/" title="The R Project for Statistical Computing">R</a> news and <a rel="nofollow" href="https://www.r-bloggers.com/search/tutorial" title="R tutorials">tutorials</a> on topics such as: <a rel="nofollow" href="https://www.r-bloggers.com/search/data%20science" title="Data science">Data science</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/Big%20Data" title="Big Data">Big Data, <a rel="nofollow" href="https://www.r-users.com/" title="R jobs">R jobs</a>, visualization (<a rel="nofollow" href="https://www.r-bloggers.com/search/ggplot2" title="ggplot and ggplot2 tutorials">ggplot2</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/boxplot" title="Boxplots using lattice and ggplot2 tutorials">Boxplots</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/map" title="Maps and gis">maps</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/animation" title="Animation in R">animation</a>), programming (<a rel="nofollow" href="https://www.r-bloggers.com/search/RStudio" title="RStudio IDE for R">RStudio</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/sweave" title="Sweave and literate programming">Sweave</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/LaTeX" title="LaTeX in R">LaTeX</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/SQL" title="SQL and databases">SQL</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/eclipse" title="Eclipse IDE for R">Eclipse</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/git" title="git and github, Version Control System">git</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/hadoop" title="Large data in R using Hadoop">hadoop</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/Web+Scraping" title="Web Scraping of google, facebook, yahoo, twitter and more using R">Web Scraping</a>) statistics (<a rel="nofollow" href="https://www.r-bloggers.com/search/regression" title="Regressions and ANOVA analysis tutorials">regression</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/PCA" title="principal component analysis tutorial">PCA</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/time+series" title="Time series">time series</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/trading" title="finance trading">trading</a>) and more...
<img width="1" alt="" src="http://feeds.feedburner.com/~r/RBloggers/~4/O0X3ixznXY4" height="1"></a></div>