-
Notifications
You must be signed in to change notification settings - Fork 26
/
packages.Rmd
206 lines (160 loc) · 6.26 KB
/
packages.Rmd
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
# An analysis of R package download trends {#packages}
```{r, message = FALSE, warning = FALSE, include = FALSE}
knitr::opts_knit$set(root.dir = fs::dir_create(tempfile()))
knitr::opts_chunk$set(collapse = TRUE, comment = "#>")
options(
drake_make_menu = FALSE,
drake_clean_menu = FALSE,
warnPartialMatchArgs = FALSE,
crayon.enabled = FALSE,
readr.show_progress = FALSE,
tidyverse.quiet = TRUE
)
```
```{r, message = FALSE, warning = FALSE, include = FALSE}
library(cranlogs)
library(curl)
library(drake)
library(dplyr)
library(ggplot2)
library(httr)
library(knitr)
library(magrittr)
library(R.utils)
library(rvest)
invisible(drake_example("packages", overwrite = TRUE))
invisible(file.copy("packages/report.Rmd", ".", overwrite = TRUE))
```
This chapter explores R package download trends using the `cranlogs` package, and it shows how `drake`'s custom triggers can help with workflows with remote data sources.
## Get the code.
Write the code files to your workspace.
```{r, eval = FALSE}
drake_example("packages")
```
The new `packages` folder now includes a file structure of a serious `drake` project, plus an `interactive-tutorial.R` to narrate the example. The code is also [online here](https://github.com/wlandau/drake-examples/tree/main/packages).
## Overview
This small data analysis project explores some trends in R package downloads over time. The datasets are downloaded using the [cranlogs package](https://github.com/metacran/cranlogs).
```{r}
library(cranlogs)
cran_downloads(packages = "dplyr", when = "last-week")
```
Above, each count is the number of times `dplyr` was downloaded from the RStudio CRAN mirror on the given day. To stay up to date with the latest download statistics, we need to refresh the data frequently. With `drake`, we can bring all our work up to date without restarting everything from scratch.
## Analysis
First, we load the required packages. `drake` detects the packages you install and load.
```{r}
library(cranlogs)
library(drake)
library(dplyr)
library(ggplot2)
library(knitr)
library(rvest)
```
We will want custom functions to summarize the CRAN logs we download.
```{r}
make_my_table <- function(downloads){
group_by(downloads, package) %>%
summarize(mean_downloads = mean(count))
}
make_my_plot <- function(downloads){
ggplot(downloads) +
geom_line(aes(x = date, y = count, group = package, color = package))
}
```
Next, we generate the plan.
We want to explore the daily downloads from the `knitr`, `Rcpp`, and `ggplot2` packages. We will use the [`cranlogs` package](https://github.com/metacran/cranlogs) to get daily logs of package downloads from RStudio's CRAN mirror. In our `drake_plan()`, we declare targets `older` and `recent` to kcontain snapshots of the logs.
The following `drake_plan()` syntax is described [here](https://books.ropensci.org/drake/static.html), which is supported in `drake` 7.0.0 and above.
```{r}
plan <- drake_plan(
older = cran_downloads(
packages = c("knitr", "Rcpp", "ggplot2"),
from = "2016-11-01",
to = "2016-12-01"
),
recent = target(
command = cran_downloads(
packages = c("knitr", "Rcpp", "ggplot2"),
when = "last-month"
),
trigger = trigger(change = latest_log_date())
),
averages = target(
make_my_table(data),
transform = map(data = c(older, recent))
),
plot = target(
make_my_plot(data),
transform = map(data)
),
report = knit(
knitr_in("report.Rmd"),
file_out("report.md"),
quiet = TRUE
)
)
```
Notice the custom trigger for the target `recent`. Here, we are telling `drake` to rebuild `recent` whenever a new day's log is uploaded to [http://cran-logs.rstudio.com](http://cran-logs.rstudio.com). In other words, `drake` keeps track of the return value of `latest_log_date()` and recomputes `recent` (during `make()`) if that value changed since the last `make()`. Here, `latest_log_date()` is one of our custom imported functions. We use it to scrape [http://cran-logs.rstudio.com](http://cran-logs.rstudio.com) using the [`rvest`](https://github.com/hadley/rvest) package.
```{r}
latest_log_date <- function(){
read_html("http://cran-logs.rstudio.com/") %>%
html_nodes("li:last-of-type") %>%
html_nodes("a:last-of-type") %>%
html_text() %>%
max
}
```
Now, we run the project to download the data and analyze it.
The results will be summarized in the knitted report, `report.md`,
but you can also read the results directly from the cache.
```{r, fig.width = 7, fig.height = 4}
make(plan)
readd(averages_recent)
readd(averages_older)
readd(plot_recent)
readd(plot_older)
```
If we run `make()` again right away, we see that everything is up to date. But if we wait until a new day's log is uploaded, `make()` will update `recent` and everything that depends on it.
```{r}
make(plan)
```
To visualize the build behavior, you can plot the dependency network.
```{r}
vis_drake_graph(plan)
```
## Other ways to trigger downloads
Sometimes, our remote data sources get revised, and web scraping may not be the best way to detect changes. We may want to look at our remote dataset's modification time or HTTP ETag. To see how this works, consider the CRAN log file from February 9, 2018.
```{r}
url <- "http://cran-logs.rstudio.com/2018/2018-02-09-r.csv.gz"
```
We can track the modification date using the [`httr`](https://github.com/r-lib/httr) package.
```{r}
library(httr) # For querying websites.
HEAD(url)$headers[["last-modified"]]
```
In our `drake` plan, we can track this timestamp and trigger a download whenever it changes.
```{r}
plan <- drake_plan(
logs = target(
get_logs(url),
trigger = trigger(change = HEAD(url)$headers[["last-modified"]])
)
)
plan
```
where
```{r}
library(R.utils) # For unzipping the files we download.
library(curl) # For downloading data.
get_logs <- function(url){
curl_download(url, "logs.csv.gz") # Get a big file.
gunzip("logs.csv.gz", overwrite = TRUE) # Unzip it.
out <- read.csv("logs.csv", nrows = 4) # Extract the data you need.
unlink(c("logs.csv.gz", "logs.csv")) # Remove the big files
out # Value of the target.
}
```
When we are ready, we run the workflow.
```{r}
make(plan)
readd(logs)
```
If the log file at the `url` ever changes, the timestamp will update remotely, and `make()` will download the file again.