@@ -366,20 +366,40 @@ supply_highlight_attrs <- function(p) {
366
366
# set "global" options via crosstalk variable
367
367
p $ x $ highlight <- p $ x $ highlight %|| % highlight_defaults()
368
368
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 ])
375
385
376
- # TODO: throw warning if we don't detect valid keys?
377
- hasKeys <- FALSE
378
386
for (i in p $ x $ highlight $ ctGroups ) {
387
+
388
+ # Get all the keys for this crosstalk group
379
389
k <- unique(unlist(keys [names(keys ) %in% i ], use.names = FALSE ))
380
- if (is.null(k )) next
381
390
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
+ }
383
403
384
404
# include one selectize dropdown per "valid" SharedData layer
385
405
selectize <- p $ x $ highlight $ selectize %|| % FALSE
@@ -406,31 +426,18 @@ supply_highlight_attrs <- function(p) {
406
426
407
427
p $ x $ selectize [[groupId ]] <- options
408
428
}
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
- }
420
429
}
421
430
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
432
439
)
433
- }
440
+ )
434
441
}
435
442
436
443
p
0 commit comments