R/updatePackages.R

Defines functions updatePackages

Documented in updatePackages

# After updating R, reinstall all packages that where installed on the machine
#' updatePackages Function
#'
#' This function needs only to be run when R is updated (i.e once a year). It will look at the old list of packages that where installed and will try to install them. It will also returns a list of the packages that failed (probably mostly Github packages).
#'
#' @param oldLib where the old packages where stored
#' @param newLib where the new packages will be stored
#' @importFrom utils install.packages installed.packages
#' @export
#' @import BiocManager
updatePackages <- function(oldLib, newLib = .libPaths()[1]) {
  if (!requireNamespace("BiocManager", quietly = TRUE)) {
    install.packages("BiocManager")
  }
  install(update = TRUE, ask = FALSE)
  packages <- as.data.frame(installed.packages(oldLib, stringsAsFactors = F))
  for (package in packages$Package) {
    if (!requireNamespace(package, quietly = TRUE)) {
      try(install(package, lib = newLib, update = FALSE, ask = FALSE))
    }
  }
  packages2 <- as.data.frame(installed.packages(newLib, stringsAsFactors = F))
  failed <- dplyr::anti_join(packages, packages2, by = c("Package" = "Package"))
  return(failed)
}
HectorRDB/DailyHRB documentation built on Oct. 15, 2020, 10:39 a.m.