Skip to content

Instantly share code, notes, and snippets.

@arcaravaggi
Last active July 4, 2024 12:57
Show Gist options
  • Save arcaravaggi/20acc42e3e245cc268a95269de024b2d to your computer and use it in GitHub Desktop.
Save arcaravaggi/20acc42e3e245cc268a95269de024b2d to your computer and use it in GitHub Desktop.
Update R and migrate R packages to new installation from within the console
#From https://stackoverflow.com/questions/1401904/painless-way-to-install-a-new-version-of-r
# Run in the old version of R (or via RStudio)
setwd("C:/Temp/")
packages <- installed.packages()[,"Package"]
save(packages, file="Rpackages")
# INSTALL NEW R VERSION
if(!require(installr)) { install.packages("installr"); require(installr)} #load / install+load installr
# See here for more on installr: https://www.r-statistics.com/2013/03/updating-r-from-r-on-windows-using-the-installr-package/
# step by step functions:
check.for.updates.R() # tells you if there is a new version of R or not.
install.R() # download and run the latest R installer
# Install library - run in the new version of R. This calls package names and installs them from repos, thus all packages should be correct to the most recent version
setwd("C:/Temp/")
load("Rpackages")
for (p in setdiff(packages, installed.packages()[,"Package"]))
install.packages(p)
# Installr includes a package migration tool but this simply copies packages, it does not update them
copy.packages.between.libraries() # copy your packages to the newest R installation from the one version before it (if ask=T, it will ask you between which two versions to perform the copying)
@arcaravaggi
Copy link
Author

Happy to help. :)

@Axel-Shark
Copy link

Thanks so much for this. Every time I update R I forget I need to migrate my packages. This should really be built in to R or RStudio.

@CLRafaelR
Copy link

CLRafaelR commented Jun 28, 2021

JFYI, The for loop above can be replaced with a one-liner with purrr::map, if you once have purrr package or tidyverse in the new version of R before you start installing the packages on it.

purrr::map(packages, install.packages)

@arcaravaggi
Copy link
Author

@CLRafaelR Absolutely. I just prefer to use base code here because, as you note, this supports a full re-installation, rather than requiring the user to install purrr in the first instance. I find it's a neater experience, this way.

@TuringsGhost
Copy link

Thank you so much. The instructions were brilliant and everything worked perfectly. This is incredibly helpful and saves hours of time.

@OliverAshton1
Copy link

Thank you. Much appreciated.

@ckfaber
Copy link

ckfaber commented May 3, 2024

This is so helpful! Would you recommend deleting the packages from the older installations, or do you keep these for any reason?

@arcaravaggi
Copy link
Author

@ckfaber Apologies for the delay. I always keep my older packages but that's more likely due to apathy than anything. Some would say to keep them in case of later packages changes that break your code, but you can include a legacy command in your script related to the then-appropriate package version, anyway.

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