<p>Welcome to the seventh post in the rarely relevant R ramblings series, or <span>R4</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 hack I came up with this weekend.</p>
<p>The topic is C++14, i.e. the newest formally approved language standard for C++, and its support in R and on <a href="https://travis-ci.org/">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 href="https://travis-ci.org/">Travis CI</a>. Travis is a tremendously useful and widely-deployed service, most commonly connected to <a href="https://github.com/">GitHub</a> driving &quot;continuous integration&quot; (the 'CI') testing after each commit. But <a href="https://travis-ci.org/">Travis CI</a>, for as useful as it is, is also maddingly conservative 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 href="http://math.bd.psu.edu/faculty/rutter/">Michael</a> who faithfully provides Ubuntu binaries for <a href="https://cran.r-project.org/bin/linux/ubuntu/README.html">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 current R 3.4.0 it will be built on ancient 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 its compile-time settings 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, no mas.</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 not try to enable C++14 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>