Skip to content

Instantly share code, notes, and snippets.

@florianhartig
Last active June 15, 2022 08:29
Show Gist options
  • Star 27 You must be signed in to star a gist
  • Fork 8 You must be signed in to fork a gist
  • Save florianhartig/10527319 to your computer and use it in GitHub Desktop.
Save florianhartig/10527319 to your computer and use it in GitHub Desktop.
Script to copy the packages installed on one computer or R version to another. Originally from http://stackoverflow.com/questions/1401904/painless-way-to-install-a-new-version-of-r-on-windows
# modified from http://stackoverflow.com/questions/1401904/painless-way-to-install-a-new-version-of-r-on-windows
# run on old computer / r version
setwd("/Users/Florian/Dropbox/temp") # or any other existing temp directory
packages <- installed.packages()[,"Package"]
save(packages, file="Rpackages")
# run on new computer / r version
setwd("/Users/Florian/Dropbox/temp") # or any other existing temp directory
load("Rpackages")
for (p in setdiff(packages, installed.packages()[,"Package"]))
install.packages(p)
@Troyee13
Copy link

Thanks a lot!

@Teebusch
Copy link

You could also use renv::snapshot() & renv::restore() to save & restore package versions per project – no setwd(), no Dropbox. Works like a charm and extra reproducibility makes future-you happy 💙
https://rstudio.github.io/renv/

@florianhartig
Copy link
Author

Hi, good idea, but seems to me that you will still need a dropbox or similar to transfer the info between computers?

@Teebusch
Copy link

Teebusch commented Apr 20, 2021

True, but renv will do most of the heavy lifting for you. You only have to transfer your projects' code together with the .lock file and occasionally run renv::snapshot() and renv::restore() to keep packages in sync between systems.

renv creates a system-wide cache of packages, but each project gets its own package library: When you run snapshot(), renv stores all info about the packages a project uses in a .lock file. When your run restore() on a new system, it checks in that systems package cache, and if a package you need is missing it installs it from CRAN (or the repo of your choice).

So you only transfer the .lock file and run snapshot() and restore() to keep things in sync. A cool things about this is that the package cache can contain different package versions at the same time and projects on the same system may use different versions of the same package.

renv eliminates the need to transfer and reinstall your whole R package library. Instead you only synchronize the packages you are actually using in your projects, and you are even keeping track of the exact package versions, which is great -- no more package API changes that break your old code.

Here's a good intro talk about renv

@florianhartig
Copy link
Author

OK, I will look at this, thanks for the hint!

@ML33M
Copy link

ML33M commented Aug 27, 2021

Thank you for sharing this

@Iegor-Vyshnevskyi
Copy link

Thanks!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment