From 8f265499cd8bec0635967edfb11d56c3fbf49dc1 Mon Sep 17 00:00:00 2001 From: pat-s Date: Sun, 4 Aug 2024 09:46:03 +0200 Subject: [PATCH] add helper --- get-all-pkg-versions.R | 51 ++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 51 insertions(+) create mode 100644 get-all-pkg-versions.R diff --git a/get-all-pkg-versions.R b/get-all-pkg-versions.R new file mode 100644 index 0000000..77fc713 --- /dev/null +++ b/get-all-pkg-versions.R @@ -0,0 +1,51 @@ +# Load necessary libraries +library(httr2) +library(dplyr) +library(tidyr) +library(xml2) +library(tools) +library(future) +library(future.apply) + +# Define the base URL for the CRAN archive +base_url <- "https://cran.r-project.org/src/contrib/Archive/" + +# Function to get all package names using tools::CRAN_package_db() +get_package_names <- function() { + cran_db <- CRAN_package_db() + package_names <- unique(cran_db$Package) + return(package_names) +} + +# Function to get all archived versions of a package with error handling +get_archived_versions <- function(package_name) { + package_url <- paste0(base_url, package_name, "/") + + # Try to perform the request and handle any errors + tryCatch({ + response <- request(package_url) %>% req_perform() + page <- response %>% resp_body_string() %>% read_html() + version_links <- page %>% xml_find_all("//a") %>% xml_attr("href") + # Extract version numbers + versions <- gsub(paste0(package_name, "_|\\.tar\\.gz"), "", version_links) + # Filter out invalid version strings + versions <- versions[grep("^[0-9]+\\.[0-9]+\\.[0-9]+$", versions)] + return(data.frame(Package = package_name, Version = versions, stringsAsFactors = FALSE)) + }, error = function(e) { + # If there's an error (e.g., HTTP 404), return NULL + return(NULL) + }) +} + +# Get all package names +package_names <- get_package_names() + +# Set up parallel processing +plan(multisession, workers = parallel::detectCores()) + +# Get archived versions for all packages in parallel +all_packages_df <- future_lapply(package_names, get_archived_versions) %>% + bind_rows() + +# Display the dataframe +print(all_packages_df)