Skip to content

Commit 2f72838

Browse files
committed
Ensure size mapping always leads to array of appropriate length
1 parent 6aa3717 commit 2f72838

File tree

3 files changed

+24
-7
lines changed

3 files changed

+24
-7
lines changed

R/utils.R

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -497,12 +497,22 @@ verify_attr <- function(proposed, schema) {
497497
proposed$name <- uniq(proposed$name)
498498
}
499499

500-
# if marker.sizemode='area', make sure marker.size is boxed up
501-
# (otherwise, when marker.size is a constant, it always sets the diameter!)
500+
# if marker.size was populated via `size` arg (i.e., internal map_size()),
501+
# then it should _always_ be an array
502+
# of appropriate length...
503+
# (when marker.size is a constant, it always sets the diameter!)
502504
# https://codepen.io/cpsievert/pen/zazXgw
503505
# https://github.com/plotly/plotly.js/issues/2735
504-
if ("area" %in% proposed$marker$sizemode) {
505-
proposed$marker[["size"]] <- i(proposed$marker[["size"]])
506+
if (is.default(proposed$marker$size)) {
507+
s <- proposed$marker[["size"]]
508+
if (length(s) == 1) {
509+
# marker.size could be of length 1, but we may have multiple
510+
# markers -- in that case, if marker.size is an array
511+
# of length 1 will result in just one marker
512+
# https://codepen.io/cpsievert/pen/aMmOza
513+
n <- length(proposed[["x"]] %||% proposed[["y"]])
514+
proposed$marker[["size"]] <- default(i(rep(s, n)))
515+
}
506516
}
507517

508518
# do the same for "sub-attributes"

tests/testthat/test-plotly-size.R

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ test_that("sizemode is always respected", {
44

55
# https://github.com/ropensci/plotly/issues/1133
66
p <- plot_ly(mtcars, x = ~wt, y = ~wt, size = ~wt, color = ~as.factor(carb))
7-
d <- plotly_build(p)$x$data
7+
d <- expect_doppelganger_built(p, "sizemode")$data
88
expect_length(d, length(unique(mtcars$carb)))
99

1010
for (i in seq_along(d)) {
@@ -16,5 +16,11 @@ test_that("sizemode is always respected", {
1616
s <- d[[i]]$marker$size
1717
if (length(s) == 1) expect_is(s, "AsIs")
1818
}
19-
19+
})
20+
21+
22+
test_that("size maps correctly to marker.size", {
23+
p <- plot_ly(x = 1:10, y = 1:10, size = I(30))
24+
d <- expect_doppelganger_built(p, "marker.size")$data
25+
expect_equivalent(d[[1]]$marker$size, rep(30, 10))
2026
})

tests/testthat/test-plotly.R

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -192,12 +192,13 @@ test_that("Complex example works", {
192192
l <- expect_traces(p, 3, "time-series-summary")
193193
})
194194

195-
196195
test_that("span/size controls errorbar thickness/width", {
197196

198197
p <- plot_ly(x = 1:10, y = 1:10, error_x = list(value = 3), error_y = list(value = 2), span = I(5), size = I(10), stroke = I("black"), color = I("red")) %>%
199198
plotly_build()
200199

200+
expect_doppelganger_built(p, "errorbar.width")
201+
201202
d <- p$x$data
202203
expect_length(d, 1)
203204

0 commit comments

Comments
 (0)