Skip to content

Commit 2055abd

Browse files
committed
more careful coercion of bare list of length 2
1 parent c6f96f4 commit 2055abd

File tree

2 files changed

+26
-19
lines changed

2 files changed

+26
-19
lines changed

R/utils.R

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -661,7 +661,7 @@ verify_colorscale <- function(p) {
661661

662662
# Coerce `x` into a data structure that can map to a colorscale attribute.
663663
# Note that colorscales can either be the name of a scale (e.g., 'Rainbow') or
664-
# a 2D array (e.g., [[0, 'rgb(0,0,255)', [1, 'rgb(255,0,0)']])
664+
# a 2D array (e.g., [[0, 'rgb(0,0,255)'], [1, 'rgb(255,0,0)']])
665665
colorscale_json <- function(x) {
666666
if (!length(x)) return(x)
667667
if (is.character(x)) return(x)
@@ -671,11 +671,25 @@ colorscale_json <- function(x) {
671671
x[, 1] <- as.numeric(x[, 1])
672672
}
673673
if (is.list(x) && length(x) == 2) {
674-
x <- setNames(as.data.frame(x), NULL)
674+
# ensure a list like this: list(list(0, 0.5, 1), list("red", "white", "blue"))
675+
# converts to the correct dimensions
676+
if (!is.data.frame(x) && can_be_numeric(x[[1]])) {
677+
x <- data.frame(
678+
val = as.numeric(x[[1]]),
679+
col = as.character(x[[2]]),
680+
stringsAsFactors = FALSE
681+
)
682+
}
683+
x <- setNames(x, NULL)
675684
}
676685
x
677686
}
678687

688+
can_be_numeric <- function(x) {
689+
x <- suppressWarnings(as.numeric(x))
690+
sum(is.na(x)) == 0
691+
}
692+
679693
# if an object (e.g. trace.marker) contains a non-default attribute, it has been user-specified
680694
user_specified <- function(obj = NULL) {
681695
if (!length(obj)) return(FALSE)

tests/testthat/test-plotly-colorscale.R

Lines changed: 10 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ test_that("Can specify contour colorscale", {
3939
y = 1:10,
4040
marker = list(
4141
color = 1:10,
42-
colorscale = colorScale,
42+
colorscale = colorscale,
4343
showscale = TRUE
4444
)
4545
)
@@ -53,22 +53,15 @@ test_that("Can specify contour colorscale", {
5353
test_df <- plot_colorscale(as.data.frame(colorScale))
5454
test_matrix <- plot_colorscale(as.matrix(as.data.frame(colorScale)))
5555

56-
})
57-
58-
59-
60-
test_that("marker.colorscale overrides color", {
61-
plot_ly(
62-
x = c(-9, -6, -5, -3, -1),
63-
y = c(0, 1, 4, 5, 7),
64-
color = 1:5,
65-
marker = list(
66-
#color = 6:10,
67-
colorscale='Rainbow',
68-
showscale = TRUE
69-
)
70-
)
71-
l <- expect_doppelganger_built(p, "marker.colorscale")
56+
expect_doppelganger_built(test_list, "test_list")
57+
expect_doppelganger_built(test_df, "test_df")
58+
expect_doppelganger_built(test_matrix, "test_matrix")
59+
60+
test_list_2 <- plot_colorscale(list(list(0, "rgb(0,0,255)"), list(1, "rgb(0,255,0)")))
61+
test_list_3 <- plot_colorscale(list(list(0, 1), list("rgb(0,0,255)", "rgb(0,255,0)")))
62+
63+
expect_doppelganger_built(test_list_2, "test_list_2")
64+
expect_doppelganger_built(test_list_3, "test_list_3")
7265
})
7366

7467

0 commit comments

Comments
 (0)