3

I use @shosaco solution from here to reset selection in plotly:

library(shiny)
library(plotly)
library(shinyjs)
library(V8)

ui <- shinyUI(
  fluidPage(
    useShinyjs(),
    extendShinyjs(text = "shinyjs.resetClick = function() { Shiny.onInputChange('.clientValue-plotly_selected-A', 'null'); }"),
    actionButton("reset", "Reset plotly click value"),
    plotlyOutput("plot"),
    verbatimTextOutput("clickevent")
  )
)

server <- shinyServer(function(input, output) {

  output$plot <- renderPlotly({
    plot_ly(mtcars, x=~cyl, y=~mpg)
  })

  output$clickevent <- renderPrint({
    event_data("plotly_selected")
  })

  observeEvent(input$reset, {
    js$resetClick()
  })
})

shinyApp(ui, server)

enter image description here

and it works with resetting data but does not reset marked rectangle:

enter image description here

Do you have any ideas how to get rid of that rectangle?

Taz
  • 5,755
  • 6
  • 26
  • 63
  • When I run your code, I don't even get the plot. But I get an `Error: formal argument "na" matched by multiple actual arguments` – MLavoie Feb 09 '18 at 11:37
  • I double checked that and it works, so problem is probably on your side. Maybe you should check installed packages? Check if you hav `V8` installed. – Taz Feb 09 '18 at 14:09
  • strangely enough, I can't get the `renderPrint()` from your code, but I can from the answer you cited in your first line! – MLavoie Feb 09 '18 at 14:21
  • 1
    Code works for me, i came across this question looking for an answer to the same issue. If I find anything I will let you know – Mark Mar 05 '18 at 19:50
  • 1
    I came across this SO issue: https://stackoverflow.com/questions/42996303/removing-plotly-click-event-data to remove click events, but my attempts to adapt it to plotly_selected so far didn't work sadly – Mark Mar 05 '18 at 20:08
  • if anyone has any luck making the approach I linked work for plotly_selected I'd be happy to hear about it – Mark Jun 01 '19 at 19:32

1 Answers1

0

A bit late to answer, but as I was facing the same problem just now, I thought I'd post my solution here. You can reset the selection with:

Plotly.restyle(plot, {selectedpoints: [null]});

Adding that to the extendShinyjs call will deselect the points and remove the rectangle, so something like this:

extendShinyjs(
  text = "shinyjs.resetClick = function() { 
    Shiny.onInputChange('.clientValue-plotly_selected-A', 'null'); 
    Plotly.restyle('plot', {selectedpoints: [null]});
  }"
),
bobbel
  • 1,983
  • 6
  • 21