Skip to content

Commit 5f46396

Browse files
committed
Close #2218: properly construct mapping between crosstalk sets and keys when constructing selectize payload
1 parent fa5a1d5 commit 5f46396

File tree

2 files changed

+45
-36
lines changed

2 files changed

+45
-36
lines changed

R/utils.R

+39-32
Original file line numberDiff line numberDiff line change
@@ -366,20 +366,40 @@ supply_highlight_attrs <- function(p) {
366366
# set "global" options via crosstalk variable
367367
p$x$highlight <- p$x$highlight %||% highlight_defaults()
368368

369-
# defaults are now populated, allowing us to populate some other
370-
# attributes such as the selectize widget definition
371-
# TODO: this is wrong!!! What if set is missing and present?!
372-
sets <- unlist(lapply(p$x$data, "[[", "set"))
373-
keys <- setNames(lapply(p$x$data, "[[", "key"), sets)
374-
p$x$highlight$ctGroups <- i(unique(sets))
369+
# Grab the special "crosstalk set" (i.e., group) for each trace
370+
sets <- lapply(p$x$data, "[[", "set")
371+
noSet <- vapply(sets, is.null, logical(1))
372+
373+
# If no sets are present, there's nothing more to do
374+
if (all(noSet)) {
375+
return(p)
376+
}
377+
378+
# Store the unique set of crosstalk sets (which gets looped over client-side)
379+
p$x$highlight$ctGroups <- i(unique(unlist(sets)))
380+
381+
# Build a set -> key mapping for each relevant trace, which we'll use
382+
# to set default values and/or build the selectize.js payload (if relevant)
383+
setDat <- p$x$data[!noSet]
384+
keys <- setNames(lapply(setDat, "[[", "key"), sets[!noSet])
375385

376-
# TODO: throw warning if we don't detect valid keys?
377-
hasKeys <- FALSE
378386
for (i in p$x$highlight$ctGroups) {
387+
388+
# Get all the keys for this crosstalk group
379389
k <- unique(unlist(keys[names(keys) %in% i], use.names = FALSE))
380-
if (is.null(k)) next
381390
k <- k[!is.null(k)]
382-
hasKeys <- TRUE
391+
if (length(k) == 0) next
392+
393+
# set default values via crosstalk api
394+
vals <- intersect(p$x$highlight$defaultValues, k)
395+
if (length(vals)) {
396+
p <- htmlwidgets::onRender(
397+
p, sprintf(
398+
"function(el, x) { crosstalk.group('%s').var('selection').set(%s) }",
399+
i, jsonlite::toJSON(as.character(vals), auto_unbox = FALSE)
400+
)
401+
)
402+
}
383403

384404
# include one selectize dropdown per "valid" SharedData layer
385405
selectize <- p$x$highlight$selectize %||% FALSE
@@ -406,31 +426,18 @@ supply_highlight_attrs <- function(p) {
406426

407427
p$x$selectize[[groupId]] <- options
408428
}
409-
410-
# set default values via crosstalk api
411-
vals <- p$x$highlight$defaultValues[p$x$highlight$defaultValues %in% k]
412-
if (length(vals)) {
413-
p <- htmlwidgets::onRender(
414-
p, sprintf(
415-
"function(el, x) { crosstalk.group('%s').var('selection').set(%s) }",
416-
i, jsonlite::toJSON(as.character(vals), auto_unbox = FALSE)
417-
)
418-
)
419-
}
420429
}
421430

422-
# add HTML dependencies, set a sensible dragmode default, & throw messages
423-
if (hasKeys) {
424-
p$x$layout$dragmode <- p$x$layout$dragmode %|D|%
425-
default(switch(p$x$highlight$on %||% "", plotly_selected = "select", plotly_selecting = "select") %||% "zoom")
426-
if (is.default(p$x$highlight$off)) {
427-
message(
428-
sprintf(
429-
"Setting the `off` event (i.e., '%s') to match the `on` event (i.e., '%s'). You can change this default via the `highlight()` function.",
430-
p$x$highlight$off, p$x$highlight$on
431-
)
431+
# set a sensible dragmode default, & throw messages
432+
p$x$layout$dragmode <- p$x$layout$dragmode %|D|%
433+
default(switch(p$x$highlight$on %||% "", plotly_selected = "select", plotly_selecting = "select") %||% "zoom")
434+
if (is.default(p$x$highlight$off)) {
435+
message(
436+
sprintf(
437+
"Setting the `off` event (i.e., '%s') to match the `on` event (i.e., '%s'). You can change this default via the `highlight()` function.",
438+
p$x$highlight$off, p$x$highlight$on
432439
)
433-
}
440+
)
434441
}
435442

436443
p

tests/testthat/test-animate-highlight.R

+6-4
Original file line numberDiff line numberDiff line change
@@ -82,9 +82,12 @@ test_that("group_by.plotly() retains crosstalk set", {
8282
})
8383

8484
test_that("highlight(selectize) produces a sensible payload", {
85-
p <- mtcars %>%
86-
highlight_key(~cyl, "Choose cylinder") %>%
87-
plot_ly(x = ~wt, y = ~mpg) %>%
85+
p <- plot_ly() %>%
86+
add_lines(data = mtcars, x = ~wt, y = ~mpg) %>%
87+
add_markers(
88+
data = highlight_key(mtcars, ~cyl, "Choose cylinder"),
89+
x = ~wt, y = ~mpg
90+
) %>%
8891
add_markers()
8992

9093
# Builds basic payload when selectize=TRUE
@@ -121,7 +124,6 @@ test_that("highlight(selectize) produces a sensible payload", {
121124

122125
expect_equal(b2$x$selectize[[1]], selectize)
123126

124-
125127
})
126128

127129

0 commit comments

Comments
 (0)