-
Notifications
You must be signed in to change notification settings - Fork 0
/
_common.R
50 lines (43 loc) · 1.41 KB
/
_common.R
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
# example R options set globally
options(width = 100)
# example chunk options set globally
knitr::opts_chunk$set(
comment = "#>",
collapse = TRUE,
echo=FALSE
)
# This reads the list of files/directories under
# copy_resources: in _bookdown.yml and copies them
# to the output document folder.
library(yaml)
library(fs)
yaml_path <- "_bookdown.yml"
config <- yaml::read_yaml(yaml_path)
additional_resources <- config$copy_resources
output_dir <- config$output_dir
copy_resources <- function(resources, output_dir) {
for (resource in resources) {
if (fs::file_exists(resource)) {
if (fs::is_file(resource)) {
target <- file.path(output_dir, resource)
fs::dir_create(fs::path_dir(target), recurse = TRUE)
fs::file_copy(resource, target, overwrite = TRUE)
}
else if (fs::is_dir(resource)) {
target <- file.path(output_dir, resource)
fs::dir_create(fs::path_dir(target), recurse = TRUE)
fs::dir_copy(resource, target, overwrite = TRUE)
}
} else if (grepl("[*?]", resource)) {
files <- Sys.glob(resource)
for (file in files) {
target <- file.path(output_dir, file)
fs::dir_create(fs::path_dir(target), recurse = TRUE)
fs::file_copy(file, target, overwrite = TRUE)
}
} else {
warning(paste("Resource", resource, "not found or not recognized."))
}
}
}
copy_resources(additional_resources, output_dir)