generated from inSilecoInc/workshop_R_template
-
Notifications
You must be signed in to change notification settings - Fork 0
/
part3.Rmd
580 lines (352 loc) · 11.2 KB
/
part3.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
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
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
---
title: "Part 3"
output:
xaringan::moon_reader:
css: [default, rd.css, rd-font.css, "hygge"]
lib_dir: assets
seal: false
nature:
highlightStyle: dracula
countIncrementalSlides: false
beforeInit: "macros.js"
---
```{r setup3, include = FALSE}
knitr::opts_chunk$set(
comment = "#>",
collapse = TRUE,
warning = FALSE,
message = FALSE,
dev = "png",
dpi = 108,
fig.width = 6,
fig.height = 4.5,
fig.align = 'center',
width = 120
)
options(htmltools.dir.version = FALSE, htmltools.preserve.raw = FALSE)
rfa <- function(...) icons::fontawesome(...)
emj <- function(...) emo::ji(...)
kig <- function(...) knitr::include_graphics(...)
```
---
class: inverse, center, middle
# `r rfa("toolbox")` Towards the gold standard
![:custom_hr]()
## `r rfa("database")` `r rfa("arrow-right")` `r rfa("r-project")` `r rfa("arrow-right")` `r rfa("cogs")` `r rfa("arrow-right")` `r rfa("file")` `r rfa("arrow-right")` `r rfa("sync-alt")`
## `r icons::fontawesome("clock")` ~ 30min
???
A word about setting up those tools depending on the environment.
A word about that all are not needed.
---
# Gold standard
```{R, echo = FALSE, out.width = "80%", purl = FALSE}
kig("img/spectrum.png")
```
.font80[`r rfa("file")` Peng. (2011).
[Reproducible Research in Computational Science.](https://science.sciencemag.org/content/334/6060/1226)]
--
### Towards full reproducibility, technologies are getting more complex
---
# Gold standard
<br>
```{R, echo = FALSE, out.width = "90%", purl = FALSE}
kig("img/pool.png")
```
???
A tool box of software
Coding a lot
---
# Version control `r emj("hand")`
--
### **I've changed something... not sure what... but it no longer works!**
--
<br>
### **Keep track of code changes!**
--
- to talk about the same piece of code
--
- to easily go back in time
--
- to tag code versions
---
# Version control
### Local versioning
```{r, echo = FALSE, out.width = "50%", purl = FALSE}
kig("https://git-scm.com/book/en/v2/images/local.png")
```
`r rfa("book")` Chacon & Straub (2014) [Pro Git](https://git-scm.com/book/en/v2/Getting-Started-About-Version-Control)
---
# Version control
### Centralized version control
```{r, echo = FALSE, out.width = "50%", purl = FALSE}
kig("https://git-scm.com/book/en/v2/images/centralized.png")
```
`r rfa("book")` Chacon & Straub (2014) [Pro Git](https://git-scm.com/book/en/v2/Getting-Started-About-Version-Control)
---
# Version control with Git
### Distributed version control such as Git
```{r, echo = FALSE, out.width = "42%", purl = FALSE}
kig("https://git-scm.com/book/en/v2/images/distributed.png")
```
`r rfa("book")` Chacon & Straub (2014) [Pro Git](https://git-scm.com/book/en/v2/Getting-Started-About-Version-Control)
<br>
- [GitKraken](https://www.gitkraken.com/)
- [GitHub Desktop](https://desktop.github.com/)
- R Studio
---
# Version control with Git + GitHub/Gitlab
```{r, echo = FALSE, out.width = "42%", purl = FALSE}
kig("https://git-scm.com/book/en/v2/images/distributed.png")
```
### e.g. The [`r rfa("github")`](https://github.com/inSilecoInc/workshop_reproducibility) repository for this workshop !
<span class='server'>`r rfa("github")` `r rfa("gitlab")`</span>
???
push and pull
show repo
git log
git pulll
---
# Use a pipeline toolkit: [GNU Make](https://www.gnu.org/software/make/manual/make.html)
--
### 1. Create a file `makefile` (or `Makefile`) [`r rfa("file")`](https://raw.githubusercontent.com/inSilecoInc/workshop_reproducibility/main/demo/makefile)
--
### 2. Write down what it needs to do (steps of your workflow)
```sh
all: ms code
ms: code ms/ms.Rmd
Rscript -e 'rmarkdown::render("ms/ms.Rmd")'
code:
Rscript -e 'lapply(list.files("R", full.names = TRUE), source); pipeline()'
```
--
### 3. Use `make` command e.g. `make` or `make ms`
--
`r rfa("blog")` Sachin Patil (2018) [What is a Makefile and how does it work?](https://opensource.com/article/18/8/what-how-makefile)
???
Not R-specific
---
# Use a pipeline toolkit: [targets](https://www.gnu.org/software/make/manual/make.html)
.pull-left[
### Successor of [drake](https://github.com/ropensci/drake)
> The targets package is a Make-like pipeline toolkit for Statistics and data science in R.
<br>
`r rfa("book")` [The targets R Package User Manual](https://books.ropensci.org/targets/index.html#motivation)
]
.pull-right[
```{r, echo = FALSE, out.width = "85%", purl = FALSE}
kig("https://docs.ropensci.org/targets/reference/figures/logo.png")
```
]
---
# Use a pipeline toolkit: [targets](https://www.gnu.org/software/make/manual/make.html)
### 1. Create a `_targets.R` file at the root of your repository [`r rfa("folder")`](https://github.com/inSilecoInc/workshop_reproducibility/raw/main/demo/targets_workflow.zip)
```{R, eval = FALSE}
library(targets)
library(tarchetypes)
rsrc <- lapply(list.files("R", full.names = TRUE), source)
options(tidyverse.quiet = TRUE)
tar_option_set(packages = c("weathercan", "tidyverse"))
list(
tar_target(data, get_data()),
tar_target(tab1, create_tab1(data)),
tar_target(fig1, create_fig1(data)),
tar_render(report, "ms/ms.Rmd")
)
```
--
### 2. use `tar_make()`
---
# Structure your repo as an R package
## Why?
--
### 1. Because the structure of an R package is very relevant for research compendia
--
### 2. Because R developers created great packages to develop packages!
--
`r rfa("file")` Marwick et al. (2018) [Packaging Data Analytical Work Reproducibly Using R (and Friends)](https://www.tandfonline.com/doi/abs/10.1080/00031305.2017.1375986)
---
# Structure your repo as an R package
## Basic structure of an R package
```sh
.
├── DESCRIPTION
├── doc
├── data
├── LICENSE
├── man
├── NAMESPACE
├── R
├── README.md
├── tests
└── vignettes
```
---
# Structure your repo as an R package
## Basic structure of an R package
```sh
.
├── DESCRIPTION # includes info about package versions
├── doc # include a website
├── data
├── LICENSE # always good to specify a license
├── man # document function
├── NAMESPACE # no need
├── R
├── README.md
├── tests # test your code
└── vignettes # include your ms
```
---
# Make a R package !
## Is it an overkill?
--
### **NO!**
--
- Jim Hester (2018) [You can make a package in 20 minutes](https://rstudio.com/resources/rstudioconf-2018/you-can-make-a-package-in-20-minutes-jim-hester/)
--
- The collection of packages for developers makes every steps easy
--
- This is a very good avenue to go further
???
Yes... it might be a steep learning curve but it's worth it.
---
# Make a R package !
## Benefit from popular development tools
```{r, echo = FALSE, out.width = "42%", purl = FALSE}
kig("https://raw.githubusercontent.com/r-lib/devtools/master/man/figures/logo.svg")
```
`r rfa("github")` [r-lib/devtools](https://github.com/r-lib/devtools/)
---
# Make a R package !
### It structures your workflow
```R
library(devtools)
# load all functions
load_all()
# document functions and edit Namespace
document()
# test functions
test()
# check the package integrity
check()
```
???
shared workflow
---
# Make a R package !
## Benefit from popular development tools
```{r, echo = FALSE, out.width = "42%", purl = FALSE}
kig("https://raw.githubusercontent.com/r-lib/usethis/master/man/figures/logo.png")
```
`r rfa("github")` [r-lib/usethis](https://github.com/r-lib/usethis)
---
# Make a R package !
> usethis is a workflow package: it automates repetitive tasks that arise during project setup and development, both for R packages and non-package projects.
```R
library(usethis)
usethis::use_gpl_license()
usethis::use_citation()
```
---
# Manage your dependencies
## Different R versions, different results!
```R
# R 3.6
df <- data.frame(fact = rep(c("F1", "F2"), each = 5), val = runif(10))
plot(val ~ fact, data = df) # works
# R 4.0
df <- data.frame(fact = rep(c("F1", "F2"), each = 5), val = runif(10))
plot(val ~ fact, data = df) # does not work
```
---
# Manage your dependencies
```{r, echo = FALSE, out.width = "50%", purl = FALSE}
kig("https://raw.githubusercontent.com/rstudio/renv/master/man/figures/logo.svg")
```
`r rfa("github")` [rstudio/renv](https://github.com/rstudio/renv)
---
# Manage your dependencies
## Save all dependencies in renv.lock and install them later seamlessly
```R
renv::init()
renv::snapshot()
# later (on another device)
renv::restore()
```
`r rfa("file")` [Introduction to renv](https://rstudio.github.io/renv/articles/renv.html)
---
# Continuous Integration (CI)
## Check your code on a freshly installed machine
--
### e.g. The [`r rfa("github")`](https://github.com/inSilecoInc/workshop_reproducibility/actions) repository for this workshop!
--
<br>
## Again, with an R package, everything goes smoothly
```R
usethis::use_github_action_check_standard()
```
???
start thinking about OS agnosticism
---
# Use a container with [Docker `r rfa("docker")`](https://www.docker.com/)
> Containers are a standardized unit of software that allows developers to
> isolate their app from its environment, solving the “it works on my machine”
> headache.
`r rfa("link")` https://www.docker.com/why-docker
---
# Use a container with [Docker `r rfa("docker")`](https://www.docker.com/)
```{r, echo = FALSE, out.width = "45%", purl = FALSE}
kig("img/contain1.png")
```
---
# Use a container with [Docker `r rfa("docker")`](https://www.docker.com/)
```{r, echo = FALSE, out.width = "45%", purl = FALSE}
kig("img/contain2.png")
```
---
# Use a container with [Docker `r rfa("docker")`](https://www.docker.com/)
```{r, echo = FALSE, out.width = "45%", purl = FALSE}
kig("img/contain3.png")
```
---
# Use a container with [Docker `r rfa("docker")`](https://www.docker.com/)
```{r, echo = FALSE, out.width = "45%", purl = FALSE}
kig("img/contain4.png")
```
---
# Use a container with [Docker `r rfa("docker")`](https://www.docker.com/)
```{r, echo = FALSE, out.width = "45%", purl = FALSE}
kig("img/contain5.png")
```
---
# Use a container with [Docker `r rfa("docker")`](https://www.docker.com/)
```{r, echo = FALSE, out.width = "45%", purl = FALSE}
kig("img/contain6.png")
```
---
# Use a container with [Docker `r rfa("docker")`](https://www.docker.com/)
### docker allows you
- to create images (via dockerfiles)
- to use images!
<br>
--
`r rfa("github")` [rocker-org/rocker](https://github.com/rocker-org/rocker)
--
```sh
$ docker run --rm -ti rocker/r-base
```
--
Run your code directly in the container!
---
# Holepunch
> `holepunch` will read the contents of your R project on GitHub, create a DESCRIPTION file with all dependencies, write a Dockerfile, add a badge to your README, and build a Docker image. Once these 4 steps are complete, any reader can click the badge and within minutes, be dropped into a free, live, RStudio server.
`r rfa("github")` https://github.com/karthik/holepunch
--
### - Uses [Binder](https://mybinder.org/) (GitHub repo ==> Docker image) + RStudio server.
--
### - Example: `r rfa("github")` [HomogenFishOntario](https://github.com/McCannLab/HomogenFishOntario)!
---
# Archive your code/data with [Zenodo](https://zenodo.org/)
```{r, echo = FALSE, out.width = "80%", purl = FALSE}
kig("img/zenodo.png")
```