forked from carpentries-incubator/targets-workshop
-
Notifications
You must be signed in to change notification settings - Fork 3
/
lifecycle.Rmd
183 lines (145 loc) · 4.56 KB
/
lifecycle.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
---
title: 'The Workflow Lifecycle'
teaching: 10
exercises: 2
---
:::::::::::::::::::::::::::::::::::::: questions
- What happens if we re-run a workflow?
- How does `targets` know what steps to re-run?
- How can I override default settings?
::::::::::::::::::::::::::::::::::::::::::::::::
::::::::::::::::::::::::::::::::::::: objectives
- Explain how `targets` helps increase efficiency
- Be able to inspect a workflow to see what parts are outdated
- Be able to run only a specific part of the workflow
::::::::::::::::::::::::::::::::::::::::::::::::
```{r}
#| label: setup
#| echo: FALSE
#| message: FALSE
#| warning: FALSE
library(targets)
library(visNetwork)
```
## Re-running the workflow
One of the features of `targets` is that it maximizes efficiency by only running the parts of the workflow that need to be run.
This is easiest to understand by trying it yourself. Let's try running the workflow again:
```{r}
#| label: targets-run-show
#| eval: FALSE
tar_make()
```
```{r}
#| label: targets-run-hide
#| echo: FALSE
tar_dir({
tar_script({
summ <- function(dataset) {
summarize(dataset, mean_x = mean(x))
}
tar_option_set(packages = "dplyr")
list(
tar_target(my_data, data.frame(x = sample.int(100), y = sample.int(100))),
tar_target(my_summary, summ(my_data))
)
})
tar_make(reporter = "silent")
tar_make()
})
```
Remember how the first time we ran the pipeline, `targets` printed out a list of each target as it was being built?
This time, it tells us it is skipping those targets; they have already been built, so there's no need to run that code again.
Remember, the fastest code is the code you don't have to run!
## Re-running the workflow after modification
What happens when we change one part of the workflow then run it again?
Let's modify the workflow to calculate the mean of both `x` and `y`.
Edit `_targets.R` so that the `summ()` function looks like this:
```{r}
#| label: new-func
#| eval: FALSE
summ <- function(dataset) {
summarize(dataset, mean_x = mean(x), mean_y = mean(y))
}
```
Then run it again.
```{r}
#| label: targets-run-show-2
#| eval: FALSE
tar_make()
```
```{r}
#| label: targets-run-hide-2
#| echo: FALSE
tar_dir({
# Original workflow
tar_script({
summ <- function(dataset) {
summarize(dataset, mean_x = mean(x))
}
tar_option_set(packages = "dplyr")
list(
tar_target(my_data, data.frame(x = sample.int(100), y = sample.int(100))),
tar_target(my_summary, summ(my_data))
)
})
# Run it silently
tar_make(reporter = "silent")
# New workflow
tar_script({
summ <- function(dataset) {
summarize(dataset, mean_x = mean(x), mean_y = mean(y))
}
tar_option_set(packages = "dplyr")
list(
tar_target(my_data, data.frame(x = sample.int(100), y = sample.int(100))),
tar_target(my_summary, summ(my_data))
)
})
# Run it again
tar_make()
})
```
What happened?
This time, it skipped `my_data` and only ran `my_summary`.
Of course, since our example workflow is so short we don't even notice the amount of time saved.
But imagine using this in a series of computationally intensive analysis steps.
The ability to automatically skip steps results in a massive increase in efficiency.
## Visualizing the workflow
Typically, you will be making edits to various places in your code, adding new targets, and running the workflow periodically.
It is good to be able to visualize the state of the workflow.
This can be done with `tar_visnetwork()`
```{r}
#| label: targets-run-show-3
#| eval: FALSE
tar_visnetwork()
```
```{r}
#| label: targets-run-hide-3
#| echo: FALSE
tar_dir({
# New workflow
tar_script({
summ <- function(dataset) {
summarize(dataset, mean_x = mean(x), mean_y = mean(y))
}
tar_option_set(packages = "dplyr")
list(
tar_target(my_data, data.frame(x = sample.int(100), y = sample.int(100))),
tar_target(my_summary, summ(my_data))
)
})
# Run it again
tar_make(reporter = "silent")
tar_visnetwork()
})
```
::::::::::::::::::::::::::::::::::::: caution
You may encounter an error message `The package "visNetwork" is required.`
In this case, install it first with `install.packages("visNetwork")`.
::::::::::::::::::::::::::::::::::::::::::::::::
::::::::::::::::::::::::::::::::::::: keypoints
- `targets` only runs the steps that have been affected by a change to the code
- `tar_visnetwork()` shows the current state of the workflow as a network
- `tar_progress()` shows the current state of the workflow as a data frame
- `tar_outdated()` lists outdated targets that will be built in the next `tar_make()`
::::::::::::::::::::::::::::::::::::::::::::::::