Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get CRAN R package versions of specific date

Tags:

r

Is there an easy way to get a list with CRAN packages and their versions for a specific date?

E.g. code like this:

package_versions(packages = c("data.table", "lubridate"), date = "2018-01-01")

# Output
tribble(~package,     ~version,
        "data.table", "1.10.4-3",
        "lubridate",  "1.7.1")

One idea would be to set a checkpoint to a specific date and then use something like packageVersion. But I wonder if there is some simpler way without the need to install all those packages. Maybe scraping https://mran.microsoft.com/timemachine?


2 Answers

Try it:

pack_version <- function(pack_list,date){
  require(rvest)
  myfunc <- function(x=pack_list,){
     url<-paste0("https://cran.microsoft.com/snapshot/", date,"/web/packages/", x, "/index.html")
     webpage <- read_html(url)
     table <- html_nodes(webpage, xpath='//td')
     html_text(table)[2]
}
  sapply(pack_list, myfunc, date=date) 
}

pack_list <- c("ggplot2", "abind")
date <- "2016-08-01"
pack_version(pack_list, date)

  ggplot2   abind 
  "2.1.0" "1.4-5"
like image 146
Iman Avatar answered Dec 15 '25 11:12

Iman


Now that MRAN doesn't exist, here's an updated version of Lucas Prestes solution that's using the CRAN archive (but it returns the package version url)

pack_version <- function(pack_list, date){

require(rvest)
require(dplyr)

myfunc <- function(x=pack_list, date=date){
  url<-paste0("https://cran.r-project.org/src/contrib/Archive/", x)
  webpage <- read_html(url)
  table <- html_table(webpage)[[1]]
  paste0(url, "/",
       table[, nchar(names(table)) > 0] |>
         filter(!is.na(`Last modified`) & `Last modified` != "") |>
         mutate(
           date_from = as.Date(`Last modified`, format = "%Y-%m-%d"),
           date_to = lead(date_from, default = Sys.Date())
         ) |>
         filter(date_from <= !!date & date_to >= !!date) |>
         pull(Name)
  )          
}
  sapply(pack_list, myfunc, date=date) 
}

r_date <- "2022-06-23" # version 4.1.2
r_packages <- c("ggplot2", "abind")

pack_version(r_packages, r_date)
like image 28
Andy Barker Avatar answered Dec 15 '25 11:12

Andy Barker



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!