Author: annepier
transferring installed packages to a different installation of R
It used to take me a while to reinstall all the R packages that I use after upgrading to a new version of R. I couldn’t think of another way to do this than to create a list of installed packages by examining the R package directory, and to manually select and install each one of those packages in the new version of R. In order to ensure that my home and office installation of R had the same packages installed, I did something similar.
I recently discovered that there is a much, much easier way to transfer the packages that you have installed to a different installation of R. I found some R code on the web that I adapted to my needs. Here is what you need to do:
1. Run the script “store_packages.R” in your current version of R.
# store_packages.R
#
# stores a list of your currently installed packages
tmp = installed.packages()
installedpackages = as.vector(tmp[is.na(tmp[,"Priority"]), 1])
save(installedpackages, file="~/Desktop/installed_packages.rda")
(Make sure that all the quotation marks in the script are straight. The scripts will generate an error if they include any curly quotation marks. For some reason, when I saved this blog entry, some quotation marks changed to curly ones. WordPress is probably to blame for this problem, which I have not been able to fix.)
2. Close R. Open the installation of R that you want the packages to be installed in.
3. Run the script “restore_packages.R”.
# restore_packages.R
#
# installs each package from the stored list of packages
load("~/Desktop/installed_packages.rda")
for (count in 1:length(installedpackages)) install.packages(installedpackages[count])
Note that if you want to install the list of packages in an installation of R on a different computer, you should transfer the .rda file that is created by the store_packages script to that computer, and make sure that the path for the “load” command in the restore_packages script is set to the right location.