diff --git a/NAMESPACE b/NAMESPACE index 631bc42..460e6d9 100644 --- a/NAMESPACE +++ b/NAMESPACE @@ -1,6 +1,7 @@ # Generated by roxygen2 (4.0.2): do not edit by hand export(cran_downloads) +export(cran_top_downloads) importFrom(httr,GET) importFrom(httr,content) importFrom(jsonlite,fromJSON) diff --git a/R/cranlogs.R b/R/cranlogs.R index d083b7e..7555fce 100644 --- a/R/cranlogs.R +++ b/R/cranlogs.R @@ -8,7 +8,9 @@ #' @importFrom jsonlite fromJSON NULL -url <- "http://cranlogs.r-pkg.org/downloads/daily/" +base_url <- "http://cranlogs.r-pkg.org/" +daily_url <- paste0(base_url, "downloads/daily/") +top_url <- paste0(base_url, "top/") #' Daily package downloads from the RStudio CRAN mirror #' @@ -26,6 +28,7 @@ url <- "http://cranlogs.r-pkg.org/downloads/daily/" #' \item \code{date} Day of the downloads, it is a Date object. #' \item \code{count} Download count. #' +#' @family CRAN downloads #' @export #' @examples #' @@ -65,7 +68,7 @@ cran_downloads <- function(packages = NULL, packages <- paste0("/", packages) } - r <- fromJSON(content(GET(paste0(url, interval, packages)), as = "text"), + r <- fromJSON(content(GET(paste0(daily_url, interval, packages)), as = "text"), simplifyVector = FALSE) if ("error" %in% names(r) && r$error == "Invalid query") { @@ -111,3 +114,42 @@ fill_in_dates <- function(df, start, end) { } df } + +#' Top downloaded packages from the RStudio CRAN mirror +#' +#' @param when \code{last_day}, \code{last_week} or \code{last_month}. +#' @return A data frame with columns: \code{rank}, \code{package}, +#' \code{count}, \code{from}, \code{to}. +#' +#' @family CRAN downloads +#' @export +#' @examples +#' +#' ## Default is last day +#' cran_top_downloads() +#' +#' ## Last week instead +#'cran_top_downloads(when = "last-week") + +cran_top_downloads <- function(when = c("last-day", "last-week", + "last-month"), count = 10) { + + when <- match.arg(when) + r <- fromJSON(content(GET(paste0(top_url, when, '/', count)), as = "text"), + simplifyVector = FALSE) + + df <- data.frame( + stringsAsFactors = FALSE, + rank = seq_along(r$downloads), + package = vapply(r$downloads, "[[", "", "package"), + count = as.integer(vapply(r$downloads, "[[", "", "downloads")), + from = as.Date(r$start), + to = as.Date(r$end) + ) + + if (nrow(df) != count) { + warning("Requested ", count, " packages, returned only ", nrow(df)) + } + + df +} diff --git a/man/cran_downloads.Rd b/man/cran_downloads.Rd index 558995a..d34b843 100644 --- a/man/cran_downloads.Rd +++ b/man/cran_downloads.Rd @@ -45,4 +45,7 @@ cran_downloads(from = "2014-06-30", to = "2014-08-08") ## Multiple packages cran_downloads(packages = c("ggplot2", "plyr", "dplyr")) } +\seealso{ +Other CRAN.downloads: \code{\link{cran_top_downloads}} +} diff --git a/man/cran_top_downloads.Rd b/man/cran_top_downloads.Rd new file mode 100644 index 0000000..64d3ce3 --- /dev/null +++ b/man/cran_top_downloads.Rd @@ -0,0 +1,30 @@ +% Generated by roxygen2 (4.1.1): do not edit by hand +% Please edit documentation in R/cranlogs.R +\name{cran_top_downloads} +\alias{cran_top_downloads} +\title{Top downloaded packages from the RStudio CRAN mirror} +\usage{ +cran_top_downloads(when = c("last-day", "last-week", "last-month"), + count = 10) +} +\arguments{ +\item{when}{\code{last_day}, \code{last_week} or \code{last_month}.} +} +\value{ +A data frame with columns: \code{rank}, \code{package}, + \code{count}, \code{from}, \code{to}. +} +\description{ +Top downloaded packages from the RStudio CRAN mirror +} +\examples{ +## Default is last day +cran_top_downloads() + +## Last week instead +cran_top_downloads(when = "last-week") +} +\seealso{ +Other CRAN.downloads: \code{\link{cran_downloads}} +} +