Permalink
Browse files

Pkg name is a column in result, query multiple pkgs, #4

  • Loading branch information...
1 parent 442c7a4 commit e639866c1272d647d27a54e118a06be72678e18c @gaborcsardi gaborcsardi committed Mar 11, 2015
Showing with 57 additions and 32 deletions.
  1. +44 −19 R/cranlogs.R
  2. +13 −13 man/cran_downloads.Rd
View
@@ -12,22 +12,19 @@ url <- "http://cranlogs.r-pkg.org/downloads/daily/"
#' Daily package downloads from the RStudio CRAN mirror
#'
-#' @param package A package to query, or \code{NULL} for a sum of
-#' downloads for all packages.
+#' @param packages A character vector, the packages to query,
+#' or \code{NULL} for a sum of downloads for all 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{yesterday}. It is ignored if \code{when} is given.
#' @param to End date, in \code{yyyy-mm-dd} format, or
#' \code{yesterday}. It is ignored if \code{when} is given.
-#' @return A list with entries: \itemize{
-#' \item \code{downloads} The downloads, in a two-column data frame:
-#' date and download count.
-#' \item \code{start} Start date.
-#' \item \code{end} End date.
-#' \item \code{package} The queried package. This entry is missing
-#' if the total number of downloads for all packages was queried.
-#' }
+#' @return 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.
+#' \item \code{count} Download count.
#'
#' @export
#' @examples
@@ -36,15 +33,18 @@ url <- "http://cranlogs.r-pkg.org/downloads/daily/"
#' cran_downloads()
#'
#' ## All downloads for 'dplyr' yesterday
-#' cran_downloads(package = "dplyr")
+#' cran_downloads(packages = "dplyr")
#'
#' ## Daily downloads for 'igraph' last week
-#' cran_downloads(package = "igraph", when = "last-week")
+#' cran_downloads(packages = "igraph", when = "last-week")
#'
#' ## Downloads in the specified time interval
#' cran_downloads(from = "2014-06-30", to = "2014-08-08")
+#'
+#' ## Multiple packages
+#' cran_downloads(packages = c("ggplot2", "plyr", "dplyr"))
-cran_downloads <- function(package = NULL,
+cran_downloads <- function(packages = NULL,
when = c("last-day", "last-week", "last-month"),
from = "last-day", to = "last-day") {
@@ -58,14 +58,39 @@ cran_downloads <- function(package = NULL,
}
}
- if (is.null(package)) {
- package <- ""
+ if (is.null(packages)) {
+ packages <- ""
} else {
- package <- paste0("/", package)
+ packages <- paste(packages, collapse = ",")
+ packages <- paste0("/", packages)
}
- r <- fromJSON(content(GET(paste0(url, interval, package)), as = "text"))
- class(r) <- "cranlogs"
- r
+ r <- fromJSON(content(GET(paste0(url, interval, packages)), as = "text"),
+ simplifyVector = FALSE)
+
+ to_df(r)
+
+}
+to_df <- function(res) {
+ if (length(res) == 1 && is.null(res[[1]]$package)) {
+ data.frame(
+ stringsAsFactors = FALSE,
+ date = sapply(res[[1]]$downloads, "[[", "day"),
+ count = sapply(res[[1]]$downloads, "[[", "downloads")
+ )
+
+ } else {
+ do.call(
+ rbind,
+ lapply(res, function(x) {
+ data.frame(
+ stringsAsFactors = FALSE,
+ package = x$package,
+ date = sapply(x$downloads, "[[", "day"),
+ count = sapply(x$downloads, "[[", "downloads")
+ )
+ })
+ )
+ }
}
View
@@ -3,12 +3,12 @@
\alias{cran_downloads}
\title{Daily package downloads from the RStudio CRAN mirror}
\usage{
-cran_downloads(package = NULL, when = c("last-day", "last-week",
+cran_downloads(packages = NULL, when = c("last-day", "last-week",
"last-month"), from = "last-day", to = "last-day")
}
\arguments{
-\item{package}{A package to query, or \code{NULL} for a sum of
-downloads for all packages.}
+\item{packages}{A character vector, the packages to query,
+or \code{NULL} for a sum of downloads for all 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.}
@@ -20,14 +20,11 @@ If this is given, then \code{from} and \code{to} are ignored.}
\code{yesterday}. It is ignored if \code{when} is given.}
}
\value{
-A list with entries: \itemize{
- \item \code{downloads} The downloads, in a two-column data frame:
- date and download count.
- \item \code{start} Start date.
- \item \code{end} End date.
- \item \code{package} The queried package. This entry is missing
- if the total number of downloads for all packages was queried.
-}
+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.
+ \item \code{count} Download count.
}
\description{
Daily package downloads from the RStudio CRAN mirror
@@ -37,12 +34,15 @@ Daily package downloads from the RStudio CRAN mirror
cran_downloads()
## All downloads for 'dplyr' yesterday
-cran_downloads(package = "dplyr")
+cran_downloads(packages = "dplyr")
## Daily downloads for 'igraph' last week
-cran_downloads(package = "igraph", when = "last-week")
+cran_downloads(packages = "igraph", when = "last-week")
## Downloads in the specified time interval
cran_downloads(from = "2014-06-30", to = "2014-08-08")
+
+## Multiple packages
+cran_downloads(packages = c("ggplot2", "plyr", "dplyr"))
}

0 comments on commit e639866

Please sign in to comment.