Skip to content

Instantly share code, notes, and snippets.

@bbolker
Last active November 27, 2023 16:38
Show Gist options
  • Save bbolker/b5721b8f26c2712d0f5fd13860f57895 to your computer and use it in GitHub Desktop.
Save bbolker/b5721b8f26c2712d0f5fd13860f57895 to your computer and use it in GitHub Desktop.
search installed packages for data of specified types
i1 <- installed.packages()
null <- matrix(nrow=0, ncol = 2,
dimnames=list(NULL, c("Item", "type")))
get_data <- function(pkg) {
cat(pkg,"\n")
dd <- data(package=pkg)$results
## library(pkg, character.only = TRUE)
## on.exit(try(detach(paste0("package:", pkg))))
if (nrow(dd) == 0) return(NULL)
FUN <- function(x) {
nm <- gsub(" .*$", "", x["Item"])
data(list=nm, package = pkg)
d0 <- get(nm)
class(d0)[1]
}
aa <- apply(dd, 1, function(x) purrr::safely(FUN, NA)(x)$result)
dplyr::tibble(Item = dd[,"Item"], type = aa)
}
res <- purrr::map_dfr(i1[,"Package"], get_data, .id = "package")
sort(table(res$type), decreasing = TRUE)
dplyr::filter(res, type == "array")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment