user1830307
user1830307

Reputation:

Determine number of versions in history of R package on CRAN

Is it possible to determine the number of versions a package on CRAN has had in the past?

Upvotes: 6

Views: 682

Answers (2)

Rich Scriven
Rich Scriven

Reputation: 99331

Here's one using the XML package. This just counts the archived versions (more precisely, the number of archived tar.gz files). Add 1 to get the total number of versions, including the current.

nCRANArchived <- function(pkg) {
    link <- paste0("http://cran.r-project.org/src/contrib/Archive/", pkg)
    qry <- XML::getHTMLLinks(link, xpQuery = "//@href[contains(., 'tar.gz')]")
    length(qry)
}

nCRANArchived("data.table")
# [1] 33
nCRANArchived("ggplot2")
# [1] 28
nCRANArchived("MASS")
# [1] 40
nCRANArchived("retrosheet") ## shameless plug
# [1] 2

Upvotes: 6

eipi10
eipi10

Reputation: 93811

Here's a simple function that goes to the CRAN page with the old versions of a given package and counts them.

num.versions = function(package) {

  require(rvest)
  require(stringr)

  # Get text of web page with package version info
  page = read_html(paste0("https://cran.r-project.org/src/contrib/Archive/", package, "/"))
  doc  = html_text(page)

  # Return number of versions (add 1 for current version)
  paste("Number of versions: ", 
        length(unlist(str_extract_all(doc, "tar\\.gz"))) + 1)

}

num.versions("ggplot2")
[1] "Number of versions:  29"

num.versions("data.table")
[1] "Number of versions:  34"

num.versions("distcomp")
[1] "Number of versions:  4"

Upvotes: 3

Related Questions