@@ -112,23 +112,56 @@ from_JSON <- function(x, ...) {
112112 jsonlite :: fromJSON(x , simplifyDataFrame = FALSE , simplifyMatrix = FALSE , ... )
113113}
114114
115- # plotlyjs properties that must _always_ be an array (even if length 1)
116- get_boxed <- function () {
117- c(" x" , " y" , " lat" , " lon" , " text" )
118- }
119-
120115add_boxed <- function (x ) {
121116 for (i in seq_along(x $ data )) {
122117 # some object keys require an array, even if length one
123118 # one way to ensure atomic vectors of length 1 are not automatically unboxed,
124119 # by to_JSON(), is to attach a class of AsIs (via I())
125120 d <- x $ data [[i ]]
126- idx <- names(d ) %in% get_boxed() & sapply(d , length ) == 1
121+ idx <- names(d ) %in% get_boxed(d $ type % || % " scatter " ) & sapply(d , length ) == 1
127122 if (any(idx )) x $ data [[i ]][idx ] <- lapply(d [idx ], I )
123+ # (safely) mark individual nested properties
124+ x $ data [[i ]]$ error_x $ array <- i(d $ error_x $ array )
125+ x $ data [[i ]]$ error_y $ array <- i(d $ error_y $ array )
126+ x $ data [[i ]]$ error_x $ arrayminus <- i(d $ error_x $ arrayminus )
127+ x $ data [[i ]]$ error_y $ arrayminus <- i(d $ error_y $ arrayminus )
128128 }
129129 x
130130}
131131
132+ # plotlyjs properties that must _always_ be an array (even if length 1)
133+ get_boxed <- function (type = " scatter" ) {
134+ # if the trace type isn't found, provide some sensible defaults
135+ boxers [[type ]] %|| % c(" x" , " y" , " z" , " lat" , " lon" , " text" , " locations" )
136+ }
137+
138+ # if this ever needs updating see
139+ # https://github.com/ropensci/plotly/issues/415#issuecomment-173353138
140+ boxers <- list (
141+ choropleth = c(" locations" , " z" , " text" ),
142+ box = c(" x" , " y" ),
143+ heatmap = c(" z" , " text" ),
144+ histogram = c(" x" , " y" ),
145+ histogram2d = c(" z" , " color" ),
146+ mesh3d = c(" x" , " y" , " z" , " i" , " j" , " k" , " intensity" , " vertexcolor" , " facecolor" ),
147+ # TODO: what to do about marker.colors?
148+ pie = c(" labels" , " values" , " text" ),
149+ scatter = c(" x" , " y" , " r" , " t" ),
150+ scatter3d = c(" x" , " y" , " z" ),
151+ scattergeo = c(" lon" , " lat" , " locations" ),
152+ surface = c(" x" , " y" , " z" , " text" )
153+ )
154+
155+ i <- function (x ) {
156+ if (is.null(x )) {
157+ return (NULL )
158+ } else if (length(x ) == 1 ) {
159+ return (I(x ))
160+ } else {
161+ return (x )
162+ }
163+ }
164+
132165rm_asis <- function (x ) {
133166 # jsonlite converts NULL to {} and NA to null (plotly prefers null to {})
134167 # https://github.com/jeroenooms/jsonlite/issues/29
0 commit comments