Disable visual response events R Plotly click

I am creating a Shiny application with a plot_ly scatter plot_ly . I use the SharedData object (from the crosstalk packet) to exchange information between the graph and the datatable (from DT).

The problem is that when you click a point on the chart, it reduces the color of all the other points and adds an entry to the legend for the selected point, and once this happens, there seems to be no way to cancel it. I would like to disable these visual changes, but still be able to detect clicks on the schedule.

This problem does not occur if I just use a reactive data.frame instead of a SharedData object in the plot_ly call data parameter, but then event_data on the graph does not have enough information to select a row in datatable . (The exact x and y coordinates are floating point numbers, so matching by coordinates with data may have unexpected results.)

Here is a demo using mtcars :

 library(shiny) library(DT) library(plotly) library(data.table) library(crosstalk) ### UI function --------- ui <- fluidPage( fluidRow( plotlyOutput('my_graph', height = '400px') ), fluidRow( dataTableOutput('my_table') ) ) ### Server function ------- server <- function(input, output, session) { ### SharedData object ---- filtered_data <- reactive({ data.table(mtcars, keep.rownames = TRUE) }) shared_data <- reactive({ req(filtered_data()) SharedData$new(filtered_data(), ~rn) }) ### my_graph ---- output$my_graph <- renderPlotly({ p <- plot_ly(shared_data(), x = ~disp, y = ~mpg, color = ~factor(carb), source = 'm') p }) ### my_table --------- output$my_table <- renderDataTable({ datatable(shared_data()$data(), selection = 'single') }) observe({ click_detect = plotly::event_data('plotly_hover', source = 'm') str(click_detect) dataTableProxy('my_table') %>% selectRows(match(click_detect$key, shared_data()$data()$rn)) }) } shinyApp(ui, server) 
+7
r shiny plotly dt
source share
1 answer

Why this happens, but I see two possible workarounds.


Force the opacity of all markers to 1.

 if (click_detect$curveNumber != 0) { output$my_graph <- renderPlotly({ p <- plot_ly(shared_data(), x = ~disp, y = ~mpg, color = ~factor(carb), source = 'm', marker = list(opacity = 1)) p }) } 

Disadvantage: the graph is flickering.


Modify the filterRows . I do not know your data, but for mtcars you can filter by carb (via curveNumber ) and then through pointNumber .

 dataTableProxy('my_table') %>% selectRows( which(mtcars$carb == sort(unique(mtcars$carb))[[click_detect$curveNumber + 1]])[[click_detect$pointNumber + 1]]) 
+2
source share

All Articles