Spark's Work

Building R Packages: In-Use / Access-Denied Error

When developing R packages, I often need to "build and install" (or sometimes "clean and rebuild") the package after adding new code scripts, after which I would do some simple tests.

However, I have recently been encountering an error regarding writing permissions, as show below.

* installing *source* package 'SomePackage' ...
ERROR: cannot remove earlier installation, is it in use?

Warning in file.rename(instdir, final_instdir) :
  cannot rename file 'some temporary directory for the package in development' 
  to 'the R package directory on my computer', 
  reason 'Access is denied'
ERROR: moving to final location failed

It seems that R is trying to build the package first in a temporary folder, then copy it into the actual package directory on my computer.

The error messages were not quite helpful. I tried restarting the R session to no avail (responding to is it in use?). Regarding the second error message, while it is possible for me to locate the package directory and manually delete it, that is just too much work to do, and quite frankly, annoying.

I looked around on the Internet but found no good solution. So I referred to the options in the command R CMD BUILD (see here). It turns out that adding the "no-lock" option would temporarily solve the issue. So the build command line becomes

R CMD INSTALL --no-lock

This is not the best solution. When first running it, R still pops the first error message is it in use?. However, it then removes the current build of the package from my directory. Then, when I run the same command again, the package is successfully built with updated code scripts.

Whew! As long as it doesn't give me too much trouble, I shall take this solution as accepted.

By the way, it is exactly because of such issues that I have decided to gradually abandon R – so many weird behaviours and so few helpful error messages!