-
Notifications
You must be signed in to change notification settings - Fork 633
/
Copy pathpartial_bundles.R
160 lines (141 loc) · 6.01 KB
/
partial_bundles.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
#' Use a partial bundle of plotly.js
#'
#' Leveraging plotly.js' partial bundles can lead to smaller file sizes
#' and faster rendering. The full list of available bundles, and the
#' trace types that they support, are available
#' [here](https://github.com/plotly/plotly.js/blob/master/dist/README.md#partial-bundles)
#'
#' @details WARNING: use this function with caution when rendering multiple
#' plotly graphs on a single website. That's because, if multiple plotly.js
#' bundles are used, the most recent bundle will override the other bundles.
#' See the examples section for an example.
#'
#' @param p a plotly object.
#' @param type name of the (partial) bundle. The default, `'auto'`, attempts to
#' find the smallest single bundle that can render `p`. If no single partial bundle
#' can render `p`, then the full bundle is used.
#' @param local whether or not to download the partial bundle so that it can be
#' viewed later without an internet connection.
#' @param minified whether or not to use a minified js file (non-minified file can be useful for debugging plotly.js)
#' @author Carson Sievert
#' @export
#' @examplesIf interactive() || !identical(.Platform$OS.type, "windows")
#'
#' # ----------------------------------------------------------------------
#' # This function is always safe to use when rendering a single
#' # plotly graph. In this case, we get a 3x file reduction.
#' # ----------------------------------------------------------------------
#'
#' \dontrun{
#' library(plotly)
#' p <- plot_ly(x = 1:10, y = 1:10) %>% add_markers()
#' save_widget <- function(p, f) {
#' owd <- setwd(dirname(f))
#' on.exit(setwd(owd))
#' htmlwidgets::saveWidget(p, f)
#' mb <- round(file.info(f)$size / 1e6, 3)
#' message("File is: ", mb," MB")
#' }
#' f1 <- tempfile(fileext = ".html")
#' f2 <- tempfile(fileext = ".html")
#' save_widget(p, f1)
#' save_widget(partial_bundle(p), f2)
#'
#' # ----------------------------------------------------------------------
#' # But, since plotly.js bundles override one another,
#' # be careful when putting multiple graphs in a larger document!
#' # Note how the surface (part of the gl3d bundle) renders, but the
#' # heatmap (part of the cartesian bundle) doesn't...
#' # ----------------------------------------------------------------------
#'
#' library(htmltools)
#' p1 <- plot_ly(z = ~volcano) %>%
#' add_heatmap() %>%
#' partial_bundle()
#' p2 <- plot_ly(z = ~volcano) %>%
#' add_surface() %>%
#' partial_bundle()
#' browsable(tagList(p1, p2))
#' }
#'
partial_bundle <- function(p, type = "auto", local = TRUE, minified = TRUE) {
if (!is.plotly(p)) stop("The first argument to `partial_bundle()` must be a plotly object", call. = FALSE)
# Amongst all the 'print-time' htmlwidget dependencies,
# find the plotly.js dependency and attach some meta-info
idx <- plotlyjsBundleIDX(p)
p$dependencies[[idx]]$local <- local
p$dependencies[[idx]]$minified <- minified
p$dependencies[[idx]]$partial_bundle <- match.arg(type, c("auto", "main", names(bundleTraceMap)))
# unfortunately, htmlwidgets doesn't appear to support manipulation of
# dependencies field during preRenderHook(), so we need to explicitly build
plotly_build(p)
}
verify_partial_bundle <- function(p) {
# return early if we're using the main bundle (the default)
currentBundle <- plotlyjsBundle(p)
bundleType <- currentBundle$partial_bundle %||% "main"
if (identical(bundleType, "main")) return(p)
# grab all the required trace types
types <- unique(vapply(p$x$data, function(x) x[["type"]] %||% "scatter", character(1)))
if (identical(bundleType, "auto")) {
# resolve an auto bundle by using the 1st bundle that supports all the types,
# except for the 'strict' bundle (ordering of bundleTraceMap is important!)
for (i in seq_along(bundleTraceMap[names(bundleTraceMap) != "strict"])) {
if (all(types %in% bundleTraceMap[[i]])) {
bundleType <- names(bundleTraceMap)[[i]]
break
}
}
if (identical(bundleType, "auto")) {
warning(
"Couldn't find a single partial bundle that would support this plotly ",
"visualization. Using the main (full) bundle instead."
)
p$dependencies[[plotlyjsBundleIDX(p)]] <- plotlyMainBundle()
return(p)
}
}
# verify that this partial bundle actually supports this viz
# (at this point, bundleType should never be 'auto' or 'main')
missingTypes <- setdiff(types, bundleTraceMap[[bundleType]])
if (length(missingTypes)) {
msg <- sprintf(
"The '%s' bundle supports the following trace types: '%s'.\n\n This plotly visualization contains the following trace types: '%s'",
bundleType, paste(bundleTraceMap[[bundleType]], collapse = "', '"), paste(missingTypes, collapse = "', '")
)
stop(msg, call. = FALSE)
}
idx <- plotlyjsBundleIDX(p)
bundle_name <- sprintf("plotly-%s", bundleType)
bundle_script <- sprintf(
"plotly-%s-%s.%sjs",
bundleType, currentBundle$version,
if (isTRUE(currentBundle$minified)) "min." else ""
)
p$dependencies[[idx]]$name <- bundle_name
p$dependencies[[idx]]$script <- bundle_script
p$dependencies[[idx]]$src <- list(href = "https://cdn.plot.ly")
# download the relevant bundle
if (isTRUE(p$dependencies[[idx]]$local)) {
# TODO: implement a caching mechanism?
try_library("curl", "partial_bundle")
tmpfile <- file.path(tempdir(), bundle_script)
p$dependencies[[idx]]$src$file <- dirname(tmpfile)
if (!file.exists(tmpfile)) {
curl::curl_download(paste0("https://cdn.plot.ly/", bundle_script), tmpfile)
}
# file src is no longer in plotly's path (it's a temp file)
p$dependencies[[idx]]$package <- NULL
}
p
}
plotlyjsBundleIDX <- function(p) {
depNames <- sapply(p$dependencies, "[[", "name")
bundleNames <- paste0("plotly-", c("main", "auto", names(bundleTraceMap)))
idx <- which(depNames %in% bundleNames)
if (length(idx) != 1) stop("Couldn't find the plotlyjs bundle")
idx
}
plotlyjsBundle <- function(p) {
p$dependencies[[plotlyjsBundleIDX(p)]]
}