kwame
kwame

Reputation: 1

Double Clicks in Plotly not registering

I am trying to make a shiny app in which the user can both click and double-click on various objects to do various things. The clicks work fine but I cannot get the double clicks to work. In this hopefully simple, reproducible example I cannot get the out to print the details of the double click.

library(shiny)
library(plotly)

ui <- function(){
  fluidPage(
    
    plotlyOutput("plot2")
  )
}

server <- function(input, output, session){
  
  myPlotEventData2 <- reactive({
    event_data(event = "plotly_doubleclick", source = "myPlotSource2")
  })
  
  myPlotEventData3 <- reactive({
    event_data(event = "plotly_click", source = "myPlotSource2")
  })
  
  
  filtered_data1 <- reactive({
    generation_types <- c("Solar", "Wind", "Hydro", "Nuclear")
    MWh <- c(150, 200, 180, 250)
    random_data <- data.frame(generation_type = generation_types, MWh = MWh)
    
  }) 
  output$plot2 <- renderPlotly({
    
    a <- filtered_data1()
    
    plot_ly(data = a, labels = ~generation_type, values = ~round(MWh),
            ids = ~id,
            type = "pie", sort = FALSE,
            
            source = "myPlotSource2") %>%
      layout(showlegend = FALSE, margin = list(l = 0, r = 0, t = 0, b = 0))
    
  })
  observe({
    clicked_data <- myPlotEventData2()
    if (!is.null(clicked_data)) {
      clicked_segment <- clicked_data$pointNumber + 1
      segment_label <- filtered_data1()$generation_type[clicked_segment]
      segment_value <- filtered_data1()$MWh[clicked_segment]
      print(paste("right segment:", segment_label))
      print(paste("right value:", segment_value))
    }
  })
  
  observe({
    clicked_data <- myPlotEventData3()
    if (!is.null(clicked_data)) {
      clicked_segment <- clicked_data$pointNumber + 1
      segment_label <- filtered_data1()$generation_type[clicked_segment]
      segment_value <- filtered_data1()$MWh[clicked_segment]
      print(paste("Clicked segment:", segment_label))
      print(paste("Segment value:", segment_value))
    }
  })
  
  
}

shinyApp(ui, server)

Upvotes: 0

Views: 419

Answers (1)

ismirsehregal
ismirsehregal

Reputation: 33397

This is expected behaviour. The plotly_doubleclick event does not provide any event_data.

Please see chapter 17.2.6 Event priority here:

There are numerous events accessible through event_data() that don’t contain any information (e.g., "plotly_doubleclick", "plotly_deselect", "plotly_afterplot", etc). These events are automatically given an event priority since their corresponding shiny input value never changes. One common use case for events like "plotly_doublelick" (fired when double-clicking in a zoom or pan dragmode) and "plotly_deselect" (fired when double-clicking in a selection mode) is to clear or reset accumulating event data.

Upvotes: 1

Related Questions