{"id":4398,"date":"2019-01-13T17:18:02","date_gmt":"2019-01-13T15:18:02","guid":{"rendered":"https:\/\/engel-wolf.com\/?p=4398"},"modified":"2019-10-27T17:23:09","modified_gmt":"2019-10-27T15:23:09","slug":"travis-ci-for-r%e2%80%8a-%e2%80%8aadvanced-guide","status":"publish","type":"post","link":"https:\/\/engel-wolf.com\/?p=4398","title":{"rendered":"Travis CI for R\u200a\u2014\u200aAdvanced guide"},"content":{"rendered":"\n<p> Travis CI is a common tool to build R packages. It is in my opinion the best platform to use R in continuous integration. Some of the most downloaded R packages built at this platform. These are for example\u00a0<em>testthat<\/em>,\u00a0<em>magick\u00a0<\/em>or\u00a0<em>covr<\/em>. I also built my package\u00a0<a rel=\"noreferrer noopener\" href=\"https:\/\/medium.com\/@zappingseb\/rtest-pretty-testing-of-r-packages-50f50b135650\" target=\"_blank\"><em>RTest\u00a0<\/em><\/a>at this platform. During the setup I ran into some trouble. The knowledge I gained I\u2019m sharing with you in this guide. <\/p>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"16c2\">Table of&nbsp;contents<\/h3>\n\n\n\n<ul><li><a href=\"#a056\">Basics from \u201cBuilding an R project\u201d<\/a><\/li><li><a href=\"#031a\">Modifying R CMD build<\/a><\/li><li><a href=\"#9571\">Multiple operating systems<\/a><\/li><li><a href=\"#45c4\">Run scripts with user interfaces<\/a><\/li><li><a href=\"#5da6\">Code coverage<\/a><\/li><li><a href=\"#22c9\">Build and deploy a pkgdown page to github pages<\/a><\/li><li><a href=\"#7ef5\">ImageMagick and Travis CI<\/a><\/li><li><a href=\"#b418\">Further reading<\/a><\/li><\/ul>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"a056\">Basics from \u201cBuilding an R&nbsp;project\u201d<\/h3>\n\n\n\n<p>The article \u201c<a rel=\"noreferrer noopener\" href=\"https:\/\/docs.travis-ci.com\/user\/languages\/r\/\" target=\"_blank\">Building an R Project<\/a>\u201d from Travis CI tells you about the basics. It allows setting up a build for an R-package or R project. The main take away comes with this\u00a0.travis.yml file.<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: plain; title: ; notranslate\" title=\"\">\n# Use R language\nlanguage: r\n#Define multiple R-versions, one from bioconductor\nr:\n  - oldrel\n  - release\n  - devel\n  - bioc-devel\n# Set one of you dependencies from github\nr_github_packages: r-lib\/testthat\n# Set one of your dependencies from CRAN\nr_packages: RTest\n# set a Linux system dependency\napt_packages:\n  - libxml2-dev\n<\/pre><\/div>\n\n\n<p>The tutorial explains to you that you should setup your type language as&nbsp;<strong>R.&nbsp;<\/strong>You can use different&nbsp;<strong>R-versions.<\/strong>&nbsp;Those R-Versions are:<\/p>\n\n\n\n<p>[oldrel, release, devel, bioc-devel, bioc-release]<\/p>\n\n\n\n<p>Additionally you can load any package from github by&nbsp;<code>r_github_packages<\/code>&nbsp;. Or you can get any package from&nbsp;<a href=\"https:\/\/cran.r-project.org\/\" rel=\"noreferrer noopener\" target=\"_blank\">CRAN&nbsp;<\/a>by&nbsp;<code>r_packages<\/code>&nbsp;. A list of multiple packages can be created using the standard yml format:<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">r_packages:<br>  - RTest<br>  - testthat<\/pre>\n\n\n\n<p>In case you have a Linux dependency, it needs to be mentioned. The RTest package uses XML test cases. The XML Linux library needed is&nbsp;<code>libxml2<\/code>&nbsp;. It can be added by:<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: plain; title: ; notranslate\" title=\"\">\napt_packages:  - libxml2-dev\n<\/pre><\/div>\n\n\n<p>You are done with the basics. In case you have this&nbsp;.travis.yml file inside your repository, it will use&nbsp;<code>R CMD build<\/code>&nbsp;and&nbsp;<code>R CMD check<\/code>&nbsp;to check your project.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"031a\">Modifying R CMD&nbsp;commands<\/h3>\n\n\n\n<p>To build my project I wanted to build it like on CRAN. Therefore I needed to change the script of the package check. Therefore I added:<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">script:<br>  - R CMD build . --compact-vignettes=gs+qpdf<br>  - R CMD check *tar.gz --as-cran<\/pre>\n\n\n\n<p>Inside this script you can change<code>R CMD build<\/code>&nbsp;or<code>R CMD check<\/code>&nbsp;arguments. For a list of arguments to&nbsp;<code>R CMD<\/code>&nbsp;see this&nbsp;<a href=\"https:\/\/support.rstudio.com\/hc\/en-us\/articles\/200486518-Customizing-Package-Build-Options\" rel=\"noreferrer noopener\" target=\"_blank\">tutorial from RStudio<\/a>.<\/p>\n\n\n\n<p>To run vignette compression get&nbsp;<code>gs+qpdf<\/code>&nbsp;by:<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">addons:<br>  apt:<br>    update: true<br>    packages:<br>      - libgs-dev<br>      - qpdf<br>      - ghostscript<\/pre>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"9571\">Multiple operating systems<\/h3>\n\n\n\n<p>Travis CI offers two different operating systems right now (Jan 2019). Those are macOS and Linux. The standard way of testing is Linux. For my project\u00a0<a href=\"https:\/\/engel-wolf.com\/?p=4394\" target=\"_blank\" rel=\"noreferrer noopener\" aria-label=\" (\u00f6ffnet in neuem Tab)\">RTest\u00a0<\/a>I needed to test in macOS, too. To test in two operating systems use the\u00a0<code>matrix<\/code>\u00a0parameter of Travis CI.<\/p>\n\n\n\n<p>The&nbsp;<code>matrix<\/code>&nbsp;parameter allows adjusting certain parameters for certain builds. To have the exact same build in Linux and macOS I used the following structure:<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">matrix:<br>  include:<br>  - r: release<br>    script:<br>      - R CMD build . --compact-vignettes=gs+qpdf<br>      - R CMD check *tar.gz --as-cran <br>  - r: release<br>    os: osx<br>    osx_image: xcode7.3<br>    before_install:<br>      - sudo tlmgr install framed titling<br>    script:<br>      - R CMD build . --compact-vignettes=gs+qpdf<br>      - R CMD check *tar.gz --as-cran<\/pre>\n\n\n\n<p>The&nbsp;<code>matrix<\/code>&nbsp;function splits the build into different operating systems. For macOS I used the image xcode7.3 as it is proposed by&nbsp;<a href=\"https:\/\/ropensci.org\/blog\/2016\/07\/12\/travis-osx\/\" rel=\"noreferrer noopener\" target=\"_blank\">rOpenSCI<\/a>. An extra point for this version is that it is close to the current CRAN macOS version. As you can see you should install the Latex packages&nbsp;<code>framed<\/code>&nbsp;and&nbsp;<code>titling<\/code>&nbsp;to create vignettes.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"45c4\">Run scripts with User interfaces<\/h3>\n\n\n\n<p>My package\u00a0<a href=\"https:\/\/engel-wolf.com\/?p=4394\" target=\"_blank\" rel=\"noreferrer noopener\" aria-label=\" (\u00f6ffnet in neuem Tab)\">RTest<\/a>\u00a0uses\u00a0<a rel=\"noreferrer noopener\" href=\"https:\/\/www.rdocumentation.org\/packages\/tcltk\/versions\/3.5.2\" target=\"_blank\">Tcl\/Tk<\/a>\u00a0user interfaces. To test such user interfaces you need enable user interfaces in Linux and macOS separately. Travis CI provides the\u00a0<code>xvfb<\/code>\u00a0package for Linux. For macOS you need to reinstall\u00a0<code>xquartz<\/code>\u00a0and\u00a0<code>tcl-tk<\/code>\u00a0with\u00a0<code>homebrew<\/code>\u00a0.<\/p>\n\n\n\n<h4 class=\"wp-block-heading\" id=\"a040\">User interfaces for&nbsp;Linux<\/h4>\n\n\n\n<p>To enable user interfaces in Linux install&nbsp;<code>xvfb<\/code>&nbsp;.<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">addons:<br>  apt:<br>    update: true<br>    packages:<br>       -   x11proto-xf86vidmode-dev<br>       -   xvfb<br>       -   libxxf86vm-dev<\/pre>\n\n\n\n<p>You can run all R scripts with a user interface using the&nbsp;<code>xvfb-run<\/code>&nbsp;command in front of the&nbsp;<code>R<\/code>&nbsp;command.<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">script:<br>      - R CMD build . --compact-vignettes=gs+qpdf<br>      - xvfb-run R CMD check *tar.gz --as-cran<\/pre>\n\n\n\n<h4 class=\"wp-block-heading\" id=\"e920\">User interfaces for&nbsp;macOS<\/h4>\n\n\n\n<p>For macOS the installation of a user interface is more difficult. You need to add&nbsp;<code>xquart<\/code>&nbsp;and&nbsp;<code>tcl-tk<\/code>&nbsp;to the image provided in&nbsp;<code>xcode7.3<\/code>&nbsp;.<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">before_install:<br>      - brew update<br>      - brew cask reinstall xquartz<br>      - brew install tcl-tk --with-tk<br>      - brew link --overwrite --force tcl-tk; brew unlink tcl-tk<\/pre>\n\n\n\n<p>To use xquartz there is no&nbsp;<code>xvfb-run<\/code>&nbsp;command under macOS. In a<a href=\"https:\/\/github.com\/travis-ci\/travis-ci\/issues\/7313\" rel=\"noreferrer noopener\" target=\"_blank\">&nbsp;github issue<\/a>&nbsp;I found a solution that still makes user interfaces work with&nbsp;<code>xquartz<\/code>&nbsp;.<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">before_script:<br>      - \"export DISPLAY=:99.0\"<br>      - if [ \"${TRAVIS_OS_NAME}\" = \"osx\" ]; then ( sudo Xvfb :99 -ac -screen 0 1024x768x8; echo ok ) &amp; fi<\/pre>\n\n\n\n<p>You create a display before running any R script that can be used by the R session. It is important to export the&nbsp;<code>DISPLAY<\/code>&nbsp;variable. This variable is read by the tcktk R package.<\/p>\n\n\n\n<p>In macOS you do not need to change the script<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">script:<br>      - R CMD build . --compact-vignettes=gs+qpdf<br>      - R CMD check *tar.gz --as-cran<\/pre>\n\n\n\n<h4 class=\"wp-block-heading\" id=\"58e2\">Addon<\/h4>\n\n\n\n<p>For more information on user interfaces you can read these two github issues:<\/p>\n\n\n\n<ul><li><a href=\"https:\/\/www.google.com\/search?q=rselenium+shiny+trvais&amp;rlz=1C1GCEA_enDE800DE801&amp;oq=rselenium+shiny+trvais&amp;aqs=chrome..69i57.2913j0j4&amp;sourceid=chrome&amp;ie=UTF-8\" rel=\"noreferrer noopener\" target=\"_blank\">RSelenium with travis and shiny<\/a><\/li><li><a href=\"https:\/\/github.com\/ropensci\/RSelenium\/issues\/145\" rel=\"noreferrer noopener\" target=\"_blank\">RSelenium with sauce<\/a><\/li><\/ul>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"5da6\">Code coverage<\/h3>\n\n\n\n<p>For code coverage I would suggest to use one specific version of your builds. I decided for Linux + r-release to test the code coverage. First of all I added the&nbsp;<em>covr<\/em>&nbsp;package to my build script:<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">r_github_packages:<br>  - r-lib\/covr<\/pre>\n\n\n\n<p>Secondly I wanted to test my package using&nbsp;<em>covr<\/em>. This can be done in Travis using the&nbsp;<code>after_success<\/code>&nbsp;step. To use&nbsp;<em>covr&nbsp;<\/em>inside this step you need to define how your package tarball will be named. You can write this directly into your script. A better way to do it is to write it into the&nbsp;<code>env<\/code>&nbsp;part of you&nbsp;.travis.yml file. The name of your tarball will always be&nbsp;<em>PackageName + \u201c_\u201d + PackageVersion + \u201c.tar.gz\u201d.&nbsp;<\/em>Inside your DESCRIPTION file you defined&nbsp;<em>PackageName<\/em>&nbsp;and&nbsp;<em>PackageVersion.&nbsp;<\/em>I used CODECOV to store the results of my coverage tests.<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">env:<br>    - PKG_TARBALL=RTest_1.2.3.1000.tar.gz<\/pre>\n\n\n\n<pre class=\"wp-block-preformatted\">after_success:<br>      - tar -C .. -xf $PKG_TARBALL<br>      - xvfb-run Rscript -e 'covr::codecov(type=c(\"tests\", \"vignettes\", \"examples\"))'<\/pre>\n\n\n\n<p>The setup I\u2019m using for my package includes code coverage for all my examples, vignettes and tests. To deploy the results of the code coverage you must define the global variable&nbsp;<code>CODECOV_TOKEN<\/code>&nbsp;. The token can be found under&nbsp;<code>https:\/\/codecov.io\/gh\/&lt;owner&gt;\/&lt;repo&gt;\/settings<\/code>&nbsp;. You can insert it secretly into your Travis CI build. Add tokens inside&nbsp;<code>https:\/\/travis-ci.org\/&lt;owner&gt;\/&lt;repo&gt;\/settings<\/code>&nbsp;. The section environment variables stores variables secretly for you.<\/p>\n\n\n\n<p>To use COVERALLS instead of CODECOV use the&nbsp;<code>covr::coveralls<\/code>&nbsp;function and define a&nbsp;<code>COVERALLS_TOKEN<\/code>&nbsp;inside your environment.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"22c9\">Build and deploy a pkgdown page to github&nbsp;pages<\/h3>\n\n\n\n<p>Building a&nbsp;<em>pkgdown&nbsp;<\/em>page can be really useful to document your code. On my github repository I also host the&nbsp;<em>pkgdown&nbsp;<\/em>page of my package RTest. You can find the page here:&nbsp;<a href=\"https:\/\/zappingseb.github.io\/RTest\/index.html\" rel=\"noreferrer noopener\" target=\"_blank\">https:\/\/zappingseb.github.io\/RTest\/index.html<\/a><\/p>\n\n\n\n<p>To allow deployment to github pages I activated this feature at: https:\/\/github.com\/&lt;owner&gt;\/&lt;repo&gt;\/settings. You have to use the&nbsp;<em>gh-pages<\/em>&nbsp;branch. If you do not have such a&nbsp;<a href=\"https:\/\/help.github.com\/articles\/creating-and-deleting-branches-within-your-repository\/\" rel=\"noreferrer noopener\" target=\"_blank\">branch you need to create it<\/a>.<\/p>\n\n\n\n<p>Inside the&nbsp;.travis.yml you start by installing&nbsp;<em>pkgdown.<\/em><\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">r_github_packages:<br>  - r-lib\/pkgdown<\/pre>\n\n\n\n<p>You will have to build the page from your package tarball. The name of the package tarball has to be defined. Please see the section&nbsp;<a href=\"#5da6\">code coverage<\/a>&nbsp;for how this is done. After unpacking the tarball you should delete any leftovers from checking the package by&nbsp;<code>rm -rf &lt;PackageName&gt;.Rcheck<\/code>&nbsp;.<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">after_success:<br>      - tar -C .. -xf $PKG_TARBALL<br>      - rm -rf RTest.Rcheck<br>      - Rscript -e 'pkgdown::build_site()'<\/pre>\n\n\n\n<p>The&nbsp;<code>Rscript<\/code>&nbsp;will produce the website inside a<code>docs<\/code>&nbsp;folder. This folder must be deployed on github pages.<\/p>\n\n\n\n<p>First go to to&nbsp;<a href=\"https:\/\/github.com\/settings\/tokens\" rel=\"noreferrer noopener\" target=\"_blank\">https:\/\/github.com\/settings\/tokens<\/a>&nbsp;when you\u2019re logged into github. There you have to create a token with&nbsp;<code>public_repo<\/code>&nbsp;or&nbsp;<code>repo<\/code>&nbsp;scope. Now store this token inside your Travis CI build. Therefore go to&nbsp;<code>https:\/\/travis-ci.org\/&lt;owner&gt;\/&lt;repo&gt;\/settings<\/code>&nbsp;and store it as a global variable named&nbsp;<code>GITHUB_TOKEN<\/code>&nbsp;. The website will now be deployed on every successful build using this script:<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">deploy:<br>  - provider: pages<br>        skip-cleanup: true<br>        github-token: $GITHUB_TOKEN<br>        keep-history: false<br>        local-dir: docs<br>        on:<br>          branch: master<\/pre>\n\n\n\n<p>for more info on deploying pages you can check the&nbsp;<a href=\"https:\/\/docs.travis-ci.com\/user\/deployment\/pages\/\" rel=\"noreferrer noopener\" target=\"_blank\">Travis CI guide on pages<\/a>.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"7ef5\">ImageMagick and Travis&nbsp;CI<\/h3>\n\n\n\n<p>Inside the&nbsp;<a href=\"https:\/\/travis-ci.community\/t\/error-configuration-failed-for-package-magick\/1674\" rel=\"noreferrer noopener\" target=\"_blank\">travis-ci-community<\/a>&nbsp;there was a question on how to install the&nbsp;<code>magick<\/code>&nbsp;package on Travis-CI. The answer is simple. You need to have all system dependencies of ImageMagick. Install these for Linux by:<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">addons:<br>   apt:<br>     update: true<br>     sources:<br>       - sourceline: 'ppa:opencpu\/imagemagick'<br>       - sourceline: 'ppa:ubuntugis\/ppa'<br>     packages:<br>       - libmagick++-dev<br>       - librsvg2-dev<br>       - libwebp-dev<br>       - libpoppler-cpp-dev<br>       - libtesseract-dev<br>       - libleptonica-dev<br>       - tesseract-ocr-eng<br>       - r-cran-rgdal<br>       - libfftw3-dev<br>       - cargo<\/pre>\n\n\n\n<p>This also worked for macOS for me.<\/p>\n\n\n\n<hr class=\"wp-block-separator\"\/>\n\n\n\n<p><em>Dear Reader: It\u2019s always a pleasure to write about my work on continuous integrations. I thank you for reading until the end of this article. If you liked the article, you can star the repository on\u00a0<\/em><a rel=\"noreferrer noopener\" href=\"https:\/\/github.com\/zappingseb\/RTest\" target=\"_blank\"><strong><em>github<\/em><\/strong><\/a><em>. In case of any comment, leave it\u00a0my\u00a0<\/em><strong><em>LinkedIn\u00a0<\/em><\/strong><em>profile\u00a0<\/em><a rel=\"noreferrer noopener\" href=\"http:\/\/linkedin.com\/in\/zappingseb.\" target=\"_blank\"><em>http:\/\/linkedin.com\/in\/zappingseb.<\/em><\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Travis CI is a common tool to build R packages. It is in my opinion the best platform to use R in continuous integration. Some of the most downloaded R [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":4399,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"ngg_post_thumbnail":0,"footnotes":""},"categories":[1],"tags":[417,384,381,395,414,416],"_links":{"self":[{"href":"https:\/\/engel-wolf.com\/index.php?rest_route=\/wp\/v2\/posts\/4398"}],"collection":[{"href":"https:\/\/engel-wolf.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/engel-wolf.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/engel-wolf.com\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/engel-wolf.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=4398"}],"version-history":[{"count":1,"href":"https:\/\/engel-wolf.com\/index.php?rest_route=\/wp\/v2\/posts\/4398\/revisions"}],"predecessor-version":[{"id":4400,"href":"https:\/\/engel-wolf.com\/index.php?rest_route=\/wp\/v2\/posts\/4398\/revisions\/4400"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/engel-wolf.com\/index.php?rest_route=\/wp\/v2\/media\/4399"}],"wp:attachment":[{"href":"https:\/\/engel-wolf.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=4398"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/engel-wolf.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=4398"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/engel-wolf.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=4398"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}