Skip to content

Instantly share code, notes, and snippets.

@samcofer
Last active August 20, 2024 16:47
Show Gist options
  • Save samcofer/3d295d3f68c578b6dde62ed47ef56b2c to your computer and use it in GitHub Desktop.
Save samcofer/3d295d3f68c578b6dde62ed47ef56b2c to your computer and use it in GitHub Desktop.
Script to download all R packages from an renv.lock file
# Load required libraries
library(jsonlite)
# Path to the renv.lock file
file_path <- "renv.lock"
# Read and parse the JSON content from the file
renv_data <- fromJSON(file_path)
# Extract the package names and versions from the "Packages" section
packages <- renv_data$Packages
package_names <- names(packages)
package_versions <- sapply(packages, function(pkg) pkg$Version)
package_repo <- sapply(packages, function(pkg) pkg$Repository)
# Combine the package names and versions into a data frame
package_info <- data.frame(Package = package_names, Version = package_versions, Repos = package_repo, stringsAsFactors = FALSE)
# Print the package information
print(package_info)
package_urls <- sapply(1:length(package_names), function(i) {
if (grepl("CRAN", package_repo[i], ignore.case = TRUE) | grepl("RSPM", package_repo[i], ignore.case = TRUE)) {
paste0("http://cran.r-project.org/src/contrib/",package_names[i], "_", package_versions[i], ".tar.gz")
} else if (grepl("bioc", package_repo[i], ignore.case = TRUE)) {
paste0("https://packagemanager.posit.co/bioconductor/packages/3.16/bioc/src/contrib/", package_names[i], "_", package_versions[i], ".tar.gz")
} else {
paste0("https://example.com/", package_names[i], "_", package_versions[i], ".tar.gz")
}
})
# Define alternate URLs (you can customize this further based on your needs)
alternate_urls <- sapply(1:length(package_names), function(i) {
if (grepl("CRAN", package_repo[i], ignore.case = TRUE) | grepl("RSPM", package_repo[i], ignore.case = TRUE)) {
paste0("https://cran.r-project.org/src/contrib/Archive/", package_names[i],"/", package_names[i], "_", package_versions[i], ".tar.gz")
} else {
paste0("No alternate URL")
}
})
dir.create("package_downloads", showWarnings = FALSE)
for (i in 1:length(package_urls)) {
# Define the destination file path
destfile <- file.path(paste0("package_downloads/",package_names[i], "_", package_versions[i], ".tar.gz"))
# Try downloading the file from the primary URL
tryCatch({
print(paste0("Primary URL", package_urls[i]))
download.file(package_urls[i], destfile, method = "auto")
cat("Downloaded:", package_names[i], "version", package_versions[i], "from primary URL.\n")
}, error = function(e) {
cat("Primary URL failed for", package_names[i], "version", package_versions[i], ". Trying alternate URL...\n")
# Try downloading from the alternate URL if the primary fails
tryCatch({
print(alternate_urls[i])
download.file(alternate_urls[i], destfile, method = "auto")
cat("Downloaded:", package_names[i], "version", package_versions[i], "from alternate URL.\n")
}, error = function(e) {
cat("Failed to download:", package_names[i], "version", package_versions[i], "from both URLs.\n")
})
})
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment