-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.html
351 lines (305 loc) · 12 KB
/
index.html
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
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml" lang="" xml:lang="">
<head>
<title>The Promise and Perils of Packaging Your Processes</title>
<meta charset="utf-8" />
<meta name="author" content="Karl Hailperin khailper @khailper khailper@gmail" />
<link href="libs/remark-css-0.0.1/default.css" rel="stylesheet" />
<link href="libs/remark-css-0.0.1/default-fonts.css" rel="stylesheet" />
<link rel="stylesheet" href="footer.css" type="text/css" />
</head>
<body>
<textarea id="source">
class: center, middle, inverse, title-slide
# The Promise and Perils of Packaging Your Processes
## (but mostly promise)
### Karl Hailperin <br> <a href="http://github.com/khailper"><i class="fa fa-github"></i> khailper</a><br> <a href="http://twitter.com/khailper"> <i class="fa fa-twitter"></i> <span class="citation">@khailper</span></a><br> <a href="mailto:khailper@gmail.com"> <i class="fa fa-paper-plane fa-fw"></i> khailper@gmail</a><br>
### 2018/06/20
---
layout: true
<div class="my-footer"><span>khailper.github.io/process_packaging_pres</span></div>
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/4.7.0/css/font-awesome.min.css">
<!-- reusing footer code from https://github.com/rstudio-education/arm-workshop-rsc2019/blob/master/static/slides/xaringan.Rmd, https://github.com/rstudio-education/arm-workshop-rsc2019/blob/master/static/slides/css/sfah.css, and https://github.com/yihui/xaringan/wiki/Footer-and-header-lines stylesheet code from https://holtzy.github.io/Pimp-my-rmd/#footer_and_header-->
---
# Who am I?
- Data science consultant
- Have built R packages for a client to streamline data processing and
visualization
- They/them pronouns
---
# What this talk is (and isn't) about
- It's about practices I've found useful in developing packages for personal use
or at work.
- Not (necessarily) about destined for Github/CRAN
- Not about how to build packages/intro to `devtools` and `roxygen2` (but there
will be resources on that at the end)
---
# Why should I bother building a package if no one else is going to use it?
- `devtools` and `usethis` provide tools for documenting what you were
thinking when you wrote the code and what your dependencies are
- `testthat` makes it easier to build unit tests that will keep you from
breaking things (or at least not noticing that you're breaking things)
- Good rule of thumb (that I can't find the source for): If you re-use the same
code three times, write a function. If you re-use the same function three times,
write a package.
- Future you will be grateful
- Low-stress way to develop related skills
---
# Also, you might be sharing your processes with your co-workers
- shared != public
- It's likely your team is going to run into same problem. An internal package
means you only have to solve it once.
- Code review is quicker when you don't have to double-check the same things
every time.
- Using a package of common processes also acts as a style guide.
---
# How repeated code is like for loops
## (and therefore cupcakes)
- [For those unfamiliar with the above reference](https://www.youtube.com/watch?v=GyNqlOjhPCQ)
- Repeating code obscures changes.
- Repeating code leads to accidental changes.
- Need to remember to manually propagate changes to all versions.
---
# utils.R is your friend (internal functions)
- (`utils.R` is a convention from [Hadley Wickham](http://r-pkgs.had.co.nz/namespace.html)
for that file where I put the functions that only exist to be called by other
functions.)
- Remember that not everything needs to be `@exported`-ed
- "Rule of three" applies to code within functions
- moving chunks of code into a (well-named) internal function can make it easier
to review and test your code
- makes it easy to update a common workflow or use a parameter to 'switch'
which workflow your using
---
# Using sensible defaults
- Use defaults to hide repeated processes that you don't always want to run.
- For me, this was driven home by a _very_ slow processing step I almost never
used.
- You may have steps that happen enough to put in your package, but sometimes
it's critical to skip those steps.
- often a simple TRUE/FALSE parameter is enough
---
# Case study in internal functions and defaults
- Starting point was something like this:
```r
# assorted documentation ommitted
# usethis::use_pipe() allows us to use %>%
#' @export
read_page_views <- function(){
here::here("data", "page_views.csv") %>%
readr::read_csv() %>%
# data comes in as UTC
dplyr::mutate(time = lubridate::with_tz(time,
tz = "America/Chicago"),
# some non-standard characters in page names were
# coming out garbled, and needed to be replaced
# for use in plot labels
page_name = regex_stuff(page_name))
}
```
---
# But then we added bunch of other data sources...
--
- Now code looks more like `here::here("data", "web_data","page_views.csv")`.
--
- Those first two steps are repeated a lot across similar functions.
--
- Also, there's discussion of reading directly from AWS.
--
- So now `read_page_views()` looks more like this:
--
```r
#' @export
read_page_views <- function(){
* read_data_file(location = "web_data",
* file_name = "page_views.csv") %>%
# data comes in as UTC
dplyr::mutate(time = lubridate::with_tz(time,
tz = "America/Chicago"),
page_name = regex_stuff(page_name))
}
```
--
- `read_data_file` is an internal function that wraps the first two steps
---
# That seems overly complicated...
--
- That's not entirely unfair.
--
- I may have omitted some code.
--
- The code looked more like this:
```r
#' @export
*read_page_views <- function(location){
* data_file <- dplyr::case_when(
* location == "local" ~ read_data_file(location = "web_data",
* file_name = "page_views.csv"),
* location == "aws" ~ read_data_file(location = "aws",
* file_name = "page_views.csv"),
* TRUE ~ stop("Helpful error message")
* )
data_file %>%
# data comes in as UTC
dplyr::mutate(time = lubridate::with_tz(time,
tz = "America/Chicago"),
page_name = regex_stuff(page_name))
}
```
---
# read_data_file() pseudo-code
```r
read_data_file <- function(location, file_name){
if (location = "aws"){
# code to handle reading from AWS
} else{
here::here("data", location, file_name) %>%
readr::read_csv()
}
}
```
- Added bonus: we can give a default value for `location` parameters.
- Went from `read_page_views <- function(location = "local"){# ...}` to
`read_page_views <- function(location = "aws"){# ...}` when AWS
authorization system was ready.
- Actual code still just uses `read_page_views()`.
- Plus, `read_data_file()` makes it easy to create new `read_*()` functions as
we add data sources. Just edit the wrapper and write some data-specific unit
tests.
---
# Remember regex_stuff(page_name)?
--
- Yes...why?
--
- It turns out it takes *forever* and almost never matters
--
- Better implementation:
--
```r
#' @export
*read_page_views <- function(location = "local", process_name = FALSE){
data_file <- dplyr::case_when(
location == "local" ~ read_data_file(location = "web_data",
file_name = "page_views.csv"),
location == "aws" ~ read_data_file(location = "aws",
file_name = "page_views.csv"),
TRUE ~ stop("Helpful error message")
) %>%
mutate(time = lubridate::with_tz(time,
tz = "America/Chicago"))
* if (process_name){
* dplyr::mutate(data_file,
* page_name = regex_stuff(page_name))
* } else {
* return(data_file)
* }
}
```
---
# Additional resources
- [RStudio's cheetsheet](https://github.com/rstudio/cheatsheets/blob/master/package-development.pdf)
- [_R Packages_ by Hadley Wickham](http://r-pkgs.had.co.nz/)
- [WIP 2<sup>nd</sup> edition with Jenny Bryan](https://r-pkgs.org/)
- ["Writing an R package from scratch" by Hilary
Parker](https://hilaryparker.com/2014/04/29/writing-an-r-package-from-scratch/)
- [Thomas Westlake's version of "Writing an R package from scratch" using
`usethis`](https://r-mageddon.netlify.com/post/writing-an-r-package-from-scratch/)
- ["R Package Primer" by Karl Broman](https://kbroman.org/pkg_primer/)
- ["`usethis` workflow for package development" by Emil
Hvitfedlt](https://www.hvitfeldt.me/blog/usethis-workflow-for-package-development/)
- [The tidyverse style guide](https://style.tidyverse.org/)
- If you're interested in packaging your employer's colo(u)r preferences for use
in `ggplot2`:
[Creating corporate colour palettes for ggplot2](
https://drsimonj.svbtle.com/creating-corporate-colour-palettes-for-ggplot2) by
Simon Jackson
---
# Thank you/questions
</textarea>
<style data-target="print-only">@media screen {.remark-slide-container{display:block;}.remark-slide-scaler{box-shadow:none;}}</style>
<script src="https://remarkjs.com/downloads/remark-latest.min.js"></script>
<script>var slideshow = remark.create({
"highlightStyle": "github",
"highlightLines": true,
"countIncrementalSlides": false
});
if (window.HTMLWidgets) slideshow.on('afterShowSlide', function (slide) {
window.dispatchEvent(new Event('resize'));
});
(function(d) {
var s = d.createElement("style"), r = d.querySelector(".remark-slide-scaler");
if (!r) return;
s.type = "text/css"; s.innerHTML = "@page {size: " + r.style.width + " " + r.style.height +"; }";
d.head.appendChild(s);
})(document);
(function(d) {
var el = d.getElementsByClassName("remark-slides-area");
if (!el) return;
var slide, slides = slideshow.getSlides(), els = el[0].children;
for (var i = 1; i < slides.length; i++) {
slide = slides[i];
if (slide.properties.continued === "true" || slide.properties.count === "false") {
els[i - 1].className += ' has-continuation';
}
}
var s = d.createElement("style");
s.type = "text/css"; s.innerHTML = "@media print { .has-continuation { display: none; } }";
d.head.appendChild(s);
})(document);
// delete the temporary CSS (for displaying all slides initially) when the user
// starts to view slides
(function() {
var deleted = false;
slideshow.on('beforeShowSlide', function(slide) {
if (deleted) return;
var sheets = document.styleSheets, node;
for (var i = 0; i < sheets.length; i++) {
node = sheets[i].ownerNode;
if (node.dataset["target"] !== "print-only") continue;
node.parentNode.removeChild(node);
}
deleted = true;
});
})();</script>
<script>
(function() {
var links = document.getElementsByTagName('a');
for (var i = 0; i < links.length; i++) {
if (/^(https?:)?\/\//.test(links[i].getAttribute('href'))) {
links[i].target = '_blank';
}
}
})();
</script>
<script>
slideshow._releaseMath = function(el) {
var i, text, code, codes = el.getElementsByTagName('code');
for (i = 0; i < codes.length;) {
code = codes[i];
if (code.parentNode.tagName !== 'PRE' && code.childElementCount === 0) {
text = code.textContent;
if (/^\\\((.|\s)+\\\)$/.test(text) || /^\\\[(.|\s)+\\\]$/.test(text) ||
/^\$\$(.|\s)+\$\$$/.test(text) ||
/^\\begin\{([^}]+)\}(.|\s)+\\end\{[^}]+\}$/.test(text)) {
code.outerHTML = code.innerHTML; // remove <code></code>
continue;
}
}
i++;
}
};
slideshow._releaseMath(document);
</script>
<!-- dynamically load mathjax for compatibility with self-contained -->
<script>
(function () {
var script = document.createElement('script');
script.type = 'text/javascript';
script.src = 'https://mathjax.rstudio.com/latest/MathJax.js?config=TeX-MML-AM_CHTML';
if (location.protocol !== 'file:' && /^https?:/.test(script.src))
script.src = script.src.replace(/^https?:/, '');
document.getElementsByTagName('head')[0].appendChild(script);
})();
</script>
</body>
</html>