Skip to content

Commit 2650444

Browse files
committed
Merge pull request #417 from ropensci/fix/box
more accurate 'boxing' of data_array properties. fixes #415
2 parents aa8c7d7 + 02350bb commit 2650444

File tree

6 files changed

+73
-8
lines changed

6 files changed

+73
-8
lines changed

.travis.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ cache:
2424
before_script:
2525
- mkdir -p "$R_LIBS_USER"
2626
- git config --global user.email "[email protected]"
27-
- git config --global user.name "Carson Sievert"
27+
- git config --global user.name "cpsievert"
2828
- echo "Sys.setenv('plotly_username' = 'cpsievert')" > ~/.Rprofile
2929
- git clone https://github.com/cpsievert/plotly-test-table.git ../plotly-test-table
3030
- "wget -q -O - https://github.com/yihui/crandalf/raw/master/inst/scripts/install-pandoc | bash"

DESCRIPTION

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
Package: plotly
22
Title: Create Interactive Web Graphics via Plotly's JavaScript Graphing Library
3-
Version: 2.3.1
3+
Version: 2.3.2
44
Authors@R: c(person("Carson", "Sievert", role = c("aut", "cre"),
55
email = "[email protected]"),
66
person("Chris", "Parmer", role = c("aut", "cph"),

NEWS

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
1+
2.3.2 -- 25 Jan 2015
2+
3+
More accurate list of data_array properties. Fixes #415
4+
15
2.3.1 -- 25 Jan 2015
26

37
More accurate conversion of path width. Fixes #373.

R/utils.R

Lines changed: 39 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -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-
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)
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+
132165
rm_asis <- function(x) {
133166
# jsonlite converts NULL to {} and NA to null (plotly prefers null to {})
134167
# https://github.com/jeroenooms/jsonlite/issues/29

tests/testthat/test-ggplot-errorbar.R

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,3 +16,22 @@ test_that("geom_errorbar gives errorbars", {
1616
# right data for errorbar ymax
1717
expect_equal(L$data[[1]]$error_y$array, c(3.74, 1.26, 1.15))
1818
})
19+
20+
df <- data.frame(
21+
trt = factor(c(1, 1, 2, 2)),
22+
resp = c(1, 5, 3, 4),
23+
group = factor(c(1, 2, 3, 4)),
24+
upper = c(1.1, 5.3, 3.3, 4.2),
25+
lower = c(0.8, 4.6, 2.4, 3.6)
26+
)
27+
28+
p <- ggplot(df, aes(trt, resp, colour = group))
29+
g <- p + geom_errorbar(aes(ymin = lower, ymax = upper))
30+
31+
test_that("geom_errorbar boxes an array of length 1", {
32+
L <- save_outputs(g, "errorbar-unique-groups")
33+
expect_true(inherits(L$data[[1]]$error_y$array, "AsIs"))
34+
expect_true(inherits(L$data[[1]]$error_y$arrayminus, "AsIs"))
35+
})
36+
37+
# TODO fix and add a test for width of errorbars

tests/testthat/test-plotly.R

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -111,3 +111,12 @@ test_that("inheriting properties works as expected", {
111111
expect_equal(l$data[[2]]$opacity, 0.5)
112112
expect_true(all(l$data[[1]]$y > l$data[[2]]$y))
113113
})
114+
115+
test_that("x/y/z properties have a class of AsIs", {
116+
p <- plot_ly(x = 1, y = 1, z = 1, type = "scatter3d")
117+
l <- plotly_build(p)
118+
tr <- l$data[[1]]
119+
expect_true(inherits(tr$x, "AsIs"))
120+
expect_true(inherits(tr$y, "AsIs"))
121+
expect_true(inherits(tr$z, "AsIs"))
122+
})

0 commit comments

Comments
 (0)