/
rmarkdown.R
310 lines (287 loc) · 9.28 KB
/
rmarkdown.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
#' In-line rendering of an animated plot in an R Markdown document
#'
#' @param device The \link{animate} object.
#' @param ... Optional parameters to pass to \link{insert_animate}.
#'
#' @note This function should only be used in a code chunk of an R Markdown document.
#'
#' @examples
#' \donttest{
#' input <- tempfile(fileext = ".Rmd")
#' output <- tempfile(fileext = ".html")
#' writeLines('
#' ```{r, echo = FALSE, message = FALSE}
#' # Run / include the following in a code chunk of an R Markdown document
#' library(animate)
#' device <- animate$new(500, 500, virtual = TRUE) # set `virtual = TRUE` for R Markdown document
#' attach(device)
#'
#' # Data
#' id <- new_id(1:10)
#' s <- 1:10 * 2 * pi / 10
#' s2 <- sample(s)
#'
#' # Plot
#' par(xlim = c(-2.5, 2.5), ylim = c(-2.5, 2.5))
#' plot(2*sin(s), 2*cos(s), id = id)
#' points(sin(s2), cos(s2), id = id, transition = list(duration = 2000))
#'
#' # Render in-line in an R Markdown document
#' rmd_animate(device, click_to_play(start = 3)) # begin the plot at the third frame
#' ```
#' ```{r, echo = FALSE, message = FALSE}
#' par(xlim = NULL, ylim = NULL) # Reset `xlim` and `ylim` in `par`
#' # Do some other plots
#' off()
#' detach(device)
#' ```
#' ', input)
#' knitr::knit(input, output)
#' # browseURL(output)
#' }
#'
#' @export
rmd_animate <- function(device, ...) {
if (!device$virtual_meta$virtual) {
stop("Device is not virtual. Virtual device should be used for inline RMarkdown usage. Did you forget to set 'virtural = TRUE' in your `animate$new` function call?")
}
json_file <- tempfile(pattern = "test", fileext = ".json")
json_obj <- glue::glue('{
"plot_commands": <device$virtual_session$get()>,
"device": {
"selection": "", "id": "", "par": {},
"height": <device$virtual_meta$height>,
"width": <device$virtual_meta$width>
},
"max_num_commands": -1
}', .open = "<", .close = ">")
write(json_obj, json_file)
R.utils::gzip(json_file)
insert_animate(paste0(json_file, ".gz"), ...)
}
#' Insert an animated plot into an R Markdown document
#'
#' @param file The exported plot.
#' @param options A character string; the JavaScript to customise the playback options.
#' Two basic options `click_to_play()` and `click_to_loop()` have been implemented
#' for general usage.
#' @param style Optional style for the iframe that hosts the visualisation.
#' @param use_cdn TRUE / FALSE; if TRUE, serve the assets from a CDN, otherwise
#' embed the assets into the HTML.
#'
#' @examples
#' \donttest{
#' input <- tempfile(fileext = ".Rmd")
#' output <- tempfile(fileext = ".html")
#' writeLines('
#' ```{r, echo = FALSE, message = FALSE}
#' # Run / include the following in a code chunk of an R Markdown document
#' library(animate)
#' insert_animate(system.file("tests/Lorenz_system.json.gz", package = "animate"),
#' options = click_to_loop())
#' ```
#' ', input)
#' knitr::knit(input, output)
#' # browseURL(output)
#' }
#'
#' @export
insert_animate <- function(file, options = click_to_play(), style,
use_cdn = TRUE) {
dir0 <- tempdir()
asset <- function(x) system.file(x, package = "animate")
script <- function(x) as.character(to_shiny_tag(x))
# args <- list(...)
# if (!"style" %in% names(args)) {
device <- jsonlite::fromJSON(file)$device
if (missing(style)) {
style <- paste0("border: none;",
sprintf("width: %spx;", device$width + 50),
sprintf("height: %spx;", device$height + 50))
}
# args$style <- style
# }
asset_script <- if (use_cdn) {
script("https://cdn.jsdelivr.net/gh/kcf-jackson/animate@latest/inst/dist/animate.js")
} else {
script(asset("dist/animate.js"))
}
# Create data script
data_str <- read_file(compile_data(file))
# Create initialisation script
object_name <- filename_to_variable(basename(file))
init_str <- paste(
"JS_device = new plot2(-1);",
sprintf("JS_device.import(%s);", object_name),
options,
sep = "\n"
)
# Create html app
html_str <- paste(
"<!DOCTYPE html><html><head></head><body>",
asset_script,
sprintf("<script>%s</script>", data_str),
sprintf("<script>%s</script>", init_str),
"</body></html>",
sep = "\n"
)
shiny::tags$iframe(srcdoc = html_str, style = style)
}
#' Click an element to play a frame
#'
#' @description Playback option for the functions \link{rmd_animate}
#' and \link{insert_animate}.
#'
#' @param selector The ID of the DOM element.
#' @param start An integer; the number of frames to execute upon the beginning
#' of the visualisation. This is useful when one wants to start with some set-up
#' instead of an empty canvas.
#'
#' @examples
#' \donttest{
#' input <- tempfile(fileext = ".Rmd")
#' output <- tempfile(fileext = ".html")
#' writeLines('
#' ```{r, echo = FALSE, message = FALSE}
#' # Run / include the following in a code chunk of an R Markdown document
#' library(animate)
#' insert_animate(system.file("tests/basic_points.json", package = "animate"),
#' options = click_to_play())
#' ```
#' ', input)
#' knitr::knit(input, output)
#' # browseURL(output)
#' }
#'
#' @export
click_to_play <- function(selector = "#SVG_1", start = 2) {
sprintf(
'ctrl = new controller(JS_device);
ctrl.play_until(%s, 1, () => {
ctrl.watch("%s", "click", function() {
if (ctrl.player_pointer == 0) {
ctrl.play_until(%s, 1);
} else {
ctrl.play();
}
});
})',
start, selector, start
)
}
#' Click an element to play all frames
#'
#' @description Playback option for the functions \link{rmd_animate}
#' and \link{insert_animate}.
#'
#' @param selector The ID of the DOM element.
#' @param start An integer; the number of frames to execute upon the beginning
#' of the visualisation. This is useful when one wants to start with some set-up
#' instead of an empty canvas.
#' @param wait A number; the number of milliseconds to wait for before the
#' next frame is drawn.
#'
#' @examples
#' \donttest{
#' input <- tempfile(fileext = ".Rmd")
#' output <- tempfile(fileext = ".html")
#' writeLines('
#' ```{r, echo = FALSE, message = FALSE}
#' # Run / include the following in a code chunk of an R Markdown document
#' library(animate)
#' insert_animate(system.file("tests/Lorenz_system.json.gz", package = "animate"),
#' options = click_to_loop())
#' ```
#' ', input)
#' knitr::knit(input, output)
#' # browseURL(output)
#' }
#'
#' @export
click_to_loop <- function(selector = "#SVG_1", start = 2, wait = 20) {
sprintf(
'ctrl = new controller(JS_device);
ctrl.play_until(%s, 1, function() {
ctrl.watch("%s", "click", function callback() {
ctrl.unwatch("%s", "click", callback);
ctrl.play_until(0, %s, () => ctrl.watch("%s", "click", callback));
});
})',
start, selector, selector, wait, selector
)
}
#' Loop through the available frames n times
#'
#' @description Playback option for the functions \link{rmd_animate}
#' and \link{insert_animate}.
#'
#' @param times An integer; the number of times to loop.
#' @param wait A number; the number of milliseconds to wait for before the
#' next frame is drawn.
#'
#' @examples
#' \donttest{
#' input <- tempfile(fileext = ".Rmd")
#' output <- tempfile(fileext = ".html")
#' writeLines('
#' ```{r, echo = FALSE, message = FALSE}
#' # Run / include the following in a code chunk of an R Markdown document
#' library(animate)
#' insert_animate(system.file("tests/Lorenz_system.json.gz", package = "animate"),
#' options = loop(times = 2, wait = 15))
#' ```
#' ', input)
#' knitr::knit(input, output)
#' # browseURL(output)
#' }
#'
#' @export
loop <- function(times = 1, wait = 20) {
sprintf(
"ctrl = new controller(JS_device);
ctrl.loop(%s, %s);",
times, wait
)
}
# Convert JSON to JS ----------------------------------------------------------
compile_data <- function(input, output = tempfile(), ...) {
if (file.exists(output)) {
message(sprintf("The output data file '%s' already exists. I'll load it instead of re-compiling the input file.",
output))
return(output)
}
write(to_json(input, ...), file = output)
output
}
to_json <- function(input) {
fname <- basename(input)
sym <- filename_to_variable(fname)
if (tolower(tools::file_ext(fname)) == "gz") {
json <- jsonlite::toJSON(readBin(input, "int", n = 1e6, size = 1, signed = FALSE))
sprintf("const %s = JSON.parse(pako.inflate(%s, {to: 'string'}))", sym, json)
} else {
json <- paste(readLines(input), collapse = "\n")
glue::glue("const {sym} = {json}")
}
}
filename_to_variable <- function(x) {
gsub(x = x, pattern = "[.]|-", replacement = "_")
}
# Read a file into a string ---------------------------------------------------
read_file <- function(x) {
paste(readLines(x), collapse = "\n")
}
# Load JavaScript -------------------------------------------------------------
to_shiny_tag <- function(src, ...) {
if (is_web_link(src)) {
return(shiny::tags$script(src = src, ...))
}
if (is_local(src)) {
data_uri <- base64enc::dataURI(file = src, mime = "text/javascript")
return(shiny::tags$script(src = data_uri, ...))
}
stop("File '", basename(src), "' is not supported.")
}
is_web_link <- function(x) has_prefix(x, "http://") || has_prefix(x, "https://")
is_local <- function(x) !is_web_link(x)
has_prefix <- function(x, y) substr(x, 1, nchar(y)) == y