<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/7-c14-r-and-travis-a-useful-hack/"></div></div><div></div></div><div></div></div>
<div>
(This article was first published on <strong><a href="http://dirk.eddelbuettel.com/blog/2017/06/13#007_c++14_r_travis"> Thinking inside the box </a></strong>, and kindly contributed to <a rel="nofollow" href="https://www.r-bloggers.com/">R-bloggers)</a> </div>
<p>Welcome to the seventh post in the <em>rarely relevant R ramblings</em> series, or <span><em>R</em><sup>4</sup></span> for short.</p>
<p>We took a short break as several conferences and other events interfered during the month of May, keeping us busy and away from this series. But we are back now with a short and useful <em>hack</em> I came up with this weekend.</p>
<p>The topic is C++14, <em>i.e.</em> the newest formally approved language standard for C++, and its support in R and on <a rel="nofollow" href="https://travis-ci.org/" target="_blank">Travis CI</a>. With release R 3.4.0 of a few weeks ago, R now formally supports C++14. Which is great.</p>
<p>But there be devils. A little known fact is that R hangs on to its configuration settings from its own compile time. That matters in cases such as the one we are looking at here: <a rel="nofollow" href="https://travis-ci.org/" target="_blank">Travis CI</a>. Travis is a <em>tremendously</em> useful and widely-deployed service, most commonly connected to <a rel="nofollow" href="https://github.com/" target="_blank">GitHub</a> driving &quot;continuous integration&quot; (the ‘CI’) testing after each commit. But <a rel="nofollow" href="https://travis-ci.org/" target="_blank">Travis CI</a>, for as useful as it is, is also <em>maddingly conservative</em> still forcing everybody to live and die by [Ubuntu 14.04]http://releases.ubuntu.com/14.04/). So while we all benefit from the fine work by <a rel="nofollow" href="http://math.bd.psu.edu/faculty/rutter/" target="_blank">Michael</a> who faithfully provides Ubuntu binaries for <a rel="nofollow" href="https://cran.r-project.org/bin/linux/ubuntu/README.html" target="_blank">distribution via CRAN</a> (based on the Debian builds provided by yours truly), we are stuck with Ubuntu 14.04. Which means that while Michael can provide us with <em>current</em> R 3.4.0 it will be built on <em>ancient</em> Ubuntu 14.04.</p>
<p>Why does this matter, you ask? Well, if you just try to turn the very C++14 support added to R 3.4.0 on in the binary running on Travis, you get this error:</p>
<pre><code>** libs
Error in .shlib_internal(args) : 
  C++14 standard requested but CXX14 is not defined</code></pre>
<p>And you get it whether or not you define <code>CXX14</code> in the session.</p>
<p>So R (in version 3.4.0) may want to use C++14 (because a package we submitted requests it), but having been built on the dreaded Ubuntu 14.04, it just can’t oblige. Even when we supply a newer compiler. Because R hangs on to <em>its compile-time settings</em> rather than current environment variables. And that means no C++14 as its compile-time compiler was too ancient. Trust me, I tried: adding not only <code>g++-6</code> (from a suitable repo) but also adding <code>C++14</code> as the value for <code>CXX_STD</code>. Alas, <em>no mas</em>.</p>
<p>The trick to overcome this is twofold, and fairly straightforward. First off, we just rely on the fact that <code>g++</code> version 6 defaults to C++14. So by supplying <code>g++-6</code>, we are in the green. We have C++14 by default without requiring extra options. Sweet.</p>
<p>The remainder is to tell R to <em>not try to enable C++14</em> even though we are using it. How? By removing <code>CXX_STD=C++14</code> on the fly and just for Travis. And this can be done easily with a small script <code>configure</code> which conditions on being on Travis by checking two environment variables:</p>
<div>
<pre><code><span>#!/bin/bash</span>

<span>## Travis can let us run R 3.4.0 (from CRAN and the PPAs) but this R version</span>
<span>## does not know about C++14. Even though we can select CXX_STD = C++14, R</span>
<span>## will fail as the version we use there was built in too old an environment,</span>
<span>## namely Ubuntu &quot;trusty&quot; 14.04.</span>
<span>##</span>
<span>## So we install g++-6 from another repo and rely on the fact that is</span>
<span>## defaults to C++14. Sadly, we need R to not fail and hence, just on</span>
<span>## Travis, remove the C++14 instruction</span>

<span>if [[</span> <span>&quot;</span><span>${CI}</span><span>&quot;</span> <span>==</span> <span>&quot;true&quot;</span><span> ]]</span>; <span>then</span>
    <span>if [[</span> <span>&quot;</span><span>${TRAVIS}</span><span>&quot;</span> <span>==</span> <span>&quot;true&quot;</span><span> ]]</span>; <span>then</span> 
        <span>echo</span> <span>&quot;** Overriding src/Makevars and removing C++14 on Travis only&quot;</span>
        <span>sed</span> -i <span>'s|CXX_STD = CXX14||'</span> src/Makevars
    <span>fi</span>
<span>fi</span></code></pre>
</div>
<p>I have deployed this now for two sets of builds in two distinct repositories for two &quot;under-development&quot; packages not yet on CRAN, and it just works. In case you turn on C++14 via <code>SystemRequirements:</code> in the file <code>DESCRIPTION</code>, you need to modify it here.</p>
<p>So to sum up, there it is: C++14 with R 3.4.0 on Travis. Only takes a quick Travis-only modification.</p>
	<p>
<div>
<div>To <strong>leave a comment</strong> for the author, please follow the link and comment on their blog: <strong><a href="http://dirk.eddelbuettel.com/blog/2017/06/13#007_c++14_r_travis"> Thinking inside the box </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/9G-idot1ICQ" height="1"></a></div>