-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathutils.R
196 lines (184 loc) · 5.94 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
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
#' Generate a ID
#'
#' @description
#' Generates a HTML valid ID.
#' HTML valid IDs should follow specific standards:
#' - The ID must start with a letter (a-z or A-Z).
#' - All subsequent characters can be letters, numbers (0-9), hyphens (-),
#' underscores (_), colons (:), and periods (.).
#' - Each ID must be unique within the document.
#'
#' @importFrom magrittr "%>%"
#' @importFrom stringi stri_rand_strings
#'
#' @return A valid CSS id.
#' @keywords utils internal
generateID <- function() {
generated_id <- Sys.time() %>%
as.integer() %>%
paste0(stri_rand_strings(1, 12))
getOption("imola.settings")$string_templates$generated_id %>%
stringTemplate(id = generated_id)
}
#' Evaluate a string template
#'
#' @description
#' Processes a string template in the [htmlTemplate] format into a valid string
#' with no placeholders. The string must use the [htmlTemplate] format, meaning
#' placeholders are marked using the {{placeholder}} convention.
#'
#' @param string The string template. Uses the same format as the [htmlTemplate]
#' function from shiny. placeholders in the template should use the
#' {{placeholder}} format.
#' @param ... Named arguments to use in the template string. All placeholders
#' in the template string must have a corresponding named argument.
#'
#' @importFrom magrittr "%>%"
#' @importFrom shiny htmlTemplate
#'
#' @return A string.
#' @keywords utils internal
stringTemplate <- function(string, ...) {
string %>%
htmlTemplate(text_ = ., ...) %>%
as.character()
}
#' Evaluate a css template
#'
#' @description
#' Applies a CSS statement template stored in the package settings. These
#' templates use the [htmlTemplate] format, meaning placeholders are marked
#' using the {{placeholder}} convention.
#' Each placeholder value should be passed as a named argument to the function
#' using the placeholder value as a name.
#' Used primarily as a shorthand to [stringTemplate] for stored templates.
#'
#' @param template The template name to use. Available templates are saved in
#' options, under `getOption("imola.settings")$string_templates`.
#' @param ... Named arguments to use in the template string. All placeholders
#' in the template must have a corresponding named argument.
#'
#' @importFrom magrittr "%>%"
#'
#' @return A valid CSS string.
#' @keywords utils internal
stringCSSRule <- function(template, ...) {
getOption("imola.settings")$string_templates[[template]] %>%
stringTemplate(...)
}
#' Process HTML content
#'
#' @description
#' Adds a css class to any HTML elements from the content that are named and
#' which name is in the areas vector for names. This allows content to be
#' assigned to the grid areas via named argument while still allowing other
#' generic HTML tag attributes to be used.
#'
#' @param content A (named) list of HTML elements.
#' @param areas The names in content that should have a class added.
#'
#' @importFrom stringi stri_remove_empty
#' @importFrom magrittr "%<>%"
#' @importFrom shiny tagAppendAttributes
#' @importFrom htmltools tagQuery
#'
#' @return A list of HTML elements.
#' @keywords utils internal
processContent <- function(content, areas) {
for (name in stri_remove_empty(names(content))) {
if (name %in% areas) {
tags <- content[[name]] %>% htmltools::tagQuery()
tags$addClass(name)
content[[name]] <- tags$allTags()
names(content) <- replace(names(content), names(content) == name, "")
}
}
content
}
#' Process Object to css
#'
#' @description
#' Converts a R List or vector object into a valid css string. Used primarily
#' to convert normalized attribute values into css values during processing.
#'
#' @param value List or vector with the values to be converted into css
#' @param property The target css property for which the value will be used.
#'
#' @return string containing a valid css value.
#' @keywords utils internal
valueToCSS <- function(value, property) {
if (property == "grid-template-areas") {
value %<>%
lapply(function(row) {
row %>% paste0(collapse = " ") %>% paste0("'", ., "'")
})
}
value %>% unlist() %>% paste0(collapse = " ")
}
#' Create media rule template
#'
#' @description
#' Creates a valid glue::glue string template for a css media query.
#' Used internally to generate a breakpoint specific wrapper.
#'
#' @param options The options for the required template. if no valid
#' values are given, a non media query template is created instead.
#'
#' @importFrom glue glue
#'
#' @return A valid glue::glue template string to be processed later.
#' @keywords utils internal
mediaRuleTemplate <- function(options) {
if (is.null(options$min) && is.null(options$max)) {
return("{{rules}}")
}
stringTemplate(
getOption("imola.settings")$string_templates$media_rule,
min = paste0(ifelse(
is.null(options$min), "", glue("and (min-width: {options$min}px) ")
)),
max = paste0(ifelse(
is.null(options$max), "", glue("and (max-width: {options$max}px) ")
)),
rules = "{{rules}}"
)
}
#' Import a settings file
#'
#' @description
#' Reads the content of a yaml settings file from the package directory.
#'
#' @param file The file name to read. Settings files are stored in the package
#' installation directory and include different settings and options.
#'
#' @importFrom yaml read_yaml
#' @importFrom magrittr "%>%"
#'
#' @return A list object containing the content of the settings yaml file
#' @keywords utils internal
readSettingsFile <- function(file) {
file %>%
paste0("settings/", ., ".yml") %>%
system.file(package = "imola") %>%
yaml::read_yaml()
}
#' Add object class
#'
#' @description
#' Adds a class to a object
#'
#' @param object Any R object.
#' @param class A string representing a object class.
#' @importFrom yaml read_yaml
#'
#' @importFrom magrittr "%<>%"
#'
#' @return The given R object with the additional class.
#' @keywords utils internal
addClass <- function(object, class) {
object %>% {
class(.) %<>%
append(class)
.
}
}