💩 Warning! This document contains 1 failing test.

This report shows downloads of the CRAN packages: assertr, assertthat, ensurer, assertive, checkmate, validate, breakfast

library(knitr)
library(dplyr)
library(ggplot2)
library(cranlogs)
library(lubridate)

packages <- strsplit(params$packages, ",\\s*")[[1]]
library(testthat)

expect_true(params$start < params$end)
expect_gt(length(packages), 0)

Fetch and summarize

The data comes from the cranlogs package, which uses the logs from RStudio’s CRAN mirrors.

daily_downloads <- cran_downloads(packages, from = params$start, to = params$end)
head(daily_downloads)
##         date count package
## 1 2016-01-01     7 assertr
## 2 2016-01-02     9 assertr
## 3 2016-01-03     2 assertr
## 4 2016-01-04    15 assertr
## 5 2016-01-05    14 assertr
## 6 2016-01-06    13 assertr
expect_equal(colnames(daily_downloads), c("date", "count", "package"))
expect_gt(nrow(daily_downloads), 0)
expect_is(daily_downloads[["date"]], "Date")
expect_is(daily_downloads[["count"]], "numeric")
expect_is(daily_downloads[["package"]], "character")

for (pkg in packages) {
  package_daily_downloads <- daily_downloads %>%
    filter(package == pkg) %>%
    arrange(date)
  
  # Make sure no days are missing
  expected_range <- params$start:params$end
  expect_equal(
    as.integer(as.Date(package_daily_downloads$date)),
    expected_range,
    info = pkg
  )
}

In addition to daily downloads, we’ll calculate weekly downloads. We’ll use lubridate’s floor_date to add a new week column to the data frame, then group and sum.

weekly_downloads <- daily_downloads %>%
  mutate(week = floor_date(date, "week")) %>%
  # Discard partial weeks
  filter(week >= ceiling_date(params$start, "week") & week < floor_date(Sys.time(), "week")) %>%
  group_by(package, week) %>%
  summarise(count = sum(count))
head(weekly_downloads)
## Source: local data frame [6 x 3]
## Groups: package [1]
## 
##     package       week count
##       <chr>     <date> <dbl>
## 1 assertive 2016-01-03   125
## 2 assertive 2016-01-10   138
## 3 assertive 2016-01-17   170
## 4 assertive 2016-01-24   192
## 5 assertive 2016-01-31   118
## 6 assertive 2016-02-07   133
zero_rows <- weekly_downloads %>%
  filter(count == 0)
expect_equal(nrow(zero_rows), 0, info = unique(zero_rows$package))
## Error: nrow(zero_rows) not equal to 0.
## 1/1 mismatches
## [1] 72 - 0 == 72
## breakfast

Downloads by week

week_plot <- ggplot(weekly_downloads, aes(week, count, color = package)) +
  geom_line()
plotly::ggplotly(week_plot)
## Warning: We recommend that you use the dev version of ggplot2 with `ggplotly()`
## Install it with: `devtools::install_github('hadley/ggplot2')`

Total downloads

daily_downloads %>%
  group_by(package) %>%
  summarise(count = sum(count))
## # A tibble: 7 × 2
##      package   count
##        <chr>   <dbl>
## 1  assertive   19108
## 2    assertr    9325
## 3 assertthat 2824490
## 4  breakfast       0
## 5  checkmate  518520
## 6    ensurer    4844
## 7   validate    6216