Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
quarto-dev
GitHub Repository: quarto-dev/quarto-cli
Path: blob/main/src/resources/rmd/ojs_static.R
12926 views
1
ojs_define <- function(...) {
2
# validate that we aren't in a cached chunk
3
cache <- knitr:::opts_current$get("cache")
4
if (is.numeric(cache) && cache > 0) {
5
stop(
6
"ojs_define() cannot be cached\n",
7
"Add cache: false to this cell's options to disable caching",
8
call. = FALSE
9
)
10
}
11
quos <- rlang::enquos(...)
12
vars <- rlang::list2(...)
13
nm <- names(vars)
14
if (is.null(nm)) {
15
nm <- rep_len("", length(vars))
16
}
17
contents <- jsonlite::toJSON(
18
list(
19
contents = I(mapply(
20
function(q, nm, val) {
21
# Infer name, if possible
22
if (nm == "") {
23
tryCatch(
24
{
25
nm <- rlang::as_name(q)
26
},
27
error = function(e) {
28
code <- paste(collapse = "\n", deparse(rlang::f_rhs(q)))
29
stop(
30
"ojs_define() could not create a name for the argument: ",
31
code
32
)
33
}
34
)
35
}
36
list(name = nm, value = val)
37
},
38
quos,
39
nm,
40
vars,
41
SIMPLIFY = FALSE,
42
USE.NAMES = FALSE
43
))
44
),
45
dataframe = "columns",
46
null = "null",
47
na = "null",
48
auto_unbox = TRUE,
49
digits = NA
50
)
51
script_string <- c(
52
"<script type=\"ojs-define\">",
53
contents,
54
"</script>"
55
)
56
invisible(
57
knitr::knit_meta_add(
58
list(structure(class = "ojs-define", script_string))
59
)
60
)
61
}
62
63