Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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?

like image 536
mikeck Avatar asked Feb 13 '18 18:02

mikeck


1 Answers

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.

like image 56
mikeck Avatar answered Nov 11 '22 17:11

mikeck