Skip to content

Instantly share code, notes, and snippets.

@jsta
Forked from bearloga/upgrade_packages.R
Last active April 26, 2017 16:35
Show Gist options
  • Save jsta/65f47f4c45012ee1c79f553265917152 to your computer and use it in GitHub Desktop.
Save jsta/65f47f4c45012ee1c79f553265917152 to your computer and use it in GitHub Desktop.
The script can be used to re-install packages after upgrading R (on Windows), since libraries cannot be reused between different minor versions (e.g. when upgrading 3.2.3 to 3.3.2). It detects when a package was installed from CRAN vs GitHub/Git and re-installs it using the appropriate func. Usage: `Rscript upgrade_packages.R`
#Setup##########################################################################
message("Checking for a personal library...")
if (!dir.exists(Sys.getenv("R_LIBS_USER"))) {
warning("Personal library not found, creating one...")
dir.create(Sys.getenv("R_LIBS_USER"), recursive = TRUE)
message("Registering newly created personal library...")
.libPaths(Sys.getenv("R_LIBS_USER"))
} else {
message("Personal library found.")
}
message("Installing devtools for re-installing from GitHub, etc....")
install.packages("devtools", repos = "https://cran.rstudio.com/")
# ^ also ensures that we have a personal library for this version of R
if (! "devtools" %in% installed.packages()[, "Package"]) {
stop("devtools is/was not installed!")
}
#Find the path to the library of the R version that is not current #############
pkg_versions <- as.numeric(dir(strsplit(.libPaths()[1], "/\\d")[[1]][1]))
latest_pkg_versions <- tail(setdiff(pkg_versions[-which.max(pkg_versions)],
max(pkg_versions)), 1)
pkg_path <- dir(strsplit(.libPaths()[1], "/\\d")[[1]][1],
full.names = TRUE)[which(pkg_versions == latest_pkg_versions)]
# List of installed packages in the user's personal library#####################
installed_pkgs <- dir(pkg_path)
if (length(installed_pkgs) == 0) {
stop("Did not find any packages from previous R installation.")
}
message("Found ", length(installed_pkgs), " packages from previous R installation.")
# A helper function for extracting a parameter
# from an installed package's DESCRIPTION file
extract_param <- function(DESCRIPTION, param) {
return(sub(paste0(param, ": "), "", DESCRIPTION[grepl(param, DESCRIPTION)], fixed = TRUE))
}
################################################################################
message("Extracting metadata about packages from previous R installation...")
pkgs_info <- do.call(rbind, lapply(installed_pkgs, function(installed_pkg) {
# message('Checking how "', installed_pkg, '" was installed...')
pkg_description <- readLines(file.path(find.package(installed_pkg, lib.loc = pkg_path), "DESCRIPTION"))
if (any(grepl("Repository: CRAN", pkg_description))) {
# message('"', installed_pkg, '" was installed from CRAN.')
pkg_source <- "cran"; pkg_url <- NA
} else if (any(grepl("RemoteType", pkg_description))) {
# message('"', installed_pkg, '" was installed from a remote source like GitHub.')
pkg_source <- extract_param(pkg_description, "RemoteType")
if (pkg_source == "github") {
pkg_url <- paste(extract_param(pkg_description, "GithubUsername"), extract_param(pkg_description, "GithubRepo"), sep = "/")
} else if (pkg_source == "git") {
pkg_url <- extract_param(pkg_description, "RemoteUrl")
} else {
pkg_source <- "other remote"; pkg_url <- NA
}
} else {
# message('"', installed_pkg, '" was installed from a local source.')
pkg_source <- "local"; pkg_url <- NA
}
return(data.frame(
package = installed_pkg,
source = pkg_source,
url = pkg_url,
stringsAsFactors = FALSE
))
}))
#Re-install packages:###########################################################
if (sum(pkgs_info$source == "cran") > 0) {
message("Re-installing ", sum(pkgs_info$source == "cran"), " R packages from CRAN...")
install.packages(pkgs_info$package[pkgs_info$source == "cran"], repos = "https://cran.rstudio.com/")
}
if (sum(pkgs_info$source == "github") > 0) {
message("The following ", sum(pkgs_info$source == "github"), " packages will be re-installed from GitHub: ", paste(pkgs_info$package[pkgs_info$source == "github"], collapse = ", "))
devtools::install_github(pkgs_info$url[pkgs_info$source == "github"])
}
if (sum(pkgs_info$source == "git") > 0) {
message("The following ", sum(pkgs_info$source == "git"), " packages will be re-installed from Git repos: ", paste(pkgs_info$package[pkgs_info$source == "git"], collapse = ", "))
devtools::install_git(pkgs_info$url[pkgs_info$source == "git"])
}
if (sum(pkgs_info$source %in% c("other remote", "local")) > 0) {
message("The following ", sum(pkgs_info$source %in% c("other remote", "local")), " packages will need to be manually re-installed (sorry): ", paste(pkgs_info$package[pkgs_info$source %in% c("other remote", "local")], collapse = ", "))
}
################################################################################
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment