Save your R packages when updating R version

This post is a copy and paste of an r-blogger article.

However, it is so useful that I keep it here to remember about the process to save and restore all R packages when updating to a newer version of R.

tmp <- installed.packages()
installedpkgs <- as.vector(tmp[is.na(tmp[,"Priority"]), 1])
save(installedpkgs, file="installed_old.rda")
tmp <- installed.packages()
installedpkgs.new <- as.vector(tmp[is.na(tmp[,"Priority"]), 1])
missing <- setdiff(installedpkgs, installedpkgs.new)
install.packages(missing)
update.packages()

Enjoy!