@@ -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)']])
665665colorscale_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
680694user_specified <- function (obj = NULL ) {
681695 if (! length(obj )) return (FALSE )
0 commit comments