Addressing the "cannot remove prior installation of package" error when updating multiple packages at once

mikeck picture mikeck · Feb 13, 2018 · Viewed 7.2k times · Source

This is a problem that comes up repeatedly, e.g.

I have come across this issue multiple times. Most annoying is when it occurs with a bunch of packages; e.g. I do something like

update.packages(ask = FALSE)

and get a number of these errors. The error is misleading though, because R tends to actually remove the old package version---but then doesn't install the new version. Once this happens, you can't fix it with update.packages; you have to reinstall it from scratch.

The most frustrating aspect of this problem is that it often occurs when installing dependencies, so e.g. I might successfully update dplyr, but in the process Rcpp is removed. Now I can't load dplyr because Rcpp is missing, so I need to reinstall Rcpp.

My current workaround (when the number of uninstalled packages is large) is to close all of my R sessions, open a new one with the base R GUI (i.e. not RStudio or RTVS) and do

lapply(dir(.libPaths()), install.packages)

which reinstalls my entire library. This is overkill.

Is there a better way to check which folders in .libPaths() actually contain packages, so that I only reinstall the missing packages?

Alternatively: Is there a better way to check for missing package depedencies?

Answer

mikeck picture mikeck · Feb 13, 2018

One strategy is to do

setdiff(dir(.libPaths()), .packages(all = TRUE))

To get a list of directories in the library that don't seem to actually contain packages. Now I have a much smaller list of packages to reinstall.

I'm not sure if it's foolproof, but it's better than reinstalling my entire library.