Skip to content

Commit 887372e

Browse files
committed
more accurate list of data_array properties. fixes #415
1 parent 1530880 commit 887372e

File tree

1 file changed

+24
-6
lines changed

1 file changed

+24
-6
lines changed

R/utils.R

Lines changed: 24 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -112,23 +112,41 @@ 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-
120115
add_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)
128123
}
129124
x
130125
}
131126

127+
# plotlyjs properties that must _always_ be an array (even if length 1)
128+
get_boxed <- function(type = "scatter") {
129+
boxers[[type]]
130+
}
131+
132+
# if this ever needs updating see
133+
# https://github.com/ropensci/plotly/issues/415#issuecomment-173353138
134+
boxers <- list(
135+
choropleth = c("locations", "z", "text"),
136+
box = c("x", "y"),
137+
heatmap = c("z", "text"),
138+
histogram = c("x", "y"),
139+
histogram2d = c("z", "color"),
140+
mesh3d = c("x", "y", "z", "i", "j", "k", "intensity", "vertexcolor", "facecolor"),
141+
# TODO: what to do about marker.colors?
142+
pie = c("labels", "values", "text"),
143+
scatter = c("x", "y", "r", "t"),
144+
scatter3d = c("x", "y", "z"),
145+
scattergeo = c("lon", "lat", "locations"),
146+
surface = c("x", "y", "z", "text")
147+
)
148+
149+
132150
rm_asis <- function(x) {
133151
# jsonlite converts NULL to {} and NA to null (plotly prefers null to {})
134152
# https://github.com/jeroenooms/jsonlite/issues/29

0 commit comments

Comments
 (0)