Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

check if package name belongs to a CRAN archived package

Tags:

r

cran

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.

like image 906
Tyler Rinker Avatar asked Nov 08 '15 01:11

Tyler Rinker


People also ask

How many packages are in the current R repository CRAN?

Currently, the CRAN package repository features 18506 available packages.

How do I install a package that has been archived from CRAN?

The way forward is to download the tarball, install all package dependencies and then install the package from the local downloaded file. Note: This will only work if you have the build tools installed on your machine. On Linux this will be the case.

How many packages does CRAN have?

*Actually, as of this writing, there are 9,999 packages on CRAN.

Where are R packages stored?

R packages are a collection of R functions, complied code and sample data. They are stored under a directory called "library" in the R environment. By default, R installs a set of packages during installation.


2 Answers

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")
like image 80
hrbrmstr Avatar answered Oct 12 '22 11:10

hrbrmstr


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
like image 29
Joshua Ulrich Avatar answered Oct 12 '22 10:10

Joshua Ulrich