-
Notifications
You must be signed in to change notification settings - Fork 633
/
Copy pathtest-plotly.R
350 lines (296 loc) · 12.2 KB
/
test-plotly.R
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
context("plotly")
expect_traces <- function(p, n.traces, name){
stopifnot(is.numeric(n.traces))
L <- expect_doppelganger_built(p, paste0("plotly-", name))
expect_equivalent(length(L$data), n.traces)
L
}
# expect 2 plotly graphs to have the same traces
expect_same_data <- function(p1, p2) {
if (!is.plotly(p1) || !is.plotly(p2)) {
stop("Both arguments must be plotly objects", call. = FALSE)
}
d1 <- plotly_build(p1)$x$data
d2 <- plotly_build(p2)$x$data
if (length(d1) != length(d2)) {
stop("Number of traces is different.", call. = FALSE)
}
# for each trace, align the names (since ordering doesn't matter)
d1 <- Map(function(x, y) structure(x[names(y)], class = oldClass(x)), d1, d2)
expect_identical(d1, d2)
}
test_that("vector values with repeated values are returned verbatim", {
p <- plot_ly(x = c(1, 2), y = c(1, 1))
l <- plotly_build(p)$x
expect_equivalent(l$data[[1]]$x, c(1, 2))
expect_equivalent(l$data[[1]]$y, c(1, 1))
})
test_that("plot_ly defaults to scatterplot", {
p1 <- plot_ly(mtcars, x = ~wt, y = ~mpg)
p2 <- plot_ly(mtcars, x = ~wt, y = ~mpg) %>% add_markers()
expect_same_data(p1, p2)
})
test_that("Variable mappings return same result regardless of where they appear", {
p1 <- plot_ly(mtcars, x = ~wt, y = ~mpg, size = ~disp)
p2 <- plot_ly(mtcars, x = ~wt, y = ~mpg) %>% add_markers(size = ~disp)
expect_same_data(p1, p2)
p1 <- plot_ly(mtcars, x = ~wt, y = ~mpg, color = ~disp)
p2 <- plot_ly(mtcars, x = ~wt, y = ~mpg) %>% add_markers(color = ~disp)
expect_same_data(p1, p2)
p1 <- plot_ly(mtcars, x = ~wt, y = ~mpg, symbol = ~factor(am))
p2 <- plot_ly(mtcars, x = ~wt, y = ~mpg) %>% add_markers(symbol = ~factor(am))
expect_same_data(p1, p2)
p1 <- plot_ly(mtcars, x = ~wt, y = ~mpg, linetype = ~factor(am))
p2 <- plot_ly(mtcars, x = ~wt, y = ~mpg) %>% add_markers(linetype = ~factor(am))
expect_message(plotly_build(p1), "Adding lines to mode")
expect_message(plotly_build(p2), "Adding lines to mode")
expect_same_data(p1, p2)
})
test_that("plot_ly() handles a simple scatterplot", {
p <- plot_ly(data = iris, x = ~Sepal.Length, y = ~Petal.Length, mode = "markers")
l <- expect_traces(p, 1, "scatterplot")
expect_equivalent(l$data[[1]]$mode, "markers")
expect_equivalent(l$data[[1]]$x, iris$Sepal.Length)
expect_equivalent(l$data[[1]]$y, iris$Petal.Length)
expect_true(l$layout$xaxis$title == "Sepal.Length")
expect_true(l$layout$yaxis$title == "Petal.Length")
expect_true(l$layout$xaxis$automargin)
expect_true(l$layout$yaxis$automargin)
})
test_that("type inference + add_data + layering works as expected", {
p <- plot_ly(iris, x = ~Species) %>%
add_trace(opacity = 0.3) %>%
add_data(iris[sample(nrow(iris), 10), ]) %>%
add_trace() %>%
layout(barmode = "overlay")
l <- expect_traces(p, 2, "bar-inference")
types <- unique(unlist(lapply(l$data, "[[", "type")))
expect_equivalent(types, "histogram")
expect_equivalent(l$data[[1]]$opacity, 0.3)
expect_equivalent(l$layout$barmode, "overlay")
expect_true(length(l$data[[1]]$x) > length(l$data[[2]]$x))
})
test_that("x/y/z properties have a class of AsIs", {
p <- plot_ly(x = 1, y = 1, z = 1, type = "scatter3d")
l <- expect_traces(p, 1, "box-data-array")
tr <- l$data[[1]]
expect_true(inherits(tr$x, "AsIs"))
expect_true(inherits(tr$y, "AsIs"))
expect_true(inherits(tr$z, "AsIs"))
})
test_that("grouping within multiples traces works", {
g <- expand.grid(visit = 1:2, id = 1:3, cohort = c("A", "B"))
g$response <- rnorm(nrow(g))
d <- group_by(g, id)
p <- plot_ly(d, x = ~visit, y = ~response, color = ~cohort, colors = c("red", "blue"))
l <- expect_traces(add_lines(p), 2, "group-within-trace")
expect_equivalent(l$data[[1]]$x, c(1, 2, NA, 1, 2, NA, 1, 2))
expect_equivalent(l$data[[2]]$x, c(1, 2, NA, 1, 2, NA, 1, 2))
expect_true(l$data[[1]]$line$color == toRGB("red"))
expect_true(l$data[[2]]$line$color == toRGB("blue"))
})
test_that("Alpha can be applied to both constant and scaled colors", {
p <- plot_ly(x = rnorm(100), y = rnorm(100), color = ~rnorm(100))
p <- add_markers(p, alpha = 0.05)
p <- add_lines(p, x = -1:1, y = -1:1, color = I("red"), alpha = 0.4)
# one trace for the colorbar
l <- expect_traces(p, 3, "alpha-blending")
# verify the correct alpha for the points
rgbs <- l$data[[1]]$marker$colorscale[, 2]
alphas <- unique(sub("\\)", "", sapply(strsplit(rgbs, ","), "[[", 4)))
expect_equivalent("0.05", alphas)
# verify the correct alpha for the lines
rgb <- l$data[[2]]$line$color
alpha <- sub("\\)", "", sapply(strsplit(rgb, ","), "[[", 4))
expect_equivalent("0.4", alpha)
})
test_that("Alpha still applies when no color is applied", {
p <- plot_ly(mtcars, x = ~mpg, y = ~disp, alpha = 0.5)
l <- expect_traces(p, 1, "alpha-no-color")
# verify the correct alpha for the points
expect_true(l$data[[1]]$marker$color == "rgba(31,119,180,0.5)")
})
test_that("Factors correctly mapped to a positional axis", {
x <- factor(c(1, 2, 4, 8, 16, 32))
p <- plot_ly(x = x, y = c(1, 2, 3, 4, 5, 6)) %>% add_markers()
l <- expect_traces(p, 1, "factor-axis")
expect_equivalent(l$layout$xaxis$type, "category")
expect_equivalent(l$layout$xaxis$categoryorder, "array")
expect_equivalent(l$layout$xaxis$categoryarray, levels(x))
})
test_that("Character strings correctly mapped to a positional axis", {
# scramble alphabet order
letters <- LETTERS[as.numeric(sort(as.character(1:26)))]
p <- plot_ly(x = letters, y = seq_along(letters)) %>%
add_bars(color = rep(c("a1", "a2"), length.out = 26))
l <- expect_traces(p, 2, "character-axis")
expect_equivalent(l$layout$xaxis$type, "category")
expect_equivalent(l$layout$xaxis$categoryorder, "array")
expect_equivalent(l$layout$xaxis$categoryarray, LETTERS)
})
test_that("Histogram", {
p <- plot_ly(x = rnorm(100))
l <- expect_traces(p, 1, "histogram")
o <- unlist(lapply(l$data, "[[", "orientation"))
types <- unlist(lapply(l$data, "[[", "type"))
expect_null(o)
expect_equivalent(unique(types), "histogram")
})
test_that("Discrete variable mapped to x creates horizontal bar chart", {
p <- plot_ly(y = rnorm(100))
l <- expect_traces(p, 1, "histogram-vert")
o <- unlist(lapply(l$data, "[[", "orientation"))
types <- unlist(lapply(l$data, "[[", "type"))
expect_equivalent(unique(o), "h")
expect_equivalent(unique(types), "histogram")
})
test_that("Can avoid inheriting attributes", {
p <- plot_ly(mtcars, x = ~wt, y = ~mpg, color = I("red")) %>%
add_histogram(x = ~factor(vs), inherit = FALSE)
l <- expect_traces(p, 1, "inherit-FALSE")
expect_equivalent(l$data[[1]][["type"]], "histogram")
expect_equivalent(l$data[[1]][["x"]], factor(mtcars[["vs"]]))
expect_null(l$data[[1]][["y"]])
expect_true(l$data[[1]][["marker"]][["color"]] != toRGB("red"))
})
test_that("Complex example works", {
# note how median (the variable) doesn't exist in the second layer
p <- txhousing %>%
plot_ly(x = ~date, y = ~median) %>%
group_by(city) %>%
add_lines(alpha = 0.2, name = "Texan Cities", hoverinfo = "none") %>%
group_by(date) %>%
summarise(
q1 = quantile(median, 0.25, na.rm = TRUE),
m = median(median, na.rm = TRUE),
q3 = quantile(median, 0.75, na.rm = TRUE)
) %>%
add_lines(y = ~m, color = I("red"), name = "median") %>%
add_ribbons(ymin = ~q1, ymax = ~q3, color = I("red"), name = "IQR")
l <- expect_traces(p, 3, "time-series-summary")
})
test_that("span/size controls errorbar thickness/width", {
p <- plot_ly(x = 1:10, y = 1:10, error_x = list(value = 3), error_y = list(value = 2), span = I(5), size = I(10), stroke = I("black"), color = I("red")) %>%
plotly_build()
expect_doppelganger_built(p, "errorbar.width")
d <- p$x$data
expect_length(d, 1)
expect_true(d[[1]]$error_x$value == 3)
expect_true(d[[1]]$error_x$thickness == 5)
expect_true(d[[1]]$error_x$width == 10)
expect_true(d[[1]]$error_x$color == toRGB("red"))
expect_true(d[[1]]$error_y$value == 2)
expect_true(d[[1]]$error_y$thickness == 5)
expect_true(d[[1]]$error_y$width == 10)
expect_true(d[[1]]$error_y$color == toRGB("red"))
})
test_that("Vector of redundant text is reduced to string when hoveron=fills", {
# see https://github.com/ropensci/plotly/issues/1233
d <- data.frame(
AA = c(2,3,3,2, NA, 6,7,7,6, NA),
BB = c(2,2,3,2, NA, 6,6,7,6, NA),
CC = c(rep('abc', 5), rep('xyz', 5)),
LL = c(rep('A', 5), rep('B', 5))
)
p <- plot_ly(d) %>%
add_trace(x = ~AA,
y = ~BB,
text = ~paste('<br> <b>Example</b> of <em>custom</em> hover text <br>', LL, '<br>', CC, '<br>.'),
split = ~LL,
mode = "lines",
fill = "toself",
hoveron = 'fills',
type = "scatter",
color = I(c(rep(toRGB("black", 1), 5),
rep(toRGB("red", 1), 5)))
)
b <- plotly_build(p)
d <- b$x$data
expect_length(d, 2)
expect_true(d[[1]]$line$color == toRGB("black"))
expect_true(d[[1]]$fillcolor == toRGB("black", 0.5))
expect_true(d[[2]]$line$color == toRGB("red"))
expect_true(d[[2]]$fillcolor == toRGB("red", 0.5))
expect_true(
d[[1]]$text == '<br> <b>Example</b> of <em>custom</em> hover text <br> A <br> abc <br>.'
)
expect_true(
d[[2]]$text == '<br> <b>Example</b> of <em>custom</em> hover text <br> B <br> xyz <br>.'
)
})
test_that("Can map data to legendgroup", {
d <- data.frame(
x = 1:100,
y = runif(100),
group = letters[1:5]
)
l <- plot_ly(data = d, x = ~x, y = ~y) %>%
add_bars(color = ~group, legendgroup = ~group) %>%
add_markers(color = ~group, legendgroup = ~group) %>%
plotly_build()
expect_length(l$x$data, 10)
markers <- compact(lapply(l$x$data, function(tr) if (tr$type == "scatter") tr))
for (i in seq_along(markers)) {
expect_length(markers[[i]]$legendgroup, 1)
expect_true(markers[[i]]$legendgroup == letters[[i]])
}
bars <- compact(lapply(l$x$data, function(tr) if (tr$type == "bar") tr))
for (i in seq_along(bars)) {
expect_length(bars[[i]]$legendgroup, 1)
expect_true(bars[[i]]$legendgroup == letters[[i]])
}
})
test_that("Axis domains aren't supplied if layout.grid exists", {
p <- plot_ly(type = 'scatter',mode = 'lines', y = c(5,1,3), xaxis = 'x', yaxis = 'y') %>%
add_trace(y = c(2,1,5), xaxis = 'x2', yaxis = 'y2') %>%
add_trace(y = c(2,1,5), xaxis = 'x3', yaxis = 'y3')%>%
add_trace(y = c(2,1,5), xaxis = 'x4', yaxis = 'y4')%>%
add_trace(y = c(2,1,5), xaxis = 'x5', yaxis = 'y5')%>%
add_trace(y = c(2,1,5), xaxis = 'x6', yaxis = 'y6')%>%
layout(grid = list(rows = 2, columns = 3, pattern ='independent'))
l <- expect_doppelganger_built(p, "layout-grid")
expect_null(l$layout$xaxis$domain)
expect_null(l$layout$yaxis$domain)
})
test_that("Informative deprecation message for titlefont", {
expect_warning(config(plot_ly(), cloud = TRUE), "cloud")
})
test_that("Informative warning for invalid config attr", {
p <- config(plot_ly(), foobar = TRUE)
expect_warning(plotly_build(p), "foobar")
})
test_that("Informative deprecation message for titlefont", {
p <- layout(plot_ly(), title = "A title", titlefont = list(size = 30))
expect_warning(plotly_build(p), "titlefont")
})
test_that("toWebGL() shouldn't complain if it's already webgl", {
p <- plot_ly(x = 1, y = 1) %>%
add_trace(type = "scattergl", mode = "markers") %>%
toWebGL()
expect_silent(plotly_build(p))
})
test_that("Line breaks are properly translated (R -> HTML)", {
# create target labels
suffix <- "\n\n(third line)\n(fourth line)"
target_labels <- iris$Species %>%
unique() %>%
paste0(suffix) %>%
gsub(pattern = "\n",
replacement = br(),
x = .,
fixed = TRUE)
# test factor column
d <- iris
levels(d$Species) <- paste0(levels(d$Species), suffix)
p1 <- d %>% plot_ly(x = ~Sepal.Length,
y = ~Species)
expect_equivalent(plotly_build(p1)[["x"]][["layout"]][["yaxis"]][["categoryarray"]],
target_labels)
# test character column
p2 <- d %>%
dplyr::mutate(Species = as.character(Species)) %>%
plot_ly(x = ~Sepal.Length,
y = ~Species)
expect_equivalent(plotly_build(p2)[["x"]][["layout"]][["yaxis"]][["categoryarray"]],
target_labels)
})