Search code examples
rpackagedemocranvignette

R: How to tell which packages in a Task View have associated vignettes or demos


Without installing the packages, is there a quick way to find out which packages in a given Task View have vignettes or demos associated with them? I've figured out how to tell which packages are in a given Task View from R:

library(ctv)
# get all the available task views
taskviews<-available.views()

# get task view of interest (e.g. "TimeSeries")
tv_of_interest<-taskviews[[which(sapply(taskviews,'[',1)=="TimeSeries")]] 

# get all the packages in the task view
pckgs <- tv_of_interest[['packagelist']][1]

Here is what I've tried, but the attempts are not right, as they only consider packages that I already have:

vignette(package= c(pckgs)) 
browseVignettes(package= c(pckgs))
demo(package=c(pckgs))

I am hoping to avoid scraping, as I have no experience with it, but perhaps it's the only way. Any thoughts?


Solution

  • You don't need to scrape, just test for the existence of a vignettes folder on the web of CRAN. For efficiency, use httr and HEAD:

    hasvig <- function(packagename){
        require(httr)
        url = paste0(getOption("repos"),"/web/packages/",packagename,"/vignettes")
        c = HEAD(url)
        return(c$status_code==200)
    }
    

    Proof by induction:

    > hasvig("sp")
    [1] TRUE
    > hasvig("abd")
    [1] FALSE
    

    it works for those, so it works for everything.

    Vectorise it if you want:

    > hasvigs = Vectorize(hasvig)
    > hasvigs(c("sp","abd","Rcpp"))
       sp   abd  Rcpp 
     TRUE FALSE  TRUE 
    

    The only way to tell if a package has a demo is to download the source archive file and see if it has a demo subdir - CRAN sites don't have extracted source code and its not stored in metadata anywhere. Doable, but messy, slow, requires downloading all of CRAN to test all packages.