Skip to content

Commit 4092c1a

Browse files
committed
Reduce single-valued vectors to a constant when appropriate. Fixes #675 #817 #826
1 parent 2efa9d7 commit 4092c1a

File tree

3 files changed

+41
-6
lines changed

3 files changed

+41
-6
lines changed

NEWS.md

+2-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
# 4.5.6.9000
22

3-
## NEW FEATURES
3+
## NEW FEATURES & IMPROVEMENTS
44

55
* Added a significant amount of support for "multiple linked views". For some relatively basic examples, see the demos (the ones prefixed with "highlight" are most relevant) -- `demo(package = "plotly")`. For a more comprehensive overview, see <https://cpsievert.github.io/plotly_book/linking-views-without-shiny.html>. For some more complex examples, see <https://cpsievert.github.io/pedestrians/>
66
* Added the `highlight()` function for configuring selection modes/sequences.
@@ -13,6 +13,7 @@
1313
* Added a `plot_dendro()` function for a quick and dirty interactive dendrogram with support for hierarchial selection. For more, see -- <https://cpsievert.github.io/plotly_book/linking-views-without-shiny.html#nested-selections>
1414
* The `export()` function now tries it's best to export WebGL plots (via **RSelenium**). It also gains a `delay` argument.
1515
* The `plotlyOutput()` function gains a `inline` argument which makes it easier to place multiple plots in the same row (in a shiny application).
16+
* Better type checking of trace attributes will now automatically reduce a single-valued vector to a constant (when appropriate). This is particularly useful for anchoring multiple traces to a single legend entry via `legendgroup` (see #675, #817, #826).
1617

1718
## CHANGES
1819

R/utils.R

+17-5
Original file line numberDiff line numberDiff line change
@@ -255,24 +255,27 @@ supply_highlight_attrs <- function(p) {
255255
# make sure plot attributes adhere to the plotly.js schema
256256
verify_attr_names <- function(p) {
257257
# some layout attributes (e.g., [x-y]axis can have trailing numbers)
258-
check_attrs(
258+
attrs_name_check(
259259
sub("[0-9]+$", "", names(p$x$layout)),
260260
c(names(Schema$layout$layoutAttributes), c("barmode", "bargap", "mapType")),
261261
"layout"
262262
)
263263
for (tr in seq_along(p$x$data)) {
264264
thisTrace <- p$x$data[[tr]]
265-
validAttrs <- Schema$traces[[thisTrace$type %||% "scatter"]]$attributes
266-
check_attrs(
265+
attrSpec <- Schema$traces[[thisTrace$type %||% "scatter"]]$attributes
266+
# make sure attribute names are valid
267+
attrs_name_check(
267268
names(thisTrace),
268-
c(names(validAttrs), "key", "set", "frame", "_isNestedKey", "_isSimpleKey"),
269+
c(names(attrSpec), "key", "set", "frame", "_isNestedKey", "_isSimpleKey"),
269270
thisTrace$type
270271
)
272+
# if it makes sense, reduce a single-valued vector to a constant
273+
p$x$data[[tr]] <- attrs_simplify(thisTrace, attrSpec)
271274
}
272275
invisible(p)
273276
}
274277

275-
check_attrs <- function(proposedAttrs, validAttrs, type = "scatter") {
278+
attrs_name_check <- function(proposedAttrs, validAttrs, type = "scatter") {
276279
illegalAttrs <- setdiff(proposedAttrs, validAttrs)
277280
if (length(illegalAttrs)) {
278281
warning("'", type, "' objects don't have these attributes: '",
@@ -284,6 +287,15 @@ check_attrs <- function(proposedAttrs, validAttrs, type = "scatter") {
284287
invisible(proposedAttrs)
285288
}
286289

290+
attrs_simplify <- function(trace, spec) {
291+
for (attr in names(trace)) {
292+
type <- tryCatch(spec[[attr]]$valType, error = function(e) NULL)
293+
if (isTRUE(type %in% c("any", "data_array"))) next
294+
trace[[attr]] <- uniq(trace[[attr]])
295+
}
296+
trace
297+
}
298+
287299
# ensure both the layout and trace attributes are sent to plotly.js
288300
# as data_arrays
289301
verify_boxed <- function(p) {

inst/stars.R

+22
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
library(jsonlite)
2+
library(ggplot2)
3+
4+
stars <- fromJSON(
5+
"https://raw.githubusercontent.com/cpsievert/starline/master/data/stars.json",
6+
simplifyDataFrame = FALSE
7+
)
8+
9+
dats <- lapply(stars$repos, function(r) {
10+
starz <- unlist(r$stars$dates)
11+
starz <- starz[sort(names(starz))]
12+
data.frame(
13+
date = as.Date(names(starz)),
14+
value = cumsum(starz),
15+
repo = r$uri,
16+
stringsAsFactors = FALSE
17+
)
18+
})
19+
20+
d <- dplyr::bind_rows(dats)
21+
ggplot(d, aes(date, value, color = repo)) +
22+
geom_line() + ylab("Number of stars")

0 commit comments

Comments
 (0)