-
Notifications
You must be signed in to change notification settings - Fork 0
/
tip_packages.qmd
38 lines (25 loc) · 1.85 KB
/
tip_packages.qmd
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
---
title: "Streamlined R Package Loading"
---
## Overview
Loading packages / libraries in R can be cumbersome when working collaboratively because there is no guarantee that you all have the same packages installed. While you could comment-out an `install.packages()` line for every package you need for a given script, we recommend using the R package `librarian` to greatly simplify this process!
`librarian::shelf()` accepts the names of all of the packages--either CRAN or GitHub--installs those that are missing in that particular R session and then attaches all of them.
## Traditional Package Loading
To load packages typically you'd have something like the following in your script:
```{r library_og_method}
#| eval: false
# Install packages (if needed)
install.packages("tidyverse")
install.packages("devtools")
devtools::install_github("NCEAS/scicomptools")
# Load libraries
library(tidyverse); library(scicomptools)
```
## Package Loading with `librarian`
With `librarian::shelf()` however this becomes _much_ cleaner! In addition to being fewer lines, using `librarian` also removes the possibility that someone running your code misses one of the packages that your script depends on and then the script breaks for them later on. `librarian::shelf()` automatically detects whether a package is installed, installs it if necessary, and then attaches the package.
In essence, `librarian::shelf()` wraps `install.packages()`, `devtools::install_github()`, and `library()` into a single, human-readable function.
```{r shelf_demo, eval = FALSE}
# Install and load packages!
librarian::shelf(tidyverse, NCEAS/scicomptools)
```
When using `librarian::shelf()`, package names do not need to be quoted and GitHub packages can be installed without the additional steps of installing the `devtools` package and using `devtools::install_github()` instead of `install.packages()`.