This is a problem that comes up repeatedly, e.g.
- Error in install.packages : cannot remove prior installation of package ‘DBI’
- Warning in install.packages : cannot remove prior installation of package ‘data.table’
- Unable to install package forecast in R
- Install error of package plyr
- Unable to install package zoo
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?