Skip to content

Commit 80f98a2

Browse files
committed
clean-up comments; add more tests
1 parent 427bd9f commit 80f98a2

File tree

2 files changed

+39
-7
lines changed

2 files changed

+39
-7
lines changed

R/partial_bundles.R

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -32,8 +32,6 @@
3232
#'
3333

3434

35-
# TODO: implement type = 'auto' which would attempts to find the smallest partial bundle that can render `p`.
36-
# this would require, however, knowing the bundle -> trace mapping *for `p`'s plotly.js version*
3735
partial_bundle <- function(p, type = "auto", local = TRUE, minified = TRUE) {
3836

3937
if (!is.plotly(p)) stop("The first argument to `partial_bundle()` must be a plotly object", call. = FALSE)
@@ -45,6 +43,8 @@ partial_bundle <- function(p, type = "auto", local = TRUE, minified = TRUE) {
4543
p$dependencies[[idx]]$minified <- minified
4644
p$dependencies[[idx]]$partial_bundle <- match.arg(type, c("auto", "main", names(bundleTraceMap)))
4745

46+
# unfortunately, htmlwidgets doesn't appear to support manipulation of
47+
# dependencies field during preRenderHook(), so we need to explicitly build
4848
plotly_build(p)
4949
}
5050

@@ -69,8 +69,8 @@ verify_partial_bundle <- function(p) {
6969
}
7070

7171
if (identical(bundleType, "auto")) {
72-
message(
73-
"Couldn't find a single partial bundle that would support this plotly",
72+
warning(
73+
"Couldn't find a single partial bundle that would support this plotly ",
7474
"visualization. Using the main (full) bundle instead."
7575
)
7676
p$dependencies[[plotlyjsBundleIDX(p)]] <- plotlyMainBundle()
Lines changed: 35 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,14 @@
11
context("partial-bundles")
22

3-
4-
test_that("Can reduce saved file size with an auto partial bundle", {
3+
test_that("Can reduce saved file size with an basic (auto) partial bundle by half", {
4+
skip_on_cran()
55

66
p1 <- plot_ly(x = 1:10, y = 1:10) %>% add_markers()
77
p2 <- partial_bundle(p1)
8+
expect_match(plotlyjsBundle(p2)$name, "basic")
9+
810
f1 <- tempfile(fileext = ".html")
911
f2 <- tempfile(fileext = ".html")
10-
1112
file_size <- function(p, f) {
1213
owd <- setwd(dirname(f))
1314
on.exit(setwd(owd))
@@ -16,3 +17,34 @@ test_that("Can reduce saved file size with an auto partial bundle", {
1617
}
1718
expect_true(file_size(p1, f1) / 2 > file_size(p2, f2))
1819
})
20+
21+
test_that("Can find the right bundle", {
22+
skip_on_cran()
23+
24+
p1 <- plot_ly(z = ~volcano) %>% add_heatmap()
25+
p2 <- partial_bundle(p1)
26+
expect_match(plotlyjsBundle(p2)$name, "cartesian")
27+
28+
p3 <- plot_ly(z = ~volcano) %>% add_surface()
29+
p4 <- partial_bundle(p3)
30+
expect_match(plotlyjsBundle(p3)$name, "gl3d")
31+
32+
# At least right now, we don't support multiple partial bundles
33+
expect_warning(
34+
subplot(p1, p3) %>% partial_bundle(),
35+
"Using the main (full) bundle",
36+
fixed = TRUE
37+
)
38+
})
39+
40+
test_that("Can specify the partial bundle", {
41+
skip_on_cran()
42+
43+
p1 <- plot_ly(x = 1:10, y = 1:10) %>% add_markers()
44+
p2 <- partial_bundle(p1, type = "basic")
45+
p3 <- partial_bundle(p1, type = "cartesian")
46+
47+
expect_match(plotlyjsBundle(p2)$name, "basic")
48+
expect_match(plotlyjsBundle(p3)$name, "cartesian")
49+
50+
})

0 commit comments

Comments
 (0)