Skip to content

Commit 0dd3613

Browse files
committed
merge conflicts; closes #1209
2 parents 1cf9021 + 72bd60f commit 0dd3613

File tree

10 files changed

+143
-32
lines changed

10 files changed

+143
-32
lines changed

NEWS.md

+39-15
Original file line numberDiff line numberDiff line change
@@ -2,36 +2,60 @@
22

33
## NEW FEATURES & IMPROVEMENTS
44

5-
* Upgraded to plotly.js v1.38.2. A _huge_ amount of features and improvements have been made since v1.29.2 (i.e., the version included in the last CRAN release of the R package - v4.7.1). Highlights include a complete re-write of `scattergl` to make it nearly feature complete with `scatter`, localization of text rendering (i.e., international translations), and two new trace types (`violin` & `table`). Read more about the v1.32.0 release [here](https://codeburst.io/notes-from-the-latest-plotly-js-release-b035a5b43e21) and the complete list of changes [here](https://github.com/plotly/plotly.js/releases).
5+
### plotly.js and `plot_ly()` specific improvements
6+
7+
* Upgraded to plotly.js v1.38.2. A _huge_ amount of features and improvements have been made since v1.29.2 (i.e., the version included in the last CRAN release of the R package - v4.7.1). Highlights include a complete re-write of `scattergl` to make it nearly feature complete with `scatter`, localization of text rendering (i.e., international translations), and six new trace types (`cone`, `scatterpolar`, `scatterpolargl`, `splom`, `table`, & `violin`)! See [here](https://github.com/plotly/plotly.js/releases) for a complete list of plotly.js-specific improvements.
68
* Support for **sf** (simple feature) data structures was added to `plot_ly()`, `plot_mapbox()`, and `plot_geo()` (via the new `add_sf()` function). See [this blog post](https://blog.cpsievert.me/2018/03/30/visualizing-geo-spatial-data-with-sf-and-plotly) for an overview.
7-
* New "special arguments" `stroke`, `strokes`, `alpha_stroke`, `span`, and `spans` were added for easier control over the stroke (i.e., outline) appearance of various (filled) graphical marks. For an overview, see the **sf** blog post linked to in the bullet point above and the new package demos (list all demos with `demo(package = "plotly")`).
8-
* The new `partial_bundle()` function makes it easy to leverage [partial bundles of plotly.js](https://github.com/plotly/plotly.js#partial-bundles) for reduced file sizes and faster render times.
9-
* The `config()` function gains a `locale` argument for easily changing localization defaults (see #1270). This makes it possible localize date axes, and in some cases, modebar buttons (see #1270)
9+
* Better control over the stroke (i.e., outline) appearance of various filled graphical marks via the new "special arguments" (`stroke`, `strokes`, `alpha_stroke`, `span`, and `spans`). For an overview, see the **sf** blog post linked to in the bullet point above and the new package demos (list all demos with `demo(package = "plotly")`).
10+
11+
### `ggplotly()` specific improvements
12+
13+
* `ggplotly()` now supports conversion of **ggplot2**'s `geom_sf()`.
1014
* One may now inform `ggplotly()` about the relevant **shiny** output size via `session$clientData`. This ensures `ggplotly()` sizing is closer to **ggplot2** sizing, even on window resize. For an example, run `plotly_example("shiny", "ggplotly_sizing")`.
11-
* Instead of an error, `ggplotly(NULL, "message")` and `plotly_build(NULL, "message")` now returns `htmltools::div("message")`, making it easier to relay messages in shiny when data isn't yet ready to plot (see #1116)
15+
16+
### Other improvements relevant for all **plotly** objects
17+
1218
* The selection (i.e., linked-brushing) mode can now switch from 'transient' to 'persistent' by holding the 'shift' key. It's still possible to _force_ persistent selection by setting `persistent = TRUE` in `highlight()`, but `persistent = FALSE` (the default) is now recommended since it allows one to switch between [persistent/transient selection](https://plotly-book.cpsievert.me/linking-views-without-shiny.html#transient-versus-persistent-selection) in the browser, rather than at the command line.
13-
* The `highlight()` function gains a `debounce` argument for throttling the rate at which `on` events may be fired. This is mainly useful for improving user experience when `highlight(on = "plotly_hover")` and mousing over relevant markers at a rapid rate (see #1277)
14-
* The `animation_button()` function gains a `label` argument, making it easier to control the label of an animation button generated through the `frame` API (see #1205).
15-
* You can now do async rendering of Plotly plots with Shiny, using the [promises](https://rstudio.github.io/promises/) package.
19+
* The `highlight()` function gains a `debounce` argument for throttling the rate at which `on` events may be fired. This is mainly useful for improving user experience when `highlight(on = "plotly_hover")` and mousing over relevant markers at a rapid rate (#1277)
20+
* The new `partial_bundle()` function makes it easy to leverage [partial bundles of plotly.js](https://github.com/plotly/plotly.js#partial-bundles) for reduced file sizes and faster render times.
21+
* The `config()` function gains a `locale` argument for easily changing localization defaults (#1270). This makes it possible localize date axes, and in some cases, modebar buttons (#1270).
22+
* Instead of an error, `ggplotly(NULL, "message")` and `plotly_build(NULL, "message")` now returns `htmltools::div("message")`, making it easier to relay messages in shiny when data isn't yet ready to plot (#1116).
23+
* The `animation_button()` function gains a `label` argument, making it easier to control the label of an animation button generated through the `frame` API (#1205).
24+
* Support for async rendering of inside **shiny** apps using the [promises](https://rstudio.github.io/promises/) package (#1209).
1625

1726
## CHANGES
1827

28+
### `plot_ly()` specific changes
29+
30+
* 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")` should become `plot_ly(mtcars, x = ~wt, y = ~mpg, color = ~factor(vs), name = ~paste(vs, "\na"))`)
1931
* 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"))`.
2032
* 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")`).
2133
* 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))`).
2234
* `layout.showlegend` now defaults to `TRUE` for a *single* pie trace. This is a more sensible default and matches pure plotly.js behavior.
23-
* The `elementId` field is no longer populated, which fixes the "Ignoring explicitly provided widget ID" warning in shiny applications (see #985).
35+
36+
### Other changes relevant for all **plotly** objects
37+
38+
* The `elementId` field is no longer populated, which fixes the "Ignoring explicitly provided widget ID" warning in shiny applications (#985).
2439

2540
## BUG FIXES
2641

42+
### `ggplotly()` specific fixes
2743

2844
* The default `height`/`width` that `ggplotly()` assumes is now more consistently correct in various context, but it also now requires access to one of the following devices: `Cairo::Cairo()`, `png()`, or `jpg()`.
29-
* In RStudio, `ggplotly()` was ignoring a specified `height`/`width` (see #1190).
30-
* `ggplotly()` now uses fixed heights for facet strips meaning that their height is still correct after a window resize (see #1265).
31-
* Bug fix for linking views with crosstalk where the source of the selection is an aggregated trace (see #1218).
32-
* Fixed algorithm for coercing the proposed layout to the plot schema (see #1156).
33-
* `add_*()` no longer inherits `crosstalk::SharedData` key information when `inherit = FALSE` (see #1242).
34-
* The `limits` argument of `colorbar()` wasn't being applied to `line.color`/`line.cmin`/`line.cmax` (see #1236).
45+
* In RStudio, `ggplotly()` was ignoring a specified `height`/`width` (#1190).
46+
* `ggplotly()` now uses fixed heights for facet strips meaning that their height is still correct after a window resize (#1265).
47+
48+
### `plot_ly()` specific fixes
49+
50+
* The `limits` argument of `colorbar()` wasn't being applied to `line.color`/`line.cmin`/`line.cmax` (#1236).
51+
52+
### Other fixes relevant for all **plotly** objects
53+
54+
* Bug fix for linking views with crosstalk where the source of the selection is an aggregated trace (#1218).
55+
* Fixed algorithm for coercing the proposed layout to the plot schema (#1156).
56+
* `add_*()` no longer inherits `crosstalk::SharedData` key information when `inherit = FALSE` (#1242).
57+
58+
3559

3660
# 4.7.1
3761

R/highlight.R

+4-3
Original file line numberDiff line numberDiff line change
@@ -43,8 +43,9 @@
4343
#' @param opacityDim a number between 0 and 1 used to reduce the
4444
#' opacity of non-selected traces (by multiplying with the existing opacity).
4545
#' @param selected attributes of the selection, see [attrs_selected()].
46-
#' @param debounce amount of time to wait before firing an event (in milliseconds).
47-
#' This is especially useful when `on = "plotly_hover"` to avoid firing too many events
46+
#' @param debounce amount of time to wait before firing an event (in milliseconds).
47+
#' The default of 0 means do not debounce at all.
48+
#' Debouncing is mainly useful when `on = "plotly_hover"` to avoid firing too many events
4849
#' when users clickly move the mouse over relevant graphical marks.
4950
#' @param ... currently not supported.
5051
#' @export
@@ -83,7 +84,7 @@ highlight <- function(p, on = "plotly_click", off,
8384
dynamic = FALSE, color = NULL,
8485
selectize = FALSE, defaultValues = NULL,
8586
opacityDim = getOption("opacityDim", 0.2),
86-
selected = attrs_selected(), debounce = 1,
87+
selected = attrs_selected(), debounce = 0,
8788
...) {
8889

8990
# currently ... is not-supported and will catch

R/layers2traces.R

+2-1
Original file line numberDiff line numberDiff line change
@@ -273,7 +273,8 @@ to_basic.GeomRect <- function(data, prestats_data, layout, params, p, ...) {
273273
#' @export
274274
to_basic.GeomSf <- function(data, prestats_data, layout, params, p, ...) {
275275

276-
data <- sf::st_as_sf(data)
276+
data[["geometry"]] <- sf::st_sfc(data[["geometry"]])
277+
data <- sf::st_as_sf(data, sf_column_name = "geometry")
277278
geom_type <- sf::st_geometry_type(data)
278279
# st_cast should "expand" a collection into multiple rows (one per feature)
279280
if ("GEOMETRYCOLLECTION" %in% geom_type) {

R/plotly.R

+9-5
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,9 @@
2323
#' (e.g. `plot_ly(x = 1:10, y = 1:10, color = I("red"), marker = list(color = "blue"))`).
2424
#' @param type A character string specifying the trace type (e.g. `"scatter"`, `"bar"`, `"box"`, etc).
2525
#' If specified, it *always* creates a trace, otherwise
26+
#' @param name Values mapped to the trace's name attribute. Since a trace can
27+
#' only have one name, this argument acts very much like `split` in that it
28+
#' creates one trace for every unique value.
2629
#' @param color Values mapped to relevant 'fill-color' attribute(s)
2730
#' (e.g. [fillcolor](https://plot.ly/r/reference#scatter-fillcolor),
2831
#' [marker.color](https://plot.ly/r/reference#scatter-marker-color),
@@ -124,7 +127,7 @@
124127
#'
125128
#' }
126129
#'
127-
plot_ly <- function(data = data.frame(), ..., type = NULL,
130+
plot_ly <- function(data = data.frame(), ..., type = NULL, name,
128131
color, colors = NULL, alpha = NULL,
129132
stroke, strokes = NULL, alpha_stroke = 1,
130133
size, sizes = c(10, 100),
@@ -160,6 +163,7 @@ plot_ly <- function(data = data.frame(), ..., type = NULL,
160163
}
161164

162165
# tack on variable mappings
166+
attrs$name <- if (!missing(name)) name
163167
attrs$color <- if (!missing(color)) color
164168
attrs$stroke <- if (!missing(stroke)) stroke
165169
attrs$size <- if (!missing(size)) size
@@ -193,10 +197,10 @@ plot_ly <- function(data = data.frame(), ..., type = NULL,
193197
# we always deal with a _list_ of traces and _list_ of layouts
194198
# since they can each have different data
195199
layout = list(
196-
width = width,
197-
height = height,
198-
# sane margin defaults (mainly for RStudio)
199-
margin = list(b = 40, l = 60, t = 25, r = 10)
200+
width = width,
201+
height = height,
202+
# sane margin defaults (mainly for RStudio)
203+
margin = list(b = 40, l = 60, t = 25, r = 10)
200204
),
201205
source = source
202206
)

R/plotly_build.R

+3-3
Original file line numberDiff line numberDiff line change
@@ -189,7 +189,7 @@ plotly_build.plotly <- function(p, registerFrames = TRUE) {
189189
dataArrayAttrs, special_attrs(trace), npscales(), "frame",
190190
# for some reason, text isn't listed as a data array in some traces
191191
# I'm looking at you scattergeo...
192-
".plotlyGroupIndex", "text", "key", "fillcolor"
192+
".plotlyGroupIndex", "text", "key", "fillcolor", "name"
193193
)
194194
tr <- trace[names(trace) %in% allAttrs]
195195
# TODO: does it make sense to "train" matrices/2D-tables (e.g. z)?
@@ -205,7 +205,7 @@ plotly_build.plotly <- function(p, registerFrames = TRUE) {
205205
isAsIs <- vapply(builtData, function(x) inherits(x, "AsIs"), logical(1))
206206
isDiscrete <- vapply(builtData, is.discrete, logical(1))
207207
# note: can only have one linetype per trace
208-
isSplit <- names(builtData) %in% c("split", "linetype", "frame", "fillcolor") |
208+
isSplit <- names(builtData) %in% c("split", "linetype", "frame", "fillcolor", "name") |
209209
!isAsIs & isDiscrete & names(builtData) %in% c("symbol", "color")
210210
if (any(isSplit)) {
211211
paste2 <- function(x, y) if (identical(x, y)) x else paste(x, y, sep = br())
@@ -980,7 +980,7 @@ traceify <- function(dat, x = NULL) {
980980
new_dat <- list()
981981
for (j in seq_along(lvls)) {
982982
new_dat[[j]] <- lapply(dat, function(y) recurse(y, n, x %in% lvls[j]))
983-
new_dat[[j]]$name <- lvls[j]
983+
new_dat[[j]]$name <- new_dat[[j]]$name %||% lvls[j]
984984
}
985985
return(new_dat)
986986
}

R/utils.R

+4
Original file line numberDiff line numberDiff line change
@@ -492,6 +492,10 @@ verify_attr <- function(proposed, schema) {
492492
proposed[[attr]] <- structure(proposed[[attr]], apiSrc = TRUE)
493493
}
494494

495+
if (length(proposed$name) > 0) {
496+
proposed$name <- uniq(proposed$name)
497+
}
498+
495499
# do the same for "sub-attributes"
496500
if (identical(role, "object")) {
497501
proposed[[attr]] <- verify_attr(proposed[[attr]], schema[[attr]])

inst/htmlwidgets/plotly.js

+10-3
Original file line numberDiff line numberDiff line change
@@ -166,10 +166,15 @@ HTMLWidgets.widget({
166166

167167
} else {
168168

169+
// new x data could contain a new height/width...
170+
// attach to instance so that resize logic knows about the new size
171+
instance.width = x.layout.width || instance.width;
172+
instance.height = x.layout.height || instance.height;
173+
169174
// this is essentially equivalent to Plotly.newPlot(), but avoids creating
170175
// a new webgl context
171176
// https://github.com/plotly/plotly.js/blob/2b24f9def901831e61282076cf3f835598d56f0e/src/plot_api/plot_api.js#L531-L532
172-
177+
173178
// TODO: restore crosstalk selections?
174179
Plotly.purge(graphDiv);
175180
// TODO: why is this necessary to get crosstalk working?
@@ -471,8 +476,10 @@ HTMLWidgets.widget({
471476
}
472477
}
473478
};
474-
475-
graphDiv.on(x.highlight.on, debounce(turnOn, x.highlight.debounce));
479+
if (x.highlight.debounce > 0) {
480+
turnOn = debounce(turnOn, x.highlight.debounce);
481+
}
482+
graphDiv.on(x.highlight.on, turnOn);
476483

477484
graphDiv.on(x.highlight.off, function turnOff(e) {
478485
// remove any visual clues

man/highlight.Rd

+3-2
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

tests/testthat/test-animate-highlight.R

+19
Original file line numberDiff line numberDiff line change
@@ -368,3 +368,22 @@ test_that("animation button can be customized", {
368368
expect_true(menu$font$color == "white")
369369
expect_true(menu$buttons[[1]]$label == "Custom")
370370
})
371+
372+
373+
test_that("sf works with crosstalk", {
374+
skip_if_not_installed("sf")
375+
376+
nc <- sf::st_read(system.file("shape/nc.shp", package = "sf"), quiet = TRUE)
377+
# shared data will make the polygons "query-able"
378+
ncsd <- crosstalk::SharedData$new(nc)
379+
p <- ggplot(ncsd) +
380+
geom_sf(aes(fill = AREA, text = paste0(NAME, "\n", "FIPS: ", FIPS))) +
381+
ggthemes::theme_map()
382+
gg <- ggplotly(p, tooltip = "text")
383+
d <- gg$x$data
384+
for (i in seq_along(d)) {
385+
if (!isTRUE(d[["_isGraticule"]])) next
386+
expect_false(is.null(d[[i]]$key))
387+
expect_false(is.null(d[[i]]$set))
388+
}
389+
})

tests/testthat/test-plotly-name.R

+50
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", ]$carat)
38+
density2 <- density(diamonds[diamonds$cut %in% "Ideal",]$carat)
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)