Skip to content

Commit d430b5e

Browse files
authored
Merge pull request #1480 from ropensci/marker-size-fix
Ensure size mapping always leads to array of appropriate length
2 parents 6aa3717 + a73f527 commit d430b5e

File tree

4 files changed

+31
-10
lines changed

4 files changed

+31
-10
lines changed

R/utils.R

+14-4
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"]] %||% proposed[["lat"]] %||% proposed[["lon"]])
514+
proposed$marker[["size"]] <- default(i(rep(s, n)))
515+
}
506516
}
507517

508518
# do the same for "sub-attributes"

tests/testthat/test-plotly-sf.R

+6-3
Original file line numberDiff line numberDiff line change
@@ -240,14 +240,16 @@ test_that("sizing constants", {
240240
p <- plot_mapbox(mn_pts, size = I(30)) %>% plotly_build()
241241
d <- p$x$data
242242
expect_length(d, 1)
243-
expect_true(d[[1]]$marker$size == 30)
243+
expect_length(d[[1]]$marker$size, nrow(mn_pts))
244+
expect_true(all(d[[1]]$marker$size == 30))
244245
expect_true(d[[1]]$marker$sizemode == "area")
245246

246247
# span controls marker.line.width
247248
p <- plot_ly(mn_pts, size = I(30), span = I(10), stroke = I("black")) %>% plotly_build()
248249
d <- p$x$data
249250
expect_length(d, 1)
250-
expect_true(d[[1]]$marker$size == 30)
251+
expect_length(d[[1]]$marker$size, nrow(mn_pts))
252+
expect_true(all(d[[1]]$marker$size == 30))
251253
expect_true(d[[1]]$marker$sizemode == "area")
252254
expect_true(d[[1]]$marker$line$width == 10)
253255
expect_true(d[[1]]$marker$line$color == toRGB("black"))
@@ -256,7 +258,8 @@ test_that("sizing constants", {
256258
p <- plot_ly(mn_pts, size = I(20), error_x = list(value = 5)) %>% plotly_build()
257259
d <- p$x$data
258260
expect_length(d, 1)
259-
expect_true(d[[1]]$marker$size == 20)
261+
expect_length(d[[1]]$marker$size, nrow(mn_pts))
262+
expect_true(all(d[[1]]$marker$size == 20))
260263
expect_true(d[[1]]$marker$sizemode == "area")
261264
expect_true(d[[1]]$error_x$value == 5)
262265
expect_true(d[[1]]$error_x$width == 20)

tests/testthat/test-plotly-size.R

+9-2
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,12 @@ 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_length(d[[1]]$marker$size, 10)
26+
expect_true(all(d[[1]]$marker$size == 30))
2027
})

tests/testthat/test-plotly.R

+2-1
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)