Skip to content

Commit

Permalink
add dl_pkgstats_data fn for #24
Browse files Browse the repository at this point in the history
  • Loading branch information
mpadge committed Apr 6, 2022
1 parent ecb314e commit d2bb5ad
Show file tree
Hide file tree
Showing 7 changed files with 94 additions and 3 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/test-coverage.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -47,5 +47,5 @@ jobs:
needs: coverage

- name: Test coverage
run: covr::codecov()
run: covr::codecov(function_exclusions='dl_pkgstats_data')
shell: Rscript {0}
4 changes: 3 additions & 1 deletion DESCRIPTION
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
Package: pkgstats
Title: Metrics of R Packages
Version: 0.0.4.005
Version: 0.0.4.006
Authors@R:
person("Mark", "Padgham", , "[email protected]", role = c("aut", "cre"),
comment = c(ORCID = "0000-0003-2172-5265"))
Expand All @@ -20,7 +20,9 @@ Imports:
sys,
withr
Suggests:
curl,
hms,
jsonlite,
knitr,
parallel,
pkgbuild,
Expand Down
1 change: 1 addition & 0 deletions NAMESPACE
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
export(ctags_install)
export(ctags_test)
export(desc_stats)
export(dl_pkgstats_data)
export(extract_tarball)
export(loc_stats)
export(pkgstats)
Expand Down
56 changes: 56 additions & 0 deletions R/pkgstats-data-download.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
#' Download latest version of 'pkgstats' data
#'
#' @param current If 'FALSE', download data for all CRAN packages ever released,
#' otherwise (default) download data only for current CRAN packages.
#' @param path Local path to download file.
#' @param quiet If `FALSE`, display progress information on screen.
#' @return (Invisibly) A `data.frame` of `pkgstats` results, one row for each
#' package.
#' @family archive
#' @export
dl_pkgstats_data <- function (current = TRUE,
path = tempdir (),
quiet = FALSE) {

requireNamespace ("curl")
requireNamespace ("jsonlite")

u <- paste0 (
"https://api.github.com/repos/",
"ropensci-review-tools/pkgstats/",
"releases/latest"
)

res <- curl::curl_fetch_memory (u)
hdrs <- curl::parse_headers (res$headers)
http_code <- as.integer (gsub (
"^http\\/[0-9]\\s?|\\s+$",
"",
hdrs [1],
ignore.case = TRUE
))
if (http_code != 200L) {
stop (
"Call to GitHub failed with http error code [",
http_code, "]"
)
}

res <- jsonlite::fromJSON (rawToChar (res$content))
assets <- res$assets

i <- ifelse (
current,
grep ("current", assets$name, ignore.case = TRUE),
grep ("all", assets$name, ignore.case = TRUE)
)
dl_url <- assets$browser_download_url [i]
f <- file.path (path, basename (dl_url))

curl::curl_download (url = dl_url, destfile = f, quiet = quiet)
if (!quiet) {
message ("Downloaded to [", f, "]")
}

invisible (readRDS (f))
}
2 changes: 1 addition & 1 deletion codemeta.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
"codeRepository": "https://github.com/ropensci-review-tools/pkgstats",
"issueTracker": "https://github.com/ropensci-review-tools/pkgstats/issues",
"license": "https://spdx.org/licenses/GPL-3.0",
"version": "0.0.4.005",
"version": "0.0.4.006",
"programmingLanguage": {
"@type": "ComputerLanguage",
"name": "R",
Expand Down
28 changes: 28 additions & 0 deletions man/dl_pkgstats_data.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 4 additions & 0 deletions man/pkgstats_from_archive.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit d2bb5ad

Please sign in to comment.