-
Notifications
You must be signed in to change notification settings - Fork 2
/
index.Rmd
648 lines (453 loc) · 12.6 KB
/
index.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
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
---
title: "<br />Introduction to `renv`"
subtitle: "<br />"
author: <br /><br />Nicolas CASAJUS<br />.inst[Data scientist @ FRB-CESAB]
date: .inst[November 2023]
output:
xaringan::moon_reader:
css: ["css/xaringan-themer.css", "css/custom.css"]
lib_dir: "libs"
seal: true
yolo: false
self_contained: false
nature:
ratio: "4:3"
titleSlideClass: ["right", "middle"]
highlightStyle: 'zenburn'
highlightLines: false
countIncrementalSlides: false
slideNumberFormat: "%current% / %total%"
beforeInit: "libs/macros.js"
---
## Degrees of Reproducibility
`r fontawesome::fa_i("thumbs-up", class = "emph")` Research compendium<br/>
`r fontawesome::fa_i("thumbs-up", class = "emph")` Version control<br/>
`r fontawesome::fa_i("thumbs-up", class = "emph")` Literate programming<br/>
`r fontawesome::fa_i("thumbs-up", class = "emph")` Code refactoring
--
<br/>
What about `r fontawesome::fa_i("r-project")` packages (and their versions)?
--
- What are the packages versions used for my project?
- Will my project be usable on other system or in the future?
- How to deal with projects requiring different versions of a package?
---
## The package `renv`
.pull-leftt[
.center[[![:scale 75%](https://raw.githubusercontent.com/rstudio/renv/master/man/figures/logo.svg)](https://rstudio.github.io/renv/articles/renv.html)]
]
.pull-rightt[
- Developed by [**Kevin Ushey**](https://kevinushey.github.io/), also maintainer of the package [`reticulate`](https://rstudio.github.io/reticulate/)
- [`renv`](https://rstudio.github.io/renv/articles/renv.html) is a toolkit used to manage project-local libraries of `r fontawesome::fa_i("r-project")` packages
- `renv` is the successor of [`packrat`](https://rstudio.github.io/packrat/)
]
--
`renv` makes your project more:
`r fontawesome::fa_i("hand-point-right", class = "emph")` **Isolated**:
each project has its own library of `r fontawesome::fa_i("r-project")` packages.
`r fontawesome::fa_i("hand-point-right", class = "emph")` **Portable** and **reproducible**:
`renv` captures the state of your `r fontawesome::fa_i("r-project")` packages
within a _lockfile_.
--
<br/>
Inspired from `r fontawesome::fa_i("python")` and the `venv` module.
---
## Libraries _vs_ packages
--
`r fontawesome::fa_i("hand-point-right")` `Package` - a
collection of `r fontawesome::fa_i("r-project")` functions, data, and compiled
code in a well-defined format.
`r fontawesome::fa_i("hand-point-right")` `Library` - the
directory into which packages are installed.
--
<br />
By default, `r fontawesome::fa_i("r-project")` has two (or more) libraries:
```{r eval=FALSE}
.libPaths()
## [1] "C:/Users/Jane/AppData/Local/R/win-library/4.3"
## [2] "C:/Program Files/R/R-4.3.2/library"
```
The first is the **user** library and the second the **system** library.
--
<br/>
```{r eval=FALSE}
find.package("rmarkdown")
## [1] "C:/Users/Jane/AppData/Local/R/win-library/4.3/rmarkdown"
find.package("base")
## [1] "C:/PROGRA~1/R/R-43~1.2/library/base"
```
---
## The `r fontawesome::fa_i("r-project")` system
.center[
![:scale 75%](img/library-1.png)
]
All projects share the same versions of installed packages
---
## With `renv`
.pull-left[
.center[![:scale 95%](img/library-1.png)]
The `r fontawesome::fa_i("r-project")` system
]
.pull-right[
.center[![:scale 95%](img/library-2a.png)]
The `renv` system
]
---
## With `renv` - Global package cache
.pull-left[
.center[![:scale 95%](img/library-1.png)]
The `r fontawesome::fa_i("r-project")` system
]
.pull-right[
.center[![:scale 95%](img/library-2b.png)]
The `renv` system
]
---
## Initialization
- Installation
```{r eval=FALSE}
## Install renv ----
install.packages("renv")
```
<br />
--
- Initialization from command line
```{r eval=FALSE}
## Initiate renv for the project ----
renv::init()
```
--
- Initialization with RStudio
![:scale 35%](img/rstudio-renv.png)
---
## Initialization
When you open the project you will see this welcome message:
```
R version 4.3.2 (2023-10-31 ucrt) -- "Eye Holes"
Copyright (C) 2023 The R Foundation for Statistical Computing
Platform: x86_64-w64-mingw32/x64 (64-bit)
- Project '~/Documents/myproject' loaded. [renv 1.0.3]
>
```
This means that the project is currently using the `renv` system
---
## Infrastructure
What happened?
```
.
├── (.git)
├── (.gitignore)
│
├── (myproject.Rproj)
│
├── .Rprofile
│
├── renv/
│ ├── .gitignore
│ ├── activate.R
│ ├── library/
│ └── settings.dcf
│
└── renv.lock
```
---
## Infrastructure
What happened?
```
.
├── (.git) # Git files
├── (.gitignore) # Untracked files by git
│
├── (myproject.Rproj)
│
├── .Rprofile
│
├── renv/
│ ├── .gitignore
│ ├── activate.R
│ ├── library/
│ └── settings.dcf
│
└── renv.lock
```
---
## Infrastructure
What happened?
```
.
├── (.git) # Git files
├── (.gitignore) # Untracked files by git
│
├── (myproject.Rproj) # RStudio files
│
├── .Rprofile
│
├── renv/
│ ├── .gitignore
│ ├── activate.R
│ ├── library/
│ └── settings.dcf
│
└── renv.lock
```
---
## Infrastructure
What happened?
```
.
├── (.git) # Git files
├── (.gitignore) # Untracked files by git
│
├── (myproject.Rproj) # RStudio files
│
├── .Rprofile # Activate renv on project opening
│
├── renv/
│ ├── .gitignore
│ ├── activate.R
│ ├── library/
│ └── settings.dcf
│
└── renv.lock
```
---
## Infrastructure
What happened?
```
.
├── (.git) # Git files
├── (.gitignore) # Untracked files by git
│
├── (myproject.Rproj) # RStudio files
│
├── .Rprofile # Activate renv on project opening
│
├── renv/
│ ├── .gitignore # Ignore large renv files (e.g. packages)
│ ├── activate.R # R script to launch renv
│ ├── library/ # R packages
│ └── settings.dcf # renv settings
│
└── renv.lock
```
---
## Infrastructure
What happened?
```
.
├── (.git) # Git files
├── (.gitignore) # Untracked files by git
│
├── (myproject.Rproj) # RStudio files
│
├── .Rprofile # Activate renv on project opening
│
├── renv/
│ ├── .gitignore # Ignore large renv files (e.g. packages)
│ ├── activate.R # R script to launch renv
│ ├── library/ # R packages
│ └── settings.dcf # renv settings
│
└── renv.lock # Packages metadata - Lockfile
```
---
## The lockfile `renv.lock`
```
{
"R": {
"Version": "4.3.2",
"Repositories": [
{
"Name": "CRAN",
"URL": "https://packagemanager.posit.co/cran/latest"
}
]
},
"Packages": {
"renv": {
"Package": "renv",
"Version": "1.0.3",
"Source": "Repository",
"Repository": "CRAN",
"Hash": "41b847654f567341725473431dd0d5ab"
}
}
}
```
This is a simple **JSON** file that specifies metadata on the `r fontawesome::fa_i("r-project")` version and all packages used for the project
--
- With this _lockfile_ you freeze (and document) your `r fontawesome::fa_i("r-project")` environment
- To collaborate with others you **only** need to share this file
---
## Installing packages
To install packages with `renv`, use the function `renv::install()`
You can install the latest version of a package, a specific version of a package,
a package from GitHub, GitLab...
```{r eval = FALSE}
## Install the latest version ----
renv::install("devtools")
## Install a specific version ----
renv::install("devtools@2.3.0")
## Install the development version from GitHub ----
renv::install("r-lib/devtools")
```
--
<br />
If you have a `DESCRIPTION` file you can install all required packages as follow:
```{r eval = FALSE}
## Install (latest versions) of packages listed in DESCRIPTION ----
renv::install()
```
--
Using `renv::install()` without argument will also screen `R` and `Rmd` files and
install packages mentioned as `library(pkg)`, `require(pkg)`, and `pkg::fun()`
---
## Working with `renv`
- Just work as usual
--
- Use `renv::status()` to check the status of your `renv` project
--
```{r eval = FALSE}
renv::status()
## * The project is already synchronized with the lockfile.
##
## The following package(s) are used in the project, but are not installed:
##
## ggplot2
##
## Consider installing these packages, and then using `renv::snapshot()`
## to record these packages in the lockfile.
```
In this example we use the package `ggplot2` in our code but it is not installed (locally)
--
- Let's install this package
```{r eval = FALSE}
renv::install("ggplot2")
```
---
## Working with `renv`
- Let's use `renv::status()` again
```{r eval = FALSE}
renv::status()
## The following package(s) are installed but not recorded in the lockfile:
##
## ggplot2 [3.4.4]
##
## Use `renv::snapshot()` to add these packages to your lockfile.
```
--
- Here we need to update the *lockfile* with `renv::snapshot()`
```{r eval = FALSE}
renv::snapshot()
## The following package(s) will be updated in the lockfile:
##
## # CRAN ===============================
## - ggplot2 [* -> 3.4.4]
##
## Do you want to proceed? [y/N]: y
##
## * Lockfile written to '~/Documents/myproject/renv.lock'.
```
--
```{r eval = FALSE}
renv::status()
## * The project is already synchronized with the lockfile.
```
---
## Cleaning the local environment
- If you no longer use some packages you need to remove them from the *lockfile*
```{r eval = FALSE}
renv::status()
## The following package(s) are no longer used in this project:
##
## ggplot2 [3.4.4]
##
## Use `renv::snapshot()` to remove them from the lockfile.
```
--
<br />
```{r eval = FALSE}
renv::snapshot()
## The following package(s) will be updated in the lockfile:
##
## # CRAN ===============================
## - ggplot2 [3.4.4 -> *]
##
## Do you want to proceed? [y/N]: y
##
## * Lockfile written to '~/Documents/myproject/renv.lock'.
```
---
## Cleaning the local environment
- You can also remove them from your local library (optional)
```{r eval = FALSE}
renv::clean()
## The following packages are installed in the project library,
## but appear to be no longer used in your project.
##
## ggplot2
##
## These packages will be removed.
##
## Do you want to proceed? [y/N]: y
##
## Removing package 'ggplot2' ... Done!
##
## * Done! Removed 1 package.
## * The project has been cleaned.
```
---
## Collaborating with `renv`
- Just share the updated *lockfile* (with `r fontawesome::fa_i("github")`)
--
- Your collaborator will then run `renv::restore()` to locally install required
packages (with the same versions as you)
```{r eval = FALSE}
renv::restore()
## The following package(s) will be updated:
##
## # CRAN ===============================
## - ggplot2 [* -> 3.4.4]
##
## Do you want to proceed? [y/N]: y
##
## Installing ggplot2 [3.4.4] ...
## OK [linked cache]
```
`r fontawesome::fa_i("hand-point-right")` If you only share the *lockfile* your collaborator needs to run `renv::init()` before
--
<br />
`r fontawesome::fa_i("exclamation-triangle", class = "emph")` One limit: collaborators need
to have the **same major version** of `r fontawesome::fa_i("r-project")`
---
## Wrap-up
```{r eval=FALSE}
## Initiate renv for the project ----
renv::init()
## Install < pkg_name > for the project ----
renv::install("pkg_name")
renv::install("pkg_name@version")
renv::install("github/pkg_name")
## Install packages listed in DESCRIPTION (and/or R and Rmd files) ----
renv::install()
## Check renv status ----
renv::status()
## Update lockfile (save local environment) ----
renv::snapshot()
## Uninstall unused packages ----
renv::clean()
## Restore local environment ----
renv::restore()
## Other functions ----
renv::deactivate() ## Deactivate local environment
renv::activate() ## (Re)activate local environment
renv::dependencies() ## List used packages (R and Rmd files)
renv::history() ## Browse previous commits (with git)
```
---
## Resources
https://kevinushey-2020-rstudio-conf.netlify.app/slides.html
https://elise.maigne.pages.mia.inra.fr/2021_package_renv/presentation.html
https://rstudio.github.io/renv/articles/renv.html
https://6chaoran.wordpress.com/2020/07/20/introduction-of-renv-package/
https://github.com/rstudio/renv/