One of the recent big shifts in R (at least for my purposes) has been RStudio’s release of the reticulate package, which provides a clean interface to Python’s base language and modules. Not only can you use this in your projects, you can also use it in other packages, including those that go on CRAN!
The challenge comes with integrating your package with Travis CI, a common continuous testing tool. Many R developers are familiar with how to do this for an R project, but reticulate
brings potentially alien Python dependencies, too. So I wrote a little tutorial on how to write a travis setup for a package featuring both. As an example we’ll be using my current projects, mwparser, which parses wikimarkup.
Writing a basic .travis.yml
Let’s start with a really basic .travis.yml
, the file which contains instructions for Travis on how to format the testing environment. A simple R one looks like:
language: r
warnings_are_errors: false
sudo: required
env:
global:
- CRAN: http://cran.rstudio.com
r_packages:
- knitr
notifications:
email:
on_failure: change
This tells Travis:
- You’re testing something in R
- It shouldn’t error out on warnings, only errors
- It needs to run as
root
http://cran.rstudio.com
should be the CRAN mirror for any packages neededknitr
is said needed R package- If the testing fails, or succeeds having failed last time, email the account holder.
We’ll also want to add a new line to .Rbuildignore
, just containing .travis.yml
, so that package builds don’t erroneously include the file and confuse CRAN.
Adding reticulate
We’ll next want to specify that mwparser
requires the R package reticulate
(and any other packages it uses - in this case, testthat for the actual tests). So now the .travis.yml
looks like:
language: r
warnings_are_errors: false
sudo: required
env:
global:
- CRAN: http://cran.rstudio.com
r_packages:
- testthat
- reticulate
notifications:
email:
on_failure: change
Specifying Python dependencies
And the meat of this post: specifying Python dependencies! For this we’ll need to add a before_install
section, and fill it with commands for pip
as to which python libraries to install before setting everything up and running all the tests.
All reticulate packages depend on the Python numpy
library; additionally, mwparser
requires Ben Kurtovik’s wonderful mwparserfromhell. So the final version of our .travis.yml
file looks like:
language: r
warnings_are_errors: false
sudo: required
dist: trusty
cache: packages
before_install:
- pip install numpy mwparserfromhell
env:
global:
- CRAN: http://cran.rstudio.com
r_packages:
- testthat
- reticulate
notifications:
email:
on_failure: change
And that’s it! We’re done and ready to test our nifty R/Python package. Go through the standard R/Travis guides if there’s anything else you need to finish up, and feel free to drop me a note on twitter should you run into a problem.