-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpkgslides.qmd
534 lines (362 loc) · 10.5 KB
/
pkgslides.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
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
% Generated by pkgslides: do not edit by hand
---
title: pkgslides
subtitle: 0.1.0
format:
revealjs:
theme: default
navigation-mode: vertical
self-contained: true
scrollable: true
footer: Gus Lipkin [aut, cre]
---
## Automatically Generate Presentations for Source Code
::: {.r-fit-text}
When pointed at the root directory of an R Package Project, this
function will automatically generate and render a Quarto/Revealjs
presentation for that package. It will have a title slide and description
slide generated from the package's DESCRIPTION file and each exported
function will have slides with its description and returns, parameters,
examples, and source code.
:::
```{r}
#| echo: false
library(pkgslides)
```
<script type='text/javascript' charset='utf-8'>
function add_author_footer() {
Reveal.on( 'slidechanged' , event => {
let footer = document.querySelector('div.footer p');
if (event.currentSlide.matches('#title-slide')) {
footer.innerHTML = 'Gus Lipkin [aut, cre]';
} else {
footer.innerHTML = '';
}
});
};
window.addEventListener('load', (event) => {
add_author_footer();
});
</script>
# build_presentation.R
## Create a Revealjs Presentation with Quarto
::: {.r-fit-text}
- **Topic:** `build_presentation`
- **Description:** When pointed at the root directory of an R Package Project, this
function will automatically generate and render a Quarto/Revealjs
presentation for that package. It will have a title slide and description
slide generated from the package's DESCRIPTION file and each exported
function will have slides with its description and returns, parameters,
examples, and source code.
- **Return:** This function creates and renders a .qmd presentation but does not
return an R object.
:::
## Parameters
::: {.r-fit-text}
- `package`: A file path to the root directory of an R package source
folder, the name of an R package from CRAN, or an R package from
GitHub in the username/repository format.
- `file`: The file name for your .qmd file. This can be a path so long as
it ends with a `file_name.qmd`
- `yaml`: A `_pkgslides.yml` file or function call to `create_yaml()`
:::
## Code
::: {.r-fit-text}
```{.r}
build_presentation <- function(package = getwd(), file = NULL, yaml = create_yaml()) {
package <- .find_package(package)
file <- .find_file(package$path, file)
yaml <- .parse_yaml(yaml)
if (package$name %in% rownames(utils::installed.packages())) {
chunk_opt <- "echo"
} else { chunk_opt <- "eval" }
credits <- .get_credits(package$path)
title_contents <-
.get_title(package$path, credits) |>
.collate_title(yaml)
package_contents <-
.get_description(package$path, credits) |>
.collate_description(chunk_opt)
contents <- .get_roxygen(package$path, yaml)
function_contents <- .get_functions(contents$functions, yaml)
r_files <-
sapply(contents$functions, \(b) .get_file_from_path(b$file))
function_contents <-
r_files |>
unique() |>
lapply(
.construct_verticals,
r_files, function_contents, chunk_opt
) |>
unlist()
data_contents <-
.get_datasets(contents$datasets, yaml) |>
lapply(.collate_datasets) |>
unlist()
file_contents <- c(
title_contents,
package_contents,
function_contents,
data_contents
)
print(file)
file.create(file)
fileConn <- file(file)
writeLines(file_contents, fileConn)
close(fileConn)
quarto::quarto_render(file)
}
```
:::
## `.find_file`
::: {.r-fit-text}
- **Topic:** `.find_file`
- **Return:** A file path to write the .qmd to
:::
## Parameters
::: {.r-fit-text}
- `package`: A path to a package source
- `file`: A file path
:::
## Code
::: {.r-fit-text}
```{.r}
.find_file <- function(package, file) {
if (is.null(file)) {
file <- .get_file_from_path(package)
}
if (!grepl("\\.qmd$", file)) {
file <- paste0(file, ".qmd")
}
file <- glue::glue("{getwd()}/{file}")
return(file)
}
```
:::
## Tries to Find or Download a Package
::: {.r-fit-text}
- **Topic:** `.find_package`
- **Return:** A path to a package source
:::
## Parameters
::: {.r-fit-text}
- `package`: A package name or file path to a package source
:::
## Code
::: {.r-fit-text}
```{.r}
.find_package <- function(package) {
if (package == getwd() | dir.exists(package)) {
name <- rev(strsplit(package, "/")[[1]])[1]
return(list(name = name, path = package))
}
source_path <- tempdir()
if (package %in% rownames(utils::available.packages())) {
print("cran")
utils::download.packages(package, source_path)
source_name <-
list.files(source_path, pattern = glue::glue("{package}.*\\.tar\\.gz"))
utils::untar(
glue::glue("{source_path}/{source_name}"),
exdir = glue::glue("{source_path}")
)
} else {
print("github")
repo <- remotes::parse_repo_spec(package)
package <- repo$repo
stopifnot(repo$username != "" & repo$repo != "")
zip <- glue::glue("{source_path}/{package}.zip")
utils::download.file(url = glue::glue("https://github.com/{repo$username}/{repo$repo}/archive/master.zip"), zip)
if (file.exists(zip)) {
utils::unzip(zip, exdir = glue::glue("{source_path}"), overwrite = TRUE)
}
dirs <- list.dirs(glue::glue("{source_path}"), recursive = FALSE)
dirs <- grep(glue::glue("({package}-(main|master))"), dirs, value = TRUE)
new_name <- glue::glue("{source_path}/{package}")
unlink(new_name, recursive = TRUE)
file.rename(dirs, new_name)
# file.copy(glue::glue("{getwd()}/_pkgslides.yml"), source_path)
}
package_path <- glue::glue("{source_path}/{package}")
return(list(name = package, path = package_path))
}
```
:::
# yaml.R
## Create `_pkgslides.yml`
::: {.r-fit-text}
- **Topic:** `create_yaml`
- **Return:** This will create the file and write to it, then return the
file path.
:::
## Parameters
::: {.r-fit-text}
- `path`: A file path to where you want the yaml file written. This should
not end in a slash of any kind.
- `format_theme`: A length-one character vector of theme details
- `format_functions`: A named list of format function options
- `format_datasets`: A named list of format dataset options
- `choose_functions`: A list of file or function names
- `choose_datasets`: A vector of dataset names
:::
## Code
::: {.r-fit-text}
```{.r}
create_yaml <- function(
path = getwd(),
format_theme = c(), format_functions = list(), format_datasets = list(),
choose_functions = list(), choose_datasets = c()
) {
# package <- .find_package(package)
file <- glue::glue("{path}/_pkgslides.yml")
# stopifnot(!file.exists(file))
# formatting
format_theme <- format_theme[format_theme %in% c("theme")]
format_functions <-
format_functions[names(format_functions) %in% c("description", "return", "parameters", "examples", "code")]
format_datasets <-
format_datasets[names(format_datasets) %in% c("format", "source", "references")]
.append_to_yaml <- function(yaml, obj, name) {
if (length(obj) > 0) { yaml$format[[name]] <- obj }
return(yaml)
}
yaml <-
list() |>
.append_to_yaml(format_theme, "theme") |>
.append_to_yaml(format_functions, "functions") |>
.append_to_yaml(format_datasets, "datasets")
# print options
if (length(choose_functions) > 0) {
yaml <- .process_choose_functions(yaml, choose_functions)
}
yaml$datasets <- choose_datasets
yaml |>
.check_yaml() |>
yaml::write_yaml(file)
print(glue::glue("Config written to '{file}'"))
return(file)
}
```
:::
## Parse `_pkgslides.yml`
::: {.r-fit-text}
- **Topic:** `.parse_yaml`
- **Return:** A list representing a yaml file
:::
## Parameters
::: {.r-fit-text}
- `file`: A file path the _pkgslides.yml file
:::
## Code
::: {.r-fit-text}
```{.r}
.parse_yaml <- function(file) {
if (file.exists(file)) {
yaml <-
yaml::read_yaml(file) |>
.check_yaml()
} else {
yaml <- .check_yaml(list())
}
return(yaml)
}
```
:::
## Fill an Incomplete `_pkgslides.yml`
::: {.r-fit-text}
- **Topic:** `.check_yaml`
- **Return:** A list representing a yaml file
:::
## Parameters
::: {.r-fit-text}
- `yaml`: A list of properties from `.parse_yaml()`
:::
## Code
::: {.r-fit-text}
```{.r}
.check_yaml <- function(yaml) {
if (is.null(yaml$functions)) { yaml$functions <- "auto" }
if (is.null(yaml$datasets)) { yaml$datasets <- "all" }
if (is.null(yaml$format$theme)) { yaml$format$theme <- "default" }
yaml <-
yaml |>
.set_as_true(
"functions",
c("description", "return", "parameters",
"examples", "code")
) |>
.set_as_true(
"datasets",
c("format", "source", "references")
)
return(yaml)
}
```
:::
## Set Empty Values as True
::: {.r-fit-text}
- **Topic:** `.set_as_true`
- **Return:** A yaml where non-included options are set to TRUE
:::
## Parameters
::: {.r-fit-text}
- `yaml`: A yaml file as a list
- `type`: "functions" or "datasets"
- `options`: A vector of the options exposed to users in the yaml format
:::
## Code
::: {.r-fit-text}
```{.r}
.set_as_true <- function(yaml, type, options) {
requested <- names(yaml$format[[type]])
not_specified <- options[!(options %in% requested)]
yaml$format[[type]] <-
lapply(seq_along(not_specified), \(s) TRUE) |>
`names<-`(not_specified) |>
append(yaml$format[[type]])
return(yaml)
}
```
:::
## Process Choose Functions
::: {.r-fit-text}
- **Topic:** `.process_choose_functions`
- **Return:** A list representing a yaml file
:::
## Parameters
::: {.r-fit-text}
- `yaml`: A list representing a yaml file
- `choose`: The list passed to `choose_functions` in the `create_yaml`
function
:::
## Code
::: {.r-fit-text}
```{.r}
.process_choose_functions <- function(yaml, choose) {
yaml2 <- list()
dim <- vapply(choose, length, FUN.VALUE = 1)
choose_names <- names(choose)
opt_regex <- "^(auto|exported|all)$|\\.R$"
if (all(dim == 1) & is.null(choose_names)) {
stopifnot(all(grepl(opt_regex, choose)))
yaml2$functions <- lapply(choose, \(x) { list(file = x) })
} else {
if (any(choose_names == "")) {
needs_name <- which(choose_names == "" & dim == 1)
names(choose)[needs_name] <- unlist(sapply(choose, unlist)[needs_name])
choose[needs_name] <- "all"
choose_names <- names(choose)
}
stopifnot(all(grepl(opt_regex, choose_names)))
yaml2$functions <-
mapply(\(x, y) {
list(file = x, slides = y)
}, choose_names, choose, SIMPLIFY = FALSE, USE.NAMES = FALSE)
}
yaml <-
list(yaml, yaml2) |>
unlist(recursive = FALSE)
return(yaml)
}
```
:::