Search code examples
rcran

check if package name belongs to a CRAN archived package


How can one check of a package has been archived from CRAN. One can check if a package is a CRAN package like so:

"ggplot2" %in% available.packages()[,1]
## [1] TRUE

But a package like helpr shows false with the same code. How could I check if a name is archived?

"helpr" %in% available.packages()[,1]
## [1] FALSE

I could scrape the archive like this:

archs <- XML::readHTMLTable(readLines("https://cran.r-project.org/src/contrib/Archive/"), 
    stringsAsFactors = FALSE)

gsub("/$", "", na.omit(archs[[1]][, "Name"]))

but I assume there is a built in base way to to do this as using an archived package name will throw a warning in a CRAN check.


Solution

  • R CMD check basically calls tools:::.check_packages. The functionality you're looking for is in tools:::.check_package_CRAN_incoming, and tools:::CRAN_archive_db.

    Edit (by Tyler Rinker) Using Josh's answer the following code gives me what I'm after though less well succint than @hrbrmstr's:

    get_archived <- function(cran = getOption("repos")){
        if (is.null(cran)) cran <- "http://cran.rstudio.com/"
        con <- gzcon(url(sprintf("%s/%s", cran, "src/contrib/Meta/archive.rds"), open = "rb"))
        on.exit(close(con))
        x <- readRDS(con)
        names(x)
    }
    
    
    check_archived <- function(package){
        tolower(package) %in% tolower(get_archived())
    }
    
    check_archived("ggplot2")
    check_archived("helpr")
    check_archived("foo")
    
    ## > check_archived("ggplot2")
    ## [1] TRUE
    ## > check_archived("helpr")
    ## [1] TRUE
    ## > check_archived("foo")
    ## [1] FALSE