Skip to content

Commit

Permalink
Add package_downloads() utility function
Browse files Browse the repository at this point in the history
  • Loading branch information
jabenninghoff committed Jun 5, 2024
1 parent fef95cc commit 61bc0ab
Show file tree
Hide file tree
Showing 8 changed files with 81 additions and 0 deletions.
1 change: 1 addition & 0 deletions DESCRIPTION
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ BugReports: https://github.com/jabenninghoff/rdev/issues
Imports:
checkmate,
cli,
cranlogs,
curl,
desc,
devtools,
Expand Down
1 change: 1 addition & 0 deletions NAMESPACE
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ export(merge_release)
export(missing_deps)
export(new_branch)
export(open_files)
export(package_downloads)
export(rmd_metadata)
export(setup_analysis)
export(setup_rdev)
Expand Down
4 changes: 4 additions & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
# rdev 1.11.0

* Added function `package_downloads()`: A wrapper for `cranlogs::cran_downloads()` that summarizes the number of package downloads from the RStudio CRAN mirror

# rdev 1.10.11

* Minor updates
Expand Down
26 changes: 26 additions & 0 deletions R/utils.R
Original file line number Diff line number Diff line change
Expand Up @@ -216,3 +216,29 @@ open_files <- function(files = c("TODO.md", "NEWS.md", "README.Rmd", "DESCRIPTIO
writeLines(paste0("Opening files: ", toString(files)))
invisible(vapply(files, rstudioapi::navigateToFile, character(1)))
}

#' Summarize package downloads
#'
#' A wrapper for [cranlogs::cran_downloads()] that summarizes the number of package downloads from
#' the RStudio CRAN mirror.
#'
#' By default, the summary is for the last month.
#'
#' @param packages A character vector of the packages to query.
#' @param when The period to summarize, one of `last-day`, `last-week` or `last-month`
#' (the default).
#'
#' @return A data frame containing the total number of downloads by package for the specified
#' period, sorted by popularity.
#' @export
package_downloads <- function(packages, when = "last-month") {
checkmate::assert_character(packages, min.chars = 1)
if ("R" %in% packages) {
stop("Querying downloads of R is not supported!")
}

df <- cranlogs::cran_downloads(packages = packages, when = when) |>
stats::aggregate(by = count ~ package, FUN = sum)

df[order(df$count, decreasing = TRUE), ]
}
1 change: 1 addition & 0 deletions _pkgdown.yml
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@ reference:
- extra_deps
- urlchecker-reexports
- html_url_check
- package_downloads
- title: rdev Package
desc: >
rdev package documentation
Expand Down
25 changes: 25 additions & 0 deletions man/package_downloads.Rd

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

11 changes: 11 additions & 0 deletions renv.lock
Original file line number Diff line number Diff line change
Expand Up @@ -274,6 +274,17 @@
],
"Hash": "5a295d7d963cc5035284dcdbaf334f4e"
},
"cranlogs": {
"Package": "cranlogs",
"Version": "2.1.1",
"Source": "Repository",
"Repository": "CRAN",
"Requirements": [
"httr",
"jsonlite"
],
"Hash": "cfa4eec97df94fd69cb8652368966020"
},
"crayon": {
"Package": "crayon",
"Version": "1.5.2",
Expand Down
12 changes: 12 additions & 0 deletions tests/testthat/test-utils.R
Original file line number Diff line number Diff line change
Expand Up @@ -215,3 +215,15 @@ test_that("open_files opens all files", {
)
expect_named(ret, files)
})

# package_downloads

test_that("package_downloads validates arguments", {
mockery::stub(package_downloads, "cranlogs::cran_downloads", NULL)

expect_error(package_downloads(NULL), "'packages'")
expect_error(package_downloads(""), "'packages'")
expect_error(package_downloads(c("checkmate", "")), "'packages'")
expect_error(package_downloads("R"), "Querying downloads of R is not supported!")
expect_error(package_downloads(c("checkmate", "R")), "Querying downloads of R is not supported!")
})

0 comments on commit 61bc0ab

Please sign in to comment.