generated from posit-conf-2023/workshop-template
-
Notifications
You must be signed in to change notification settings - Fork 9
/
dependencies.qmd
132 lines (108 loc) · 2.63 KB
/
dependencies.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
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
---
title: Managing dependencies 🐕
execute:
eval: false
---
This is how we might use another package in a script:
```{r}
lib_summary <- function(sizes = FALSE) {
if (!is.logical(sizes)) {
stop("'sizes' must be logical (TRUE/FALSE).")
}
pkgs <- utils::installed.packages()
pkg_tbl <- table(pkgs[, "LibPath"])
pkg_df <- as.data.frame(pkg_tbl, stringsAsFactors = FALSE)
names(pkg_df) <- c("Library", "n_packages")
if (sizes) {
library("fs")
pkg_df$lib_size <- vapply(
pkg_df$Library,
function(x) {
sum(file_size(dir_ls(x, recurse = TRUE)))
},
FUN.VALUE = numeric(1)
)
}
pkg_df
}
```
But this is how we do it in a package:
```{r}
use_package("fs")
```
### Use `fs` in our function:
```{r}
lib_summary <- function(sizes = FALSE) {
pkgs <- utils::installed.packages()
pkg_tbl <- table(pkgs[, "LibPath"])
pkg_df <- as.data.frame(pkg_tbl, stringsAsFactors = FALSE)
names(pkg_df) <- c("Library", "n_packages")
if (sizes) {
pkg_df$lib_size <- vapply(
pkg_df$Library,
function(x) {
sum(fs::file_size(fs::dir_ls(x, recurse = TRUE)))
},
FUN.VALUE = numeric(1)
)
}
pkg_df
}
```
```{r}
test() # failure for unused argument
```
### Update tests
```{r}
test_that("lib_summary fails appropriately", {
expect_error(lib_summary(sizes = "foo"), "not interpretable as logical")
})
test_that("sizes argument works", {
res <- lib_summary(sizes = TRUE)
expect_equal(names(res), c("Library", "n_packages", "lib_size"))
expect_type(res$lib_size, "double")
})
```
```{r}
check() # will warn about undocumented parameter
```
### Update documentation
Ctrl+Alt+Shift+R will insert the spot for the sizes param
```{r}
#' Provides a brief summary of the package libraries on your machine
#'
#' @param sizes Should the sizes of the libraries be calculated?
#' Logical; default `FALSE`.
#'
#' @return A data.frame containing the count of packages in each of the user's
#' libraries. A `lib_size` column is included if `sizes = TRUE`.
#' @export
#'
#' @examples
#' lib_summary()
#' lib_summary(sizes = TRUE)
```
```{r}
document()
check()
```
### Alternative: use importFrom
```{r}
use_import_from("purrr", "map_dbl")
```
Use `map_dbl()` directly without namespace qualifier:
```{r}
lib_summary <- function(sizes = FALSE) {
pkgs <- utils::installed.packages()
pkg_tbl <- table(pkgs[, "LibPath"])
pkg_df <- as.data.frame(pkg_tbl, stringsAsFactors = FALSE)
names(pkg_df) <- c("Library", "n_packages")
if (sizes) {
pkg_df$lib_size <- map_dbl(
pkg_df$Library,
~ sum(fs::file_size(fs::dir_ls(.x, recurse = TRUE)))
)
}
pkg_df
}
```