Last active
April 13, 2025 19:06
-
-
Save JBGruber/28c79af6d5f9015370feef31da2cb1da to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#' 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() | |
missing_from_cran <- intersect(missing_df$Package, on_cran$Package) | |
if (length(missing_from_cran) > 0) { | |
pak::pak(missing_from_cran) | |
} | |
# 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)}) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Use with: