Skip to content

Commit 238018b

Browse files
committed
add some tests
1 parent f069ac8 commit 238018b

File tree

2 files changed

+51
-1
lines changed

2 files changed

+51
-1
lines changed

NEWS.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414

1515
## CHANGES
1616

17-
* The `name` attribute is now a "special `plot_ly()` argument" and behaves similar to `split` (it ensures a different trace for every unique value supplied). Although this leads to a breaking change (`name` was previously appended to an automatically generated trace name), it leads to a better and more flexible API. Those that wish to have the old behavior back should provide relevant mappings to the `name` attributes (e.g. `plot_ly(mtcars, x = ~wt, y = ~mpg, color = ~factor(vs), name = "a")` would become `plot_ly(mtcars, x = ~wt, y = ~mpg, color = ~factor(vs), name = ~paste(vs, "\na"))`)
17+
* The `name` attribute is now a "special `plot_ly()` argument" and behaves similar to `split` (it ensures a different trace for every unique value supplied). Although this leads to a breaking change (`name` was previously appended to an automatically generated trace name), it leads to a more flexible and transparent API. Those that wish to have the old behavior back should provide relevant mappings to the `name` attributes (e.g. `plot_ly(mtcars, x = ~wt, y = ~mpg, color = ~factor(vs), name = "a")` would become `plot_ly(mtcars, x = ~wt, y = ~mpg, color = ~factor(vs), name = ~paste(vs, "\na"))`)
1818
* The `color` argument now maps to `fillcolor`, making it much easier to use polygon fills to encode data values (e.g., choropleth maps). For backwards-compatibilty reasons, when `color` maps to `fillcolor`, `alpha` defaults to 0.5 (instead of 1). For an example, `plot_mapbox(mn_res, color = ~INDRESNAME)` or `plot_mapbox(mn_res, split = ~INDRESNAME, color = ~AREA, showlegend = FALSE, stroke = I("black"))`.
1919
* The `color` argument no longer automatically add `"markers"` to the `mode` attribute for scatter/scattergl trace types. Those who wish to have the old behavior back, should add `"markers"` to the `mode` explicity (e.g., change `plot_ly(economics, x = ~pce, y = ~pop, color = ~as.numeric(date), mode = "lines")` to `plot_ly(economics, x = ~pce, y = ~pop, color = ~as.numeric(date), mode = "lines+markers")`).
2020
* The `size` argument now informs a default [error_[x/y].width](https://plot.ly/r/reference/#scatter-error_x-width) (and `span` informs [error_[x/y].thickness](https://plot.ly/r/reference/#scatter-error_x-thickness)). Note you can override the default by specifying directly (e.g. `plot_ly(x = 1:10, y = 1:10, size = I(10), error_x = list(value = 5, width = 0))`).

tests/testthat/test-plotly-name.R

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
context("name-mapping")
2+
3+
test_that("can create multiple traces from name argument", {
4+
l <- plot_ly() %>%
5+
add_markers(x = 1:10, y = 1:10, name = rep(c("a", "b"), 5)) %>%
6+
plotly_build()
7+
8+
expect_length(l$x$data, 2)
9+
expect_equal(l$x$data[[1]]$name, "a")
10+
expect_equal(l$x$data[[2]]$name, "b")
11+
})
12+
13+
14+
test_that("can override name argument", {
15+
l <- plot_ly() %>%
16+
add_markers(x = 1:10, y = 1:10, split = rep(c("a", "b"), 5), name = "z") %>%
17+
plotly_build()
18+
19+
expect_length(l$x$data, 2)
20+
expect_equal(l$x$data[[1]]$name, "z")
21+
expect_equal(l$x$data[[2]]$name, "z")
22+
23+
# can get back old behvaior
24+
l2 <- plot_ly() %>%
25+
add_markers(x = 1:10, y = 1:10, split = rep(c("a", "b"), 5), name = paste0(rep(c("a", "b"), 5), "<br>z")) %>%
26+
plotly_build()
27+
28+
expect_length(l2$x$data, 2)
29+
expect_equal(l2$x$data[[1]]$name, "a<br>z")
30+
expect_equal(l2$x$data[[2]]$name, "b<br>z")
31+
32+
})
33+
34+
35+
test_that("doesn't break old behavior", {
36+
# from https://community.plot.ly/t/manual-color-bug/10479
37+
density1 <- density(diamonds[diamonds$cut %in% "Fair",])
38+
density2 <- density(diamonds[diamonds$cut %in% "Ideal",])
39+
40+
l <- plot_ly(x = ~density1$x, y = ~density1$y, type = 'scatter', mode = 'lines', name = 'Fair cut', fill = 'tozeroy',
41+
fillcolor = 'rgba(168, 216, 234, 0.5)',
42+
line = list(width = 0.5)) %>%
43+
add_trace(x = ~density2$x, y = ~density2$y, name = 'Ideal cut', fill = 'tozeroy',
44+
fillcolor = 'rgba(255, 212, 96, 0.5)') %>%
45+
plotly_build()
46+
47+
48+
expect_equal(l$x$data[[1]]$name, "Fair cut")
49+
expect_equal(l$x$data[[2]]$name, "Ideal cut")
50+
})

0 commit comments

Comments
 (0)