Easy Way to Reinstall Packages When Reinstalling R


When you reinstall R it deletes all your installed packages and reinstalling them manually can be a nuisance. This is an easy way to automatically reinstall all R packages while reinstalling R.


Before Reinstalling R

Before you reinstall R, use setwd() to set a directory where you can save a text file of all installed packages.

Then run these commands to save a list of packages in that directory:

    packages <- installed.packages()[,"Package"] <br>
    save(packages, file="Rpackages")


Reinstalling R

For proper instructions to completely uninstall R go here.

As of right now the correct way to do this on a Mac is to run this command in Terminal:

    sudo rm -rf /Library/Frameworks/R.framework /Applications/R.app \ 
    /usr/bin/R /usr/bin/Rscript


After Reinstalling R

After R has been reinstalled, use setwd() to set R to the same directory where the list of packages has been saved.

Then run:

    load("Rpackages") 
    for (p in setdiff(packages, installed.packages()[,"Package"]))
    install.packages(p)