Skip to content

Instantly share code, notes, and snippets.

@muschellij2
Created April 1, 2021 18:24
Show Gist options
  • Save muschellij2/0488dd47141eabeab0869debdfd0f741 to your computer and use it in GitHub Desktop.
Save muschellij2/0488dd47141eabeab0869debdfd0f741 to your computer and use it in GitHub Desktop.
Install Old Dependencies in R
old_package_version = function(repos = getOption("repos")) {
all_info = vector(mode = "list", length = length(repos))
names(all_info) = repos
for (repo in repos) {
# taken from remotes:::package_find_archives
archive <- tryCatch({
tf <- tempfile(fileext = ".gz")
on.exit(unlink(tf), add = TRUE)
remotes:::download(tf, sprintf("%s/src/contrib/Meta/archive.rds",
repo))
con <- gzfile(tf, "rb")
on.exit(close(con), add = TRUE)
readRDS(con)
}, warning = function(e) list(), error = function(e) NULL)
if (!is.null(archive)) {
archive = dplyr::bind_rows(archive, .id = "package")
archive$repo <- repo
}
all_info[[repo]] = archive
}
all_info = dplyr::bind_rows(all_info)
all_info
all_info = all_info %>%
dplyr::mutate(mtime = lubridate::as_datetime(mtime)) %>%
dplyr::arrange(package, dplyr::desc(mtime)) %>%
tibble::rownames_to_column(var = "tarball") %>%
dplyr::mutate(version = gsub(".*_(.*)[.]tar.gz", "\\1", tarball))
all_info
}
install_old_dependencies = function(pkgdir = ".",
repos = getOption("repos"),
n_versions_to_try = 3) {
x = remotes::dev_package_deps(pkgdir = pkgdir,
repos = repos,
dependencies = TRUE)
packages_required = x$package
package_check = packages_required %in% available.packages()
if (!all(package_check) && length(packages_required) > 0) {
res = old_package_version()
packages_to_install = packages_required[!package_check]
# nrow(res)/length(unique(res$package))
x = res %>%
dplyr::filter(package %in% packages_to_install)
x = x %>%
dplyr::group_by(package) %>%
dplyr::slice_head(n = n_versions_to_try) %>%
dplyr::select(package, version)
x = split(x, x$package)
for (ipack in seq_along(x)) {
ix = x[[ipack]]
package = ix$package[1]
versions = ix$version
for (iver in seq_along(versions)) {
try({
remotes::install_version(package = package, version = versions[iver])
})
if (package %in% available.packages()) {
break
}
}
}
return(all(packages_required %in% available.packages()))
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment