/
utils.R
141 lines (118 loc) · 3.2 KB
/
utils.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
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
stop_if_not_installed <- function(pkg) {
if (!requireNamespace(pkg, quietly = TRUE)) {
stop(
pkg,
" must be installed to use this function.\n",
"install.packages('",
pkg,
"')"
)
}
invisible()
}
stop_if_shiny_not_installed <- function() {
stop_if_not_installed("shiny")
}
stop_if_rmarkdown_not_installed <- function() {
stop_if_not_installed("rmarkdown")
}
stop_if_blogdown_not_installed <- function() {
stop_if_not_installed("blogdown")
}
stop_if_not_type <- function(x, type) {
if (is_yml_blank(x)) return(invisible(x))
if (!inherits(x, type)) {
x_text <- rlang::quo_text(rlang::quo(x))
stop_must_be_type(x_text, type)
}
invisible(x)
}
stop_if_not_all_type <- function(x, type) {
x_text <- rlang::quo_text(rlang::quo(x))
all_type <- all(purrr::map_lgl(x, inherits, type))
if (!all_type) {
x_text <- rlang::quo_text(rlang::quo(x))
stop_must_be_type(x_text, type)
}
}
stop_must_be_type <- function(x, type) {
usethis::ui_stop(
"{usethis::ui_code(x)} must be of type {usethis::ui_code(type)}"
)
}
warn_if_duplicate_fields <- function(yml_object, new_fields) {
fields <- names(new_fields)
duplicate_fields <- any(names(yml_object) %in% fields)
if (duplicate_fields) {
fields <- glue::glue("`{fields}`") %>%
glue::glue_collapse(sep = ", ", last = " and ")
msg <- glue::glue(
"Top-level fields {fields} already present. \\
Replacing the existing fields. \\
Use `yml_replace() if you want to replace fields without a warning."
)
warning(msg, call. = FALSE)
}
invisible(yml_object)
}
capture_yml <- function(.yml) {
utils::capture.output(print(.yml))
}
split_pluck <- function(string) {
x <- stringr::str_split(string, "\n")
x[[1]]
}
prepend_namespace <- function(function_namespace, function_name) {
ifelse(
is.null(function_namespace),
function_name,
paste0(function_namespace, "::", function_name)
)
}
`%nin%` <- Negate("%in%")
# These are derived from https://github.com/r-lib/cli/blob/e9acc82b0d20fa5c64dd529400b622c0338374ed/R/tree.R#L111
box_chars <- function(.subset = NULL) {
if (is_utf8_output()) {
x <- list(
"h" = "\u2500", # horizontal
"v" = "\u2502", # vertical
"l" = "\u2514",
"j" = "\u251C"
)
} else {
x <- list(
"h" = "-", # horizontal
"v" = "|", # vertical
"l" = "\\",
"j" = "+"
)
}
if (!is.null(.subset)) {
return(x[[.subset]])
}
x
}
is_latex_output <- function() {
if (!("knitr" %in% loadedNamespaces())) return(FALSE)
get("is_latex_output", asNamespace("knitr"))()
}
is_utf8_output <- function() {
opt <- getOption("cli.unicode", NULL)
if (!is.null(opt)) {
isTRUE(opt)
} else {
l10n_info()$`UTF-8` && !is_latex_output()
}
}
temporary_dir <- function() {
tmp_dir_path <- file.path(tempdir(), "..ymlthis_temp_dir..")
if (!fs::dir_exists(tmp_dir_path)) {
fs::dir_create(tmp_dir_path)
}
tmp_dir_path
}
unlink_temporary_dir <- function() {
tmp_dir_path <- file.path(tempdir(), "..ymlthis_temp_dir..")
unlink(tmp_dir_path, recursive = TRUE, force = TRUE)
invisible(tmp_dir_path)
}