Skip to content

Instantly share code, notes, and snippets.

@brodieG
Last active June 18, 2019 21:02
Show Gist options
  • Save brodieG/e60c94d4036f45018530ea504258bcf3 to your computer and use it in GitHub Desktop.
Save brodieG/e60c94d4036f45018530ea504258bcf3 to your computer and use it in GitHub Desktop.
Check CRAN Results
# started with `foghorn`, but that comes with dependencies and
# slows down R startup. `browseURL` just barely slows startup,
# even if actual page is slow to load.
check_cran_old <- function(email) {
utils::browseURL(
sprintf(
"https://cran.r-project.org/web/checks/check_results_%s.html",
gsub("[^A-Za-z0-9_:.-]", "_", sub("@", "_at_", email))
) ) }
if(interactive()) check_cran_old("[email protected]")
# an alternative that is not as disruptive as `browserURL`.
# The regex is likely to be pretty fragile to changes in
# page structure, but the benefit is no dependencies:
check_cran <- function(
email, cache='~/.R-cran-status.RDS', cache.life=24 * 3600
) {
url <- sprintf(
"https://cran.r-project.org/web/checks/check_results_%s.html",
gsub("[^A-Za-z0-9_:.-]", "_", sub("@", "_at_", email))
)
display_check <- function(x, extra=NULL) {
print(x)
err.cols <- unlist(x[names(x) %in% c("WARNING", "ERROR")])
if(sum(as.numeric(err.cols), na.rm=TRUE))
writeLines(c("\033[41mErrors/Warnings Present\033[m", url))
writeLines(c(extra, ""))
}
renew.cache <- TRUE
if(file.exists(cache)) {
cache.dat <- readRDS(cache)
cache.age <- Sys.time() - cache.dat[[1]]
if(as.double(cache.age, 'secs') < cache.life) {
renew.cache <- FALSE
display_check(
cache.dat[[2]],
c("",
sprintf(
"cached CRAN status (%s old).", format(round(cache.age))
) ) ) } }
if(renew.cache) {
cat("connecting to CRAN...")
page <- readLines(url)
cat("\r \r")
pattern <- "\\s*<t[hd].*?>(.*?)</t[hd]>"
has.rows <- grep(pattern, page, perl=TRUE)
strings <- gregexpr(pattern, page[has.rows], perl=TRUE)
res <- sapply(
regmatches(page[has.rows], strings),
function(x) {
submatch <- regexec(pattern, x, perl=TRUE)
vapply(regmatches(x, submatch), "[[", character(1L), 2)
}
)
res.mx <- t(gsub("<[^>]*>|^\\s+|\\s+$", "", res))
res.mx.2 <- res.mx[-1, ]
colnames(res.mx.2) <- res.mx[1, ]
res.df <- as.data.frame(res.mx.2, stringsAsFactors=FALSE)
saveRDS(list(Sys.time(), res.df), cache)
display_check(res.df)
}
}
if(interactive()) check_cran('[email protected]')
@adamhsparks
Copy link

adamhsparks commented Aug 29, 2018

Line 65: if(interactive) check_cran('[email protected]')

should be

if(interactive()) check_cran('[email protected]')

@adamhsparks
Copy link

It works, reporting the values as your screenshot on Twitter did, but I get this error on start.

Error in display_check(cache.dat[[2]], c("", sprintf("cached CRAN status (%s old).", : (list) object cannot be coerced to type 'double'

@brodieG
Copy link
Author

brodieG commented Aug 29, 2018

You must have Errors/Warnings on CRAN! I did not have them when I wrote this, so that branch of the code was not properly tested. I just fixed it. Let me know if it still doesn't work for you. (@adamhsparks; not sure how notifications work on gist).

Thanks for helping test this.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment