Skip to content

Commit f069ac8

Browse files
committed
make name a special argument
1 parent 9e4e5cc commit f069ac8

File tree

4 files changed

+23
-12
lines changed

4 files changed

+23
-12
lines changed

NEWS.md

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,8 @@
44

55
* Upgraded to plotly.js v1.38.1. 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).
66
* 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 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.
7+
* New "special `plot_ly()` 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+
* Persistent selection may now be enabled 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.
99
* 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.
1010
* 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)
1111
* 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")`.
@@ -14,6 +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"))`)
1718
* 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"))`.
1819
* 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")`).
1920
* 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))`).

R/plotly.R

Lines changed: 13 additions & 7 deletions
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
)
@@ -424,7 +428,8 @@ typedArrayPolyfill <- function() {
424428
# and bundle size at print time.
425429
plotlyMainBundle <- function() {
426430
htmltools::htmlDependency(
427-
"plotly-main", "1.38.1",
431+
"plotly-main",
432+
version = "1.38.2",
428433
src = depPath("plotlyjs"),
429434
script = "plotly-latest.min.js",
430435
all_files = FALSE
@@ -433,7 +438,8 @@ plotlyMainBundle <- function() {
433438

434439
plotlyHtmlwidgetsCSS <- function() {
435440
htmltools::htmlDependency(
436-
"plotly-htmlwidgets-css", "1.38.1",
441+
"plotly-htmlwidgets-css",
442+
version = plotlyMainBundle()$version,
437443
src = depPath("plotlyjs"),
438444
stylesheet = "plotly-htmlwidgets.css",
439445
all_files = FALSE

R/plotly_build.R

Lines changed: 3 additions & 3 deletions
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

Lines changed: 4 additions & 0 deletions
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]])

0 commit comments

Comments
 (0)