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

highlight() function: setting the 'off' event to 'plotly_unhover' results in an error. #1864

Open
ismirsehregal opened this issue Oct 16, 2020 · 3 comments

Comments

@ismirsehregal
Copy link

ismirsehregal commented Oct 16, 2020

This is a modified example from here:

library(plotly)

# load the `txhousing` dataset
data(txhousing, package = "ggplot2")

# declare `city` as the SQL 'query by' column
tx <- highlight_key(txhousing, ~city)

# initiate a plotly object
base <- plot_ly(tx, color = I("black")) %>% 
  group_by(city)

# create a time series of median house price
time_series <- base %>%
  group_by(city) %>%
  add_lines(x = ~date, y = ~median)

highlight(
  time_series, 
  on = "plotly_hover",
  off = "plotly_hover", # causes error
  selectize = FALSE, 
  dynamic = FALSE,
  color = "red",
  persistent = FALSE
)

Not providing off results in a warning:

Setting the off event (i.e., 'plotly_doubleclick') to match the on event (i.e., 'plotly_hover'). You can change this default via the highlight() function.

Setting off to plotly_hover or plotly_unhover (my initial intention) results in an error:

Error in match.arg(off, off_options) :
'arg' should be one of “plotly_doubleclick”, “plotly_deselect”, “plotly_relayout”

Found this via: https://stackoverflow.com/a/64387370/9841389

PS: using the latest CRAN version of plotly.

@ismirsehregal ismirsehregal changed the title highlight() function: setting the off event to 'plotly_hover' results in an error. highlight() function: setting the 'off' event to 'plotly_hover' results in an error. Oct 16, 2020
@ismirsehregal ismirsehregal changed the title highlight() function: setting the 'off' event to 'plotly_hover' results in an error. highlight() function: setting the 'off' event to 'plotly_unhover' results in an error. Oct 20, 2020
@cpsievert
Copy link
Collaborator

I don't think off = "plotly_hover" will ever work, but I think off = "plotly_unhover" could

@ran-codes
Copy link

ran-codes commented Aug 11, 2021

Trying to code this in R/Javascript.

Any suggestions/psuedo-code would be welcome. Is there a JS function to turn selection off?

@ran-codes
Copy link

if it helps anyone. I used onRender to add a restyling handler to the unhover event. Below 'gg' is my ggplotly object

highlight( gg, on = "plotly_hover", off = "plotly_doubleclick", color = NULL ,
               opacityDim=0.15)  %>%
    onRender("
    function(el) {
      el.on('plotly_unhover', function(d) {
        Plotly.restyle('plot_fig3', {opacity: 1}, [4, 5, 6, 7, 8, 9, 10, 11]);
      });
    }
  ")

great work @cpsievert 😄 . Actually reviewed your book in Biometrics (https://onlinelibrary.wiley.com/doi/full/10.1111/biom.13474).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants