Skip to content

Instantly share code, notes, and snippets.

@JBGruber
Last active May 14, 2024 17:38
Show Gist options
  • Save JBGruber/28c79af6d5f9015370feef31da2cb1da to your computer and use it in GitHub Desktop.
Save JBGruber/28c79af6d5f9015370feef31da2cb1da to your computer and use it in GitHub Desktop.
#' Install old packages in new version of R
#'
#' @param old_path Input the libPath of the old R version. Leave blank to detect
#' automatically.
#'
#' @return
#' @export
#'
#' @examples
#' get_old_packages()
get_old_packages <- function(old_path = NULL) {
if (is.null(old_path)) old_path <- setdiff(list.dirs(dirname(.libPaths()[1]), recursive = FALSE), .libPaths()[1])
if (length(old_path) > 1) {
sel <- menu(old_path, title = "which one looks like the library path of the previous R version?")
old_path <- old_path[sel]
}
old_packages <- installed.packages(lib.loc = old_path)
new_packages <- installed.packages()
missing_df <- as.data.frame(old_packages[
!old_packages[, "Package"] %in% new_packages[, "Package"],
])
if (!requireNamespace("pak", quietly = TRUE)) install.packages("pak")
# install from CRAN
on_cran <- tools::CRAN_package_db()
pak::pak(intersect(missing_df$Package, on_cran$Package))
# install others
missing_df <- as.data.frame(old_packages[
!old_packages[, 1] %in% installed.packages()[, 1],
])
on_gh <- function(pkg) {
repo = jsonlite::fromJSON(paste0("http://rpkg-api.gepuro.net/rpkg?q=", pkg))
message("-> Looking up ", pkg)
if (length(repo) > 0) {
repo[basename(repo$pkg_name) == pkg, ]
}
}
gh_packages <- do.call("rbind", lapply(missing_df$Package, on_gh))
sel <- 99
while (sel > 0) {
sel <- menu(c("view", "just install", "just return data.frame"),
title = "Do you want to View() GitHub packages before installation?")
switch(sel,
View(gh_packages),
{sel <- 0; pak::pak(gh_packages$pkg_name)},
{sel <- 0; return(gh_packages)})
}
}
@JBGruber
Copy link
Author

JBGruber commented May 12, 2022

Use with:

source("https://gist.githubusercontent.com/JBGruber/28c79af6d5f9015370feef31da2cb1da/raw/get_old_packages.R")
get_old_packages()

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