check if package name belongs to a CRAN archived package
Asked Answered
I

3

6

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.

Izaak answered 8/11, 2015 at 1:0 Comment(4)
I assume there is a built in base way: why?Onassis
@DirkEddelbuettel if you use helpr as a package name and run r cmd check -as.cran a warning is thrown. The check must be using something or I am assuming because I don't know what r cmd is doing.Izaak
All the code for that is in the tools package (or maybe utils, I always mix that up), so did you look?Onassis
@DirkEddelbuettel Thanks Yeah I thought so too (at least on the utils) and so looked at the utils but did not see what I believe gives that. Maybe I need to understand more how R CDM check` works. I'll look through tools there may be something in there.Izaak
W
4

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
Waves answered 8/11, 2015 at 2:52 Comment(5)
I added code based on your response. If it doesn't fit feel free to roll back.Izaak
This doesn't seem to return the correct result for package mm. check_archived("mm") returns FALSE, but R CMD check --as-cran gives two messages: Conflicting package names (submitted: mm, existing: MM [https://CRAN.R-project.org]) and Conflicting package names (submitted: mm, existing: MM [CRAN archive])Freesia
ohhh... it's case sensitive check_archived("MM") returns TRUEFreesia
@Joshua I made an edit which reflects CRAN policy (that case doesn't matter e.g. if MM exists, then mm is not allowed)Freesia
@Freesia thanks! I knew the policy, but didn't account for it in the answer. Thank you for fixing!Waves
S
5

FWIW, rolling your own CRAN_archive_db would be something like:

download.file("https://cran.rstudio.com/src/contrib/Meta/archive.rds",
              "archive.rds")
archive <- readRDS("archive.rds")
Subroutine answered 8/11, 2015 at 3:0 Comment(0)
W
4

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
Waves answered 8/11, 2015 at 2:52 Comment(5)
I added code based on your response. If it doesn't fit feel free to roll back.Izaak
This doesn't seem to return the correct result for package mm. check_archived("mm") returns FALSE, but R CMD check --as-cran gives two messages: Conflicting package names (submitted: mm, existing: MM [https://CRAN.R-project.org]) and Conflicting package names (submitted: mm, existing: MM [CRAN archive])Freesia
ohhh... it's case sensitive check_archived("MM") returns TRUEFreesia
@Joshua I made an edit which reflects CRAN policy (that case doesn't matter e.g. if MM exists, then mm is not allowed)Freesia
@Freesia thanks! I knew the policy, but didn't account for it in the answer. Thank you for fixing!Waves
B
2

I think the somewhat-recently-released package available by the ROpenSciLabs is designed for this (and much more):

github.com/ropenscilabs/available

Its readme (as of now) lists:

  • Checks for validity
  • Checks not already available on GitHub, CRAN and Bioconductor
  • Searches Urban Dictionary, Wiktionary and Wikipedia for unintended meanings
  • Can suggest possible names based on text in the package title or description.
Bowlder answered 30/8, 2017 at 1:47 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.