Permalink
Browse files

Top downloaded packages, fixes #8

  • Loading branch information...
1 parent 1b573f5 commit a4ee9e56b643d5b26c80c91f2c39de8f55bb5da1 @gaborcsardi gaborcsardi committed May 7, 2015
Showing with 78 additions and 2 deletions.
  1. +1 −0 NAMESPACE
  2. +44 −2 R/cranlogs.R
  3. +3 −0 man/cran_downloads.Rd
  4. +30 −0 man/cran_top_downloads.Rd
View
@@ -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)
View
@@ -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
+}
View
@@ -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}}
+}
View
@@ -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}}
+}
+

0 comments on commit a4ee9e5

Please sign in to comment.