/
utils.R
439 lines (365 loc) · 15.3 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
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
# ----------------------------------------------------------------------------
# Class assertion
# ----------------------------------------------------------------------------
# fiery server
is.fire <- function(x) inherits(x, "Fire")
# dependencies
is.dependency <- function(x) inherits(x, "dash_dependency")
is.output <- function(x) inherits(x, "output")
is.input <- function(x) inherits(x, "input")
is.state <- function(x) inherits(x, "state")
# components (TODO: this should be exported by dashRtranspile!)
is.component <- function(x) inherits(x, "dash_component")
# layout is really a special type of component
is.layout <- function(x) {
is.component(x) && identical(x[["props"]][["id"]], layout_container_id())
}
layout_container_id <- function() {
"_dashR-layout-container"
}
# retrieve the arguments of a callback function that are dash inputs
callback_inputs <- function(func) {
compact(lapply(formals(func), function(x) {
# missing arguments produce an error when evaluated
# TODO: should we only evaluate when `!identical(x, quote(expr = ))`?
val <- tryNULL(eval(x))
if (is.input(val)) val else NULL
}))
}
callback_states <- function(func) {
compact(lapply(formals(func), function(x) {
# missing arguments produce an error when evaluated
# TODO: should we only evaluate when `!identical(x, quote(expr = ))`?
val <- tryNULL(eval(x))
if (is.state(val)) val else NULL
}))
}
callback_events <- function(func) {
compact(lapply(formals(func), function(x) {
# missing arguments produce an error when evaluated
# TODO: should we only evaluate when `!identical(x, quote(expr = ))`?
val <- tryNULL(eval(x))
if (is.event(val)) val else NULL
}))
}
# search through a component (a recursive data structure) for a component with
# a given id and return the component's type
component_props_given_id <- function(component, id) {
is_component <- is.component(component)
is_match <- if (is_component) isTRUE(component$props$id == id) else FALSE
props <- if (is_match) component$propNames else NA
if (is_component && !is_match) {
if ("children" %in% names(component$props)) {
return(unlist(lapply(component$props$children, component_props_given_id, id)))
}
}
props
}
component_contains_type <- function(component, package, type) {
is_component <- is.component(component)
is_match <- if (is_component) isTRUE(component$type == type) && isTRUE(component$package == package) else FALSE
if (is_component && !is_match) {
if ("children" %in% names(component$props)) {
return(any(unlist(lapply(component$props$children, component_contains_type, package, type))))
}
}
is_match
}
# ----------------------------------------------------------------------
# HTTP helpers
# ----------------------------------------------------------------------
request_parse_json <- function(request) {
# request body must be parsed on demand (to avoid errors by odd formats)
# http://www.data-imaginist.com/2017/Introducing-reqres/
if (!request$is("json")) stop("Expected a JSON request", call. = FALSE)
# Unlike `reqres::default_parsers["application/json"]`, we don't
# simplify the *entire* JSON blob, but we do simplify input/state value(s)
# https://gist.github.com/cpsievert/04d53edbe902ca86a41949e24e8b4af7
from_JSON <- function(raw, directives) {
jsonlite::fromJSON(rawToChar(raw), simplifyVector = FALSE)
}
success <- request$parse(list(`application/json` = from_JSON))
if (!success) stop("Failed to parse body", call. = FALSE)
request
}
# ----------------------------------------------------------------------------
# HTML dependency helpers
# ----------------------------------------------------------------------------
# A shim for htmltools::renderDependencies
# @param dependencies a list of HTML dependencies
# @param external point to an external CDN rather local files?
render_dependencies <- function(dependencies, local = TRUE, prefix=NULL) {
html <- sapply(dependencies, function(dep) {
assertthat::assert_that(inherits(dep, "html_dependency"))
srcs <- names(dep[["src"]])
src <- if (!local && !"href" %in% srcs && "file" %in% srcs) {
message("No remote hyperlink found for HTML dependency '", dep[["name"]], "'. Using local file instead.")
"file"
} else if (local && !"file" %in% srcs && "href" %in% srcs) {
message("No local file found for HTML dependency '", dep[["name"]], "'. Using the remote hyperlink instead.")
"href"
} else if (!local) {
"href"
} else {
"file"
}
# According to Dash convention, label react and react-dom as originating
# in dash_renderer package, even though all three are currently served
# u p from the DashR package
if (dep$name %in% c("react", "react-dom")) {
dep$name <- "dash-renderer"
}
# The following lines inject _dash-component-suites into the src tags,
# as this is the current Dash convention. The dependency paths cannot
# be set solely at component library generation time, since hosted
# applications should have the app name injected as well.
#
# This is essentially analogous to this codeblock on the Python side:
# https://github.com/plotly/dash/blob/1249ffbd051bfb5fdbe439612cbec7fa8fff5ab5/dash/dash.py#L207
#
# Use the system file modification timestamp for the current
# package and add the version number of the package as a query
# parameter for cache busting
if (!is.null(dep$package)) {
# the gsub line is to remove stray duplicate slashes, to
# permit exact string matching on pathnames
dep_path <- gsub("//+",
"/",
paste(dep$src$file,
dep$script,
sep = "/")
)
full_path <- system.file(file.path(dep_path),
package = dep$package)
modified <- as.integer(file.mtime(full_path))
} else {
modified <- as.integer(Sys.time())
}
# we don't want to serve the JavaScript source maps here
if ("script" %in% names(dep) && tools::file_ext(dep[["script"]]) != "map") {
dep[["script"]] <- paste0(prefix,
"_dash-component-suites/",
dep$name,
"/",
basename(dep[["script"]]),
sprintf("?v=%s&m=%s", dep$version, modified))
html <- sprintf("<script src=\"%s\"></script>", dep[["script"]])
} else if ("stylesheet" %in% names(dep) & src == "href") {
html <- sprintf("<link href=\"%s\" rel=\"stylesheet\" />", paste(dep[["src"]][["href"]],
dep[["stylesheet"]],
sep="/"))
} else if ("stylesheet" %in% names(dep) & src == "file") {
html <- sprintf("<link href=\"%s\" rel=\"stylesheet\" />", file.path(dep[["src"]][["file"]],
dep[["stylesheet"]]))
}
})
paste(html, collapse = "\n")
}
# ----------------------------------------------------------------------------
# Other (generic) helpers
# ----------------------------------------------------------------------------
"%||%" <- function(x, y) {
if (length(x)) x else y
}
compact <- function(x) {
Filter(Negate(is.null), x)
}
# same as plotly:::to_JSON
to_JSON <- function(x, ...) {
jsonlite::toJSON(x, digits = 50, auto_unbox = TRUE, force = TRUE,
null = "null", na = "null", ...)
}
# same as plotly:::new_id
new_id <- function() {
basename(tempfile(""))
}
dir_exists <- function(paths) {
utils::file_test("-d", paths)
}
tryNULL <- function(expr) {
tryCatch(expr, error = function(e) NULL)
}
str_trim <- function(x) {
sub("\\s+$", "", sub("^\\s+", "", x))
}
setdiffsym <- function(x, y) {
setdiff(union(x, y), intersect(x, y))
}
welcome_page <- function() {
#dashHtmlComponents::htmlDiv(
# dashHtmlComponents::htmlH2("Welcome to dash!"),
# "If you see this message, you may not have yet specified a layout in your application."
#)
}
stop_report <- function(msg = "") {
stop(
msg, "\n\n",
"Please let us know about this error via ",
"https://github.com/plotly/dashR/issues/new",
call. = FALSE
)
}
try_library <- function(pkg, fun = NULL) {
if (system.file(package = pkg) != "") {
return(invisible())
}
stop("Package `", pkg, "` required", if (!is.null(fun))
paste0(" for `", fun, "`"), ".\n", "Please install and try again.",
call. = FALSE)
}
assert_valid_children <- function(children, ...) {
kids <- list(children)
if (...length()) {
pattern <- paste(paste0('^', ...), collapse = '|')
kids <- kids[!grepl(pattern, names2(kids))]
}
if (!length(kids)) return(NULL)
assert_no_names(kids)
}
append_wildcard_props <- function(component, children, ...) {
attrs <- list(children)
if (!length(children) || !...length()) return(component)
pattern <- paste(paste0('^', ...), collapse = '|')
attrs_wild <- attrs[grepl(pattern, names2(attrs))]
if (!length(attrs_wild)) return(component)
component[['props']] <- c(component[['props']] %||% list(), attrs_wild)
component[['propNames']] <- c(component[['propNames']], names(attrs_wild))
component
}
assert_no_names <- function (x)
{
if(!(is.list(x))) x <- list(x)
nms <- names(x)
if (is.null(nms))
return(x)
if (identical("", unique(nms)))
return(setNames(x, NULL))
stop(sprintf("Didn't recognize the following named arguments: '%s'",
paste(nms, collapse = "', '")), call. = FALSE)
}
filter_null <- function(x) {
if (length(x) == 0 || !is.list(x)) return(x)
x[!vapply(x, is.null, logical(1))]
}
assert_valid_callbacks <- function(output, params, func) {
inputs <- params[vapply(params, function(x) 'input' %in% attr(x, "class"), FUN.VALUE=logical(1))]
state <- params[vapply(params, function(x) 'state' %in% attr(x, "class"), FUN.VALUE=logical(1))]
invalid_params <- vapply(params, function(x) {
!any(c('input', 'state') %in% attr(x, "class"))
}, FUN.VALUE=logical(1))
# Verify that params contains no elements that are not either members of 'input' or 'state' classes
if (any(invalid_params)) {
stop(sprintf("Callback parameters must be inputs or states. Please verify formatting of callback parameters."), call. = FALSE)
}
# Verify that 'input' parameters always precede 'state', if present
if (!(valid_seq(params))) {
stop(sprintf("Strict ordering of callback handler parameters is required. Please ensure that input parameters precede all state parameters."), call. = FALSE)
}
# Assert that the component ID as passed is a string.
if(!(is.character(output$id) & !grepl("^\\s*$", output$id) & !grepl("\\.", output$id))) {
stop(sprintf("Callback IDs must be (non-empty) character strings that do not contain one or more dots/periods. Please verify that the component ID is valid."), call. = FALSE)
}
# Assert that user_function is a valid function
if(!(is.function(func))) {
stop(sprintf("The callback method's 'func' parameter requires a function as its argument. Please verify that 'func' is a valid, executable R function."), call. = FALSE)
}
# Check if inputs are a nested list
if(!(any(sapply(inputs, is.list)))) {
stop(sprintf("Callback inputs should be a nested list, in which each element of the sublist represents a component ID and its properties."), call. = FALSE)
}
# Check if state is a nested list, if the list is not empty
if(!(length(state) == 0) & !(any(sapply(state, is.list)))) {
stop(sprintf("Callback states should be a nested list, in which each element of the sublist represents a component ID and its properties."), call. = FALSE)
}
# Check that input is not NULL
if(is.null(inputs)) {
stop(sprintf("The callback method requires that one or more properly formatted inputs are passed."), call. = FALSE)
}
# Check that outputs are not inputs
# https://github.com/plotly/dash/issues/323
inputs_vs_outputs <- lapply(inputs, function(x) identical(x, output))
if(TRUE %in% inputs_vs_outputs) {
stop(sprintf("Circular input and output arguments were found. Please verify that callback outputs are not also input arguments."), call. = FALSE)
}
# TO DO: check that components contain props
TRUE
}
names2 <- function(x) names(x) %||% rep('', length(x))
valid_seq <- function(params) {
class_attr <- vapply(params, function(x) {
attr(x, "class")[attr(x, "class") %in% c('input', 'state')]
}, FUN.VALUE=character(1))
rle_result <- rle(class_attr)$values
if (identical(rle_result, 'input')) {
return(TRUE)
} else if (identical(rle_result, c('input', 'state'))) {
return(TRUE)
} else {
return(FALSE)
}
}
resolve_prefix <- function(prefix, environment_var) {
if (!(is.null(prefix))) {
assertthat::assert_that(is.character(prefix))
return(prefix)
} else {
prefix_env <- Sys.getenv(environment_var)
if (prefix_env != "") {
return(prefix_env)
} else {
return("/")
}
}
}
# The function below requires a dependency path, package information
# retrieved from a request URL, as well as a list of dependencies
# (currently in htmltools htmlDependency format). get_package_mapping
# optionally returns an R package name (if the file is contained
# inside an R package), or NULL if the dependency is not found,
# and a (local) path to the dependency.
#
# script_name is e.g. "dash_core_components.min.js"
# url_package is e.g. "dash_core_components"
# dependencies = list of htmlDependency objects
# this function returns a list with two elements:
# rpkg_name = character string supplying the name of the R package
# rpkg_path = character string providing the path to the dependency
get_package_mapping <- function(script_name, url_package, dependencies) {
# TODO: improve validation of dependency inputs, particularly
# to avoid duplicating dependencies in the package_map
package_map <- vapply(unique(dependencies), function(x) {
if (x$name %in% c('react', 'react-dom')) {
x$name <- 'dash-renderer'
}
dep_path <- paste(x$src$file,
x$script,
sep = "/")
# remove n>1 slashes and replace with / if present
result <- c(pkg_name=ifelse("package" %in% names(x), x$package, NULL),
dep_name=x$name,
dep_path=gsub("//+", replacement = "/", dep_path)
)
}, FUN.VALUE = character(3))
package_map <- t(package_map)
# pos_match is a vector of logical() values -- this allows filtering
# of the package_map entries based on name, path, and matching of
# URL package name against R package names. when all conditions are
# satisfied, pos_match will return TRUE
pos_match <- grepl(paste0(script_name, "$"), package_map[, "dep_path"]) &
grepl(url_package, package_map[,"dep_name"])
rpkg_name <- package_map[,"pkg_name"][pos_match]
rpkg_path <- package_map[,"dep_path"][pos_match]
return(list(rpkg_name=rpkg_name, rpkg_path=rpkg_path))
}
get_mimetype <- function(filename) {
# the tools package is available to all
filename_ext <- tools::file_ext(filename)
if (filename_ext == 'js')
return('application/JavaScript')
else if (filename_ext == 'css')
return('text/css')
else if (filename_ext == 'map')
return('application/json')
else
return(NULL)
}