Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

R can't find packages installed by travis

We're trying to add some unit tests to the caret package that get run by travis, but not on CRAN. This saves build time on CRAN and reduces the number of dependencies they have to install to check our package, while letting us run a more complete test suite on travis.

I thought I could simply install the requirements for the test using the r_packages: line in my travis.yml file:

r_packages:
 - ROSE
 - DMwR

However, my NOT_CRAN=TRUE builds are still failing. (NOT_CRAN=FALSE runs fine as the problematic tests are skipped)

This is really strange, as when I look at the build logs, I see travis successfully installing all the packages I need:

* installing *source* package ‘ROSE’ ...
** package ‘ROSE’ successfully unpacked and MD5 sums checked
** R
** data
** inst
** preparing package for lazy loading
** help
*** installing help indices
** building package indices
** testing if installed package can be loaded
* DONE (ROSE)

But when my tests run that depend on those packages, R can't find them:

> library(testthat)
> library(caret)
Loading required package: lattice
Loading required package: ggplot2
> 
> test_check("caret")
[1] "Reduced dimension to 3 by default. "
1 package is needed for this model and is not installed. (ROSE). Would you like to try to install it now?1. Error: check appropriate sampling calls by name -----------------------------

1: withCallingHandlers(eval(code, new_test_environment), error = capture_calls, message = function(c) invokeRestart("muffleMessage"), 
       warning = function(c) invokeRestart("muffleWarning"))
2: eval(code, new_test_environment)
3: eval(expr, envir, enclos)
4: caret:::parse_sampling(i) at test_sampling_options.R:14
5: checkInstall(pkgs)
6: stop()

testthat results ================================================================
OK: 62 SKIPPED: 0 FAILED: 1
1. Error: check appropriate sampling calls by name 

Error: testthat unit tests failed
Execution halted

(I think) the relevant line of code is here in caret's source code:

good <- rep(TRUE, length(pkg))
  for(i in seq(along = pkg)){
    tested <- try(find.package(pkg[i]), silent = TRUE)
    if(class(tested)[1] == "try-error") good[i] <- FALSE
  }

Why can't the find.package function find packages installed by travis? Do they go in a special, separate library somewhere?

Also, as an aside, how do I make my travis builds for r less verbose? By default they seem to print way too much information (e.g. it echoes all code run by the tests and manual, even code that doesn't error).

like image 386
Zach Avatar asked Aug 07 '15 13:08

Zach


2 Answers

When testing your package on Travis, R CMD check appears to be looking for installed packages in the wrong place(s).

I created a small test package to figure this out:

When testing the package on Travis using R CMD check, .libPaths() contains:

c("/tmp/RtmpnQE1WM/RLIBS_29bd3940b7fa",
  "/usr/lib/R/library")

When testing the package on Travis using devtools::test(), .libPaths() contains:

c("/usr/local/lib/R/site-library",
  "/usr/lib/R/site-library",
  "/usr/lib/R/library")

By default, R packages on Travis are installed into /usr/local/lib/R/site-library (i.e. the first entry of .libPaths()). Clearly, R CMD check is looking in the wrong place(s).

In principle, we could use the --library argument for R CMD check to point to the right place, however when you use --as-cran then --library defaults to /usr/lib/R/library.

The easiest solution is probably to install all packages (in particular the "additional" packages ROSE and DMwR) into /usr/lib/R/library. There are many ways to do that. One solution is to add

sudo mkdir -p /usr/lib/R/library
echo 'R_LIBS=/usr/lib/R/library:/usr/lib/R/site-library/' > ~/.Renviron
sudo chmod 2777 /usr/lib/R/library

to the before_install section of your .travis.yml file.

like image 165
jtilly Avatar answered Oct 07 '22 23:10

jtilly


You could clone the r-travis repo and just source from your copy. That would allow you to make it less verbose.

As to "packages not found": dunno. But the Travis instance is a vanilla Ubuntu installation so you can control things by echo'ing into a suitable ~/.Rprofile etc pp. I have found the old r-travis setup to be more convenient for me and recently blogged about one way to dramatically cut test times down by relying more on pre-built r-cran-* .deb packages.

Michael has well over 1000 in his repo, and you could build your own too via a PPA. Time permitting I may write another blog post detailing that...

like image 44
Dirk Eddelbuettel Avatar answered Oct 07 '22 23:10

Dirk Eddelbuettel