transferring installed packages to a different installation of R

Posted on Updated on


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.

Advertisements

6 thoughts on “transferring installed packages to a different installation of R

    tiflo said:
    June 1, 2012 at 4:06 pm

    Hey AP, thanks for posting this.

    Like

    wilsonfreitas said:
    January 25, 2017 at 6:17 am

    It really worked, but the loop isn’t necessary. Simply do

    install.packages(installedpackages)

    Thanks for posting

    Like

    Andi said:
    May 11, 2017 at 1:52 pm
      tiflo said:
      May 11, 2017 at 2:31 pm

      Times have changed ;).

      Like

    feirblog said:
    June 8, 2017 at 3:06 pm

    Reblogged this on Hui Tang's R Site and commented:
    A user suggested that the loop isn’t necessary. Simply do

    install.packages(installedpackages)

    Like

Questions? Thoughts?

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s