|
2 | 2 | #' |
3 | 3 | #' Leveraging plotly.js' partial bundles can lead to smaller file sizes |
4 | 4 | #' and faster rendering. The full list of available bundles, and the |
5 | | -#' trace types that they support, are available [here](https://github.com/plotly/plotly.js/blob/master/dist/README.md) |
| 5 | +#' trace types that they support, are available |
| 6 | +#' [here](https://github.com/plotly/plotly.js/blob/master/dist/README.md#partial-bundles) |
| 7 | +#' |
| 8 | +#' @details WARNING: use this function with caution when rendering multiple |
| 9 | +#' plotly graphs on a single website. That's because, if multiple plotly.js |
| 10 | +#' bundles are used, the most recent bundle will override the other bundles. |
| 11 | +#' See the examples section for an example. |
6 | 12 | #' |
7 | 13 | #' @param p a plotly object. |
8 | 14 | #' @param type name of the (partial) bundle. The default, `'auto'`, attempts to |
9 | | -#' find the smallest single bundle that can render `p`. If no single partial bundle, |
| 15 | +#' find the smallest single bundle that can render `p`. If no single partial bundle |
10 | 16 | #' can render `p`, then the full bundle is used. |
11 | 17 | #' @param local whether or not to download the partial bundle so that it can be |
12 | 18 | #' viewed later without an internet connection. |
13 | 19 | #' @param minified whether or not to use a minified js file (non-minified file can be useful for debugging plotly.js) |
14 | 20 | #' @author Carson Sievert |
15 | 21 | #' @export |
16 | 22 | #' @examples |
| 23 | +#' |
| 24 | +#' # ---------------------------------------------------------------------- |
| 25 | +#' # This function is always safe to use when rendering a single |
| 26 | +#' # plotly graph. In this case, we get a 3x file reduction. |
| 27 | +#' # ---------------------------------------------------------------------- |
17 | 28 | #' |
18 | 29 | #' library(plotly) |
19 | | -#' p1 <- plot_ly(x = 1:10, y = 1:10) %>% add_markers() |
20 | | -#' p2 <- partial_bundle(p1) |
21 | | -#' f1 <- tempfile(fileext = ".html") |
22 | | -#' f2 <- tempfile(fileext = ".html") |
23 | | -#' |
| 30 | +#' p <- plot_ly(x = 1:10, y = 1:10) %>% add_markers() |
24 | 31 | #' save_widget <- function(p, f) { |
25 | 32 | #' owd <- setwd(dirname(f)) |
26 | 33 | #' on.exit(setwd(owd)) |
27 | 34 | #' htmlwidgets::saveWidget(p, f) |
28 | 35 | #' mb <- round(file.info(f)$size / 1e6, 3) |
29 | 36 | #' message("File is: ", mb," MB") |
30 | 37 | #' } |
31 | | -#' save_widget(p1, f1) |
32 | | -#' save_widget(p2, f2) |
| 38 | +#' f1 <- tempfile(fileext = ".html") |
| 39 | +#' f2 <- tempfile(fileext = ".html") |
| 40 | +#' save_widget(p, f1) |
| 41 | +#' save_widget(partial_bundle(p), f2) |
| 42 | +#' |
| 43 | +#' # ---------------------------------------------------------------------- |
| 44 | +#' # But, since plotly.js bundles override one another, |
| 45 | +#' # be careful when putting multiple graphs in a larger document! |
| 46 | +#' # Note how the surface (part of the gl3d bundle) renders, but the |
| 47 | +#' # heatmap (part of the cartesian bundle) doesn't... |
| 48 | +#' # ---------------------------------------------------------------------- |
| 49 | +#' |
| 50 | +#' library(htmltools) |
| 51 | +#' p1 <- plot_ly(z = ~volcano) %>% |
| 52 | +#' add_heatmap() %>% |
| 53 | +#' partial_bundle() |
| 54 | +#' p2 <- plot_ly(z = ~volcano) %>% |
| 55 | +#' add_surface() %>% |
| 56 | +#' partial_bundle() |
| 57 | +#' browsable(tagList(p1, p2)) |
33 | 58 | #' |
34 | 59 |
|
35 | 60 |
|
|
0 commit comments