Warning in install.packages: unable to move temporary installation

Viewed 34914

I've found a number of questions related to this warning when installing or updating packages in R/RStudio, but none seem to completely match my situation:

  • Corporate Windows 7 system, so no access to admin privileges
  • No way to make changes to McAfee Anti-Virus exceptions lists
  • R is fully installed in the user space C:\Users\[myname]\R
  • RStudio fully installed in userspace C\Users\[myname]\RStudio
  • no permission issues in either of the directories... I have full access control over them
  • Problem only started after installing R 3.4, but RStudio has randomly failing at start or hanging for a few months now
  • R_LIBS_USER added as user environment variable, pointing to right directory
  • .libPaths() show correct directories, both system and user
  • R version 3.4.2, RStudio version 1.0.153
  • Uninstalled both R and Rstudio and did a clean re-install of both
  • Tried trace(utils:::unpackPkgZip,edit = T) and edited Line 140 Sys.sleep(0.5) to Sys.sleep(2), which sometimes works temporarily but the edit won't stay put... resets to Sys.sleep(0.5) on every session restart
  • Happens in both RStudio and RGui
  • Any package larger than a few Kb gives the message:

    package ‘packagename’ successfully unpacked and MD5 sums checked
    Warning in install.packages :
      unable to move temporary installation ‘C:\Users\[myname]\R\win-library\3.4\file2b884fc37c13\packagename’ to ‘C:\Users\[myname]\R\win-library\3.4\packagename’
    

The packages are failing to install or update. So, my questions are:

  1. is there a way to avoid the problem altogether that doesn't require admin privileges or changes to the antivirus policies?
  2. is there a way to get the edit to unpackPkgZip to save permanently?

At this point, I'm stumped. I suspect it has something to do with the antivirus temporarily locking the file/directory after download, but I can't do anything about it from that end. The Sys.sleep(2) seems to do the trick, but I can't keep doing that before every package install or update and can't seem to get the edit to stay put.

11 Answers

If you cannot turn off your antivirus here is a workaround that I found that doesn't involve editing the unpackPkgZip file. Debugging the unzip package function and then stepping through it gives the antivirus enough time to do its job without interfering. Use this command:

debug(utils:::unpackPkgZip) install.packages("packageName")

and then step through the code (by pressing enter many times) when R starts debugging during the installation.

I found this solution here.

If you want to make this change more permanent you can add the debug code into your Rprofile file, see here, but you'll still need to use step through the unzip function each time a package is installed.

Got the same error - seems to be a company gp / access security problem.

It might also be worthwhile checking whether the folder it fails to write to has a Read Only structure (Right Click - Properties). This folder's address can be found by running: .libPaths()[1] in R.

An ad hoc solution to this problem is to unzip and store the downloaded (but not moved) packages using a piece of R code below. You will get an error stating where the binary packages are located (something like: C:/Users/....AppData/...)

Now you can simply unzip the files from here to your .libPaths() location

zipF <- list.files("C:/Users/<YOURNAMEHERE>/AppData/Local/Temp/Rtmp4Apz6Z/downloaded_packages", full.names = TRUE)
outDir <- .libPaths()[1]

for(i in 1: length(zipF)) {
unzip(zipF[i],exdir=outDir)
}

A more general solution will still be extremely worthwhile, as this is unfortunately a common problem when updating R on Windows.

Extending the Sys.sleep value to 3.5 on line 142 in the unpackPkgZip function works manually via

trace(utils:::unpackPkgZip, edit=TRUE)

However, it can also be done programmatically by running the following before install.packages:

localUnpackPkgZip <- utils:::unpackPkgZip
body(localUnpackPkgZip)[[14]][[4]][[4]][[4]][[3]][[3]][[2]][[2]] <- substitute(3.5)
assignInNamespace("unpackPkgZip", localUnpackPkgZip, "utils")

This must be run every time you have a new session. You can run it multiple times in the same session without issue.

If you run the below statement right before the install.packages expression then it should install the package:

trace("unpackPkgZip", where=asNamespace("utils"), quote(Sys.sleep(2.5)), at=14L, print=FALSE)
Related