Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add 'plotly_selecting' to acceptable 'on' events #1280

Merged
merged 2 commits into from
Nov 2, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
## New features

* `ggplotly()` now supports the `{ggalluvial}` package. (#2061, thanks @moutikabdessabour)
* `highlight()` now supports `on="plotly_selecting"`, enabling client-side linked brushing via mouse click+drag (no mouse-up event required, as with `on="plotly_selected"`). (#1280)

## Bug fixes

Expand Down
3 changes: 2 additions & 1 deletion R/highlight.R
Original file line number Diff line number Diff line change
Expand Up @@ -130,6 +130,7 @@ highlight <- function(p, on = "plotly_click", off,
if (missing(off)) {
off_default <- switch(
on %||% "",
plotly_selecting = ,
plotly_selected = "plotly_deselect",
plotly_click = "plotly_doubleclick",
plotly_hover = "plotly_doubleclick"
Expand All @@ -148,7 +149,7 @@ highlight <- function(p, on = "plotly_click", off,
# main (non-plotly.js) spec passed along to HTMLwidgets.renderValue()
p$x$highlight <- list(
# NULL may be used to disable on/off events
on = if (!is.null(on)) match.arg(on, paste0("plotly_", c("click", "hover", "selected"))),
on = if (!is.null(on)) match.arg(on, paste0("plotly_", c("click", "hover", "selected", "selecting"))),
off = if (is.default(off)) off else if (!is.null(off)) match.arg(off, off_options),
persistent = persistent,
dynamic = dynamic,
Expand Down
2 changes: 1 addition & 1 deletion R/utils.R
Original file line number Diff line number Diff line change
Expand Up @@ -401,7 +401,7 @@ supply_highlight_attrs <- function(p) {
# add HTML dependencies, set a sensible dragmode default, & throw messages
if (hasKeys) {
p$x$layout$dragmode <- p$x$layout$dragmode %|D|%
default(switch(p$x$highlight$on %||% "", plotly_selected = "select") %||% "zoom")
default(switch(p$x$highlight$on %||% "", plotly_selected = "select", plotly_selecting = "select") %||% "zoom")
if (is.default(p$x$highlight$off)) {
message(
sprintf(
Expand Down
5 changes: 2 additions & 3 deletions demo/crosstalk-highlight-intro.R
Original file line number Diff line number Diff line change
Expand Up @@ -27,15 +27,14 @@ layout(p, title = "Click on a marker to highlight that patient")
# "plotly_selected", which corresponds to click and drag mouse events.
p %>%
layout(title = "Click and drag to select patient") %>%
highlight("plotly_selected")

highlight("plotly_selecting")

# Plotly provides two types of drag modes that will trigger a "plotly_selected"
# event: "lasso" and "select". You can change the dragmode interactively via
# the modebar and/or set the default dragmode via `layout()`.
p %>%
layout(title = "Click and drag to select patient", dragmode = "lasso") %>%
highlight("plotly_selected")
highlight("plotly_selecting")

# The first argument of `highlight()`, `on`, sets the interaction type used
# trigger a "highlight selection". The second argument, `off`, sets the
Expand Down