title: Addressing the "cannot remove prior installation of package" error when updating multiple packages at once
body: <p>This is a problem that comes up repeatedly, e.g.</p>
  
  <ul>
  <li><a href="https://stackoverflow.com/questions/45922418/error-in-install-packages-cannot-remove-prior-installation-of-package-dbi">Error in install.packages : cannot remove prior installation of package ‘DBI’</a></li>
  <li><a href="https://stackoverflow.com/questions/41918543/warning-in-install-packages-cannot-remove-prior-installation-of-package-data">Warning in install.packages : cannot remove prior installation of package ‘data.table’</a></li>
  <li><a href="https://stackoverflow.com/questions/46689061/unable-to-install-package-forecast-in-r">Unable to install package forecast in R</a></li>
  <li><a href="https://stackoverflow.com/questions/44240000/install-error-of-package-plyrof-islr">Install error of package plyr</a></li>
  <li><a href="https://stackoverflow.com/questions/46012471/unable-to-install-package-zoo">Unable to install package zoo</a></li>
  </ul>
  
  <p>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</p>
  
  <pre><code>update.packages(ask = FALSE)
  </code></pre>
  
  <p>and get a number of these errors. The error is misleading though, because R tends to <strong>actually remove</strong> the old package version---but then doesn't install the new version. Once this happens, you can't fix it with <code>update.packages</code>; you have to reinstall it from scratch.</p>
  
  <p>The most frustrating aspect of this problem is that it often occurs when installing <em>dependencies</em>, so e.g. I might successfully update <code>dplyr</code>, but in the process <code>Rcpp</code> is removed. Now I can't load <code>dplyr</code> because <code>Rcpp</code> is missing, so I need to reinstall <code>Rcpp</code>.</p>
  
  <p>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</p>
  
  <pre><code>lapply(dir(.libPaths()), install.packages)
  </code></pre>
  
  <p>which reinstalls my entire library. This is overkill.</p>
  
  <p><strong>Is there a better way to check which folders in <code>.libPaths()</code> actually contain packages, so that I only reinstall the missing packages?</strong> </p>
  
  <p>Alternatively: <strong>Is there a better way to check for missing package depedencies?</strong></p>