|
|
@@ -16,18 +16,23 @@ top_url <- paste0(base_url, "top/") |
|
|
#'
|
|
|
#' @param packages A character vector, the packages to query,
|
|
|
#' or \code{NULL} for a sum of downloads for all packages.
|
|
|
+#' Alternatively, it can also be \code{"R"}, to query downloads
|
|
|
+#' of R itself. \code{"R"} cannot be mixed with packages.
|
|
|
#' @param when \code{last_day}, \code{last_week} or \code{last_month}.
|
|
|
#' If this is given, then \code{from} and \code{to} are ignored.
|
|
|
#' @param from Start date, in \code{yyyy-mm-dd} format, or
|
|
|
#' \code{last-day}. It is ignored if \code{when} is given.
|
|
|
#' @param to End date, in \code{yyyy-mm-dd} format, or
|
|
|
#' \code{last-day}. It is ignored if \code{when} is given.
|
|
|
-#' @return A data frame with columns:
|
|
|
+#' @return For packages a data frame with columns:
|
|
|
#' \item{\code{package}}{The package. This column is missing if
|
|
|
#' all packages were queried.}
|
|
|
#' \item{\code{date}}{Day of the downloads, it is a Date object.}
|
|
|
#' \item{\code{count}}{Download count.}
|
|
|
#'
|
|
|
+#' For downloads of R, there are also columns for the operating
|
|
|
+#' system (\code{os}) and the R version (\code{version}).
|
|
|
+#'
|
|
|
#' @family CRAN downloads
|
|
|
#' @export
|
|
|
#' @examples
|
|
|
@@ -46,6 +51,9 @@ top_url <- paste0(base_url, "top/") |
|
|
#'
|
|
|
#' ## Multiple packages
|
|
|
#' cran_downloads(packages = c("ggplot2", "plyr", "dplyr"))
|
|
|
+#'
|
|
|
+#' ## R downloads
|
|
|
+#' cran_downloads("R")
|
|
|
|
|
|
cran_downloads <- function(packages = NULL,
|
|
|
when = c("last-day", "last-week", "last-month"),
|
|
|
@@ -62,25 +70,30 @@ cran_downloads <- function(packages = NULL, |
|
|
}
|
|
|
|
|
|
if (is.null(packages)) {
|
|
|
- packages <- ""
|
|
|
+ ppackages <- ""
|
|
|
} else {
|
|
|
- packages <- paste(packages, collapse = ",")
|
|
|
- packages <- paste0("/", packages)
|
|
|
+ if ("R" %in% packages && any(packages != "R")) {
|
|
|
+ stop("R downloads cannot be mixed with package downloads")
|
|
|
+ }
|
|
|
+ ppackages <- paste(packages, collapse = ",")
|
|
|
+ ppackages <- paste0("/", ppackages)
|
|
|
}
|
|
|
|
|
|
- r <- fromJSON(content(GET(paste0(daily_url, interval, packages)), as = "text"),
|
|
|
+ r <- fromJSON(content(GET(paste0(daily_url, interval, ppackages)), as = "text"),
|
|
|
simplifyVector = FALSE)
|
|
|
|
|
|
if ("error" %in% names(r) && r$error == "Invalid query") {
|
|
|
stop("Invalid query, probably invalid dates")
|
|
|
}
|
|
|
|
|
|
- to_df(r)
|
|
|
+ to_df(r, packages)
|
|
|
|
|
|
}
|
|
|
|
|
|
-to_df <- function(res) {
|
|
|
- if (length(res) == 1 && is.null(res[[1]]$package)) {
|
|
|
+to_df <- function(res, packages) {
|
|
|
+ if (length(res) == 1 && identical(toupper(packages), "R")) {
|
|
|
+ to_df_r(res[[1]])
|
|
|
+ } else if (length(res) == 1 && is.null(res[[1]]$package)) {
|
|
|
to_df_1(res[[1]])
|
|
|
} else {
|
|
|
dfs <- lapply(res, to_df_1)
|
|
|
@@ -98,6 +111,17 @@ to_df_1 <- function(res1) { |
|
|
fill_in_dates(df, as.Date(res1$start), as.Date(res1$end))
|
|
|
}
|
|
|
|
|
|
+to_df_r <- function(res1) {
|
|
|
+ df <- data.frame(
|
|
|
+ stringsAsFactors = FALSE,
|
|
|
+ date = as.Date(vapply(res1$downloads, "[[", "", "day")),
|
|
|
+ version = vapply(res1$downloads, "[[", "", "version"),
|
|
|
+ os = vapply(res1$downloads, "[[", "", "os"),
|
|
|
+ count = vapply(res1$downloads, "[[", 1, "downloads")
|
|
|
+ )
|
|
|
+ fill_in_dates(df, as.Date(res1$start), as.Date(res1$end))
|
|
|
+}
|
|
|
+
|
|
|
fill_in_dates <- function(df, start, end) {
|
|
|
if (start > end) stop("Empty time interval")
|
|
|
if (end > Sys.Date()) warning("Time interval in the future")
|
|
|
|
0 comments on commit
62d3f01