Building and testing R packages with latest R-Devel

If you want to submit your R package to CRAN, you need to ensure that all R checks will be executed successfully without a warning for the current and the devel version of R.

And not only that, as a package maintainer, you also need to ensure that your submitted packages will still work with upcoming R versions. If you are using native code in your package which needs compilation (e.g. C/C++), the whole process is even getting more complex, since you need to ensure that the compilation of the code works on different systems (debian, fedora, windows (32 and 64 bit), solaris (sic), and osx) and with different compilers (gcc, clang).

But, for the moment let’s focus on setting up an environment to check your package against the latest R-Devel version to be able to reproduce and resolve issues.

Building an R-Devel Docker image

Installing the latest R-Devel version means usually to pull and compile the latest R code. If you don’t want to clutter your system with new dependencies and if you want to have a new clean R-Devel installation, I’d recommend to use Docker containers for testing your package.

On Docker hub you can find the rocker/r-devel:latest Docker image, however contrary to its name (or tag), it usually doesn’t contain the latest R-Devel code (at the time of writing this article, the last image was pushed 2 month ago). That shouldn’t stop you from using Docker though, since we can simply use the recipe (aka Dockerfile), to build our own image.

# Download Docker file
curl https://raw.githubusercontent.com/rocker-org/rocker/master/r-devel/Dockerfile -O

Next we build a Docker image named r-devel-localusing the downloaded Dockerfile.

# Build Docker file
docker build -t r-devel-local .

Congrats. You’ve build a Docker image which contains the latest R-Devel code.

Testing your package with R-Devel

The next logical step is to test your package or more generally to use your package with the R-Devel version in a Docker container.

Let’s assume you have put your package in a folder named mypackage in your home folder (~/mypackage). To link this folder with a Docker container, the parameter -v ~/mypackage:/RPackage/mypackage can be provided when executing docker run. The folder /RPackage/mypackage inside the Docker container will then be linked with the ~/mypackage folder on your host system.

To start a Docker container with the built Docker image and to be able to access your package dir, run the following command:

docker run --name=r-devel-local -v ~/mypackage:/RPackage/mypackage --rm -ti r-devel-local /bin/bash

Next, you can check the R-Devel version:

RD --version

Note that RD starts the devel version of R (instead of R).

Often it is necessary to install system dependencies and other dependent R packages before you can test your package.

For example the following dependencies are necessary to build pdf vignette files (e.g. for my parallelDist package):

apt-get update && \
  apt-get install -y pandoc qpdf ghostscript

You can also install all necessary dependencies for you package with one command:

# Install dependent R packages (e.g. for parallelDist)
RD -e 'install.packages(c("dtw", "Rcpp", "RcppParallel", "RcppArmadillo", "ggplot2", "proxy", "highlight", "testthat", "RcppXPtrUtils"))'

After you have installed all necessary system dependencies and R packages, you are ready to build and test your package against the latest R-Devel version.

Just change to the /RPackage dir, build you package and test it using the following commands:

cd /RPackage
# build you package, this generates a tar.gz file
RD CMD build mypackage/
# test your package with R-Devel and strict --as-cran options
RD CMD check --as-cran mypackage_0.0.1.tar.gz

The advantage of using a Docker container becomes obvious, if the test results contain errors, warnings or notes you have to fix. You can simply work on your package on the host system (or directly in the container) and quickly test your changes in the Docker container against the latest R version. Happy fixing & testing!

comments powered by Disqus