This report shows downloads of the CRAN packages: r params$packages

testrmd::init(theme = "emoji")
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)
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)
zero_rows <- weekly_downloads %>%
  filter(count == 0)
expect_equal(nrow(zero_rows), 0)

Downloads by week

week_plot <- ggplot(weekly_downloads, aes(week, count, color = package)) +
  geom_line()
plotly::ggplotly(week_plot)

Total downloads

daily_downloads %>%
  group_by(package) %>%
  summarise(count = sum(count))


ropenscilabs/testrmd documentation built on May 30, 2019, 10:35 p.m.