Skip to content

Instantly share code, notes, and snippets.

@rplzzz
Created August 27, 2019 21:57
Show Gist options
  • Save rplzzz/795bdcbbcbde3ed26a578ee07885b25b to your computer and use it in GitHub Desktop.
Save rplzzz/795bdcbbcbde3ed26a578ee07885b25b to your computer and use it in GitHub Desktop.
R functions for saving the list of installed packages and then reinstalling them all when you upgrade R
savepkgs <- function(saveloc)
{
instpkg <- installed.packages()
cranpkg <- available.packages()
iscran <- instpkg[,'Package'] %in% cranpkg[,'Package']
pkgdata <- data.frame(package=instpkg[,'Package'], iscran=iscran, instvers=instpkg[,'Version'])
write.csv(pkgdata, saveloc)
}
restore_pkgs <- function(pkglistfile)
{
pkgtbl <- read.csv(pkglistfile, stringsAsFactors = FALSE)
pkglist <- pkgtbl$package[pkgtbl$iscran]
## install the packages we can get from CRAN
install.packages(pkglist, dependencies=TRUE, type='source', Ncpus=8)
## Message about the ones we can't get from CRAN
nocran <- pkgtbl$package[!pkgtbl$iscran]
nocranstr <- paste(nocran, collapse=', ')
message('The following packages were not available from CRAN: ', nocranstr)
## Return the list of not-installed packages for the user's consideration
nocran
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment