Permalink
Browse files

Support for R downloads

  • Loading branch information...
1 parent d66f505 commit 62d3f014469e924154c167ce8aa854cb28168a3e @gaborcsardi gaborcsardi committed Jul 27, 2015
Showing with 42 additions and 10 deletions.
  1. +32 −8 R/cranlogs.R
  2. +10 −2 man/cran_downloads.Rd
View
@@ -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")
View
@@ -9,7 +9,9 @@ cran_downloads(packages = NULL, when = c("last-day", "last-week",
}
\arguments{
\item{packages}{A character vector, the packages to query,
-or \code{NULL} for a sum of downloads for all packages.}
+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.}
\item{when}{\code{last_day}, \code{last_week} or \code{last_month}.
If this is given, then \code{from} and \code{to} are ignored.}
@@ -21,11 +23,14 @@ If this is given, then \code{from} and \code{to} are ignored.}
\code{last-day}. It is ignored if \code{when} is given.}
}
\value{
-A data frame with columns:
+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}).
}
\description{
Daily package downloads from the RStudio CRAN mirror
@@ -45,6 +50,9 @@ cran_downloads(from = "2014-06-30", to = "2014-08-08")
## Multiple packages
cran_downloads(packages = c("ggplot2", "plyr", "dplyr"))
+
+## R downloads
+cran_downloads("R")
}
\seealso{
Other CRAN.downloads: \code{\link{cran_top_downloads}}

0 comments on commit 62d3f01

Please sign in to comment.