A couple of weeks ago while trying to build an R package I noticed that devtools::document hangs. At first I assumed there might some examples which were
opening some data base connections, but the problem persisted even after removing all the examples. In fact, it was so bad that I couldn't start any other R sessions.
Deleting the R project also did not help.
I gave up after a while, killed all R processes, and continued working on a more urgent issue on another project, where, magically, I had no problems.
A few days later, going back to the problematic package, the problem reappeared. I asked my colleagues, but it looked like I was the only one experiencing it.
So I had to find a solution. Armed with a lot of patience, I started step-by-step debugging using the debugonce function.
Here are my notes from the process:
devtools::document()
debugonce(roxygen2::roxygenise)
debugonce(roxygen2:::find_load_strategy)
debugonce(roxygen2::load_pkgload)
pkgload::load_all(path, helpers = FALSE, attach_testthat = FALSE)$env
env$.onLoad
function(libname, pkgname)
{
rJava::.jpackage("xlsxjars")
rJava::.jpackage(pkgname, lib.loc = libname) # needed to load RInterface.java
set_java_tmp_dir(getOption("xlsx.tempdir", tempdir()))
wb <- createWorkbook() # load/initialize jars here as it takes
rm(wb) # a few seconds ...
options(xlsx.date.format = "m/d/yyyy") # e.g. 3/18/2013
options(xlsx.datetime.format = "m/d/yyyy h:mm:ss") # e.g. 3/18/2013 05:25:51
}
rJava::.jpackage("xlsxjars")
It turned out that the problem was the loading of the rJava package. I somehow managed to have a corrupted library in my local path. It did not give any errors or anything, just the line:
library(rJava)
never returned. Rebuilding rJava solved the problem.
I had bad feelings for wasting so much time on such a minor thing... On the positive side: This is how I learned the hard way that devtools::document loads all the packages that are under Imports in the DESCRIPTION file of the package.
Fast forward: a few days later, a colleague asks for help: their pipeline started to fail - everything was ok until yesterday, but now it fails. I checked, there was only one internal package which had a new release the day before. I assumed this package the source of the problem, so I asked if there are any tests using that package. The answer was: but no test is run at all, it just hangs at the line:
devtools::test()
And that was the moment: wait, I've been there, done that. My answer came immediately: just try to load all the packages included under Imports in the DESCRIPTION file. And indeed: the packages that was released a day before had some C++ under the belt, which triggered the problem.
Everything is much easier once you know where to look...
Just in case you need it: here is a snippet to automatically load the packages included in Imports (run this inside the root of the package):
# do not mind the meaningless variable names...
x <- desc::desc_get_field("Imports")
y <- unlist(strsplit(x = x, split = ","))
tmp <- gsub(x = y, pattern = " ", replacement = "")
for (i in tmp){
pkg <- unlist(strsplit(x = i, split = "(", fixed = TRUE))[1]
message("loading pkg: ", pkg)
# https://stackoverflow.com/questions/6290605/load-r-package-from-character-string
library(pkg, character.only = TRUE)
}
This was again a reminder to me: it always pays to understand what I'm doing...
Make a promise. Show up. Do the work. Repeat.