Thank you for the great package! I am trying to use the onclick
function as a workaround to the fact that Plotly's event_data("plotly_clicked") is not changing when clicking the same spot twice. This works when clicking in empty spaces in the plot. It does not trigger when one clicks on a marker drawn by plotly.
Minimal example:
library(plotly)
library(shinyjs)
ui <- fluidPage(
useShinyjs(),
plotlyOutput("plot")
)
server <- function(input, output, session) {
output$plot <- renderPlotly({
mtcars %>% plot_ly(x=~disp, y=~cyl)
})
onclick(id="plot", print("clicked"))
}
shinyApp(ui, server)
Unfortunately there's not much that shinyjs can do about that. Clicking on the actual points seems to not fire off an onclick of the plot even if you do it directly in javascript. Either plotly is hijacking the click and not letting the message go through, or something else is done to those points that prevents them from counting as clicks on the plot. But either way, it's completely out of shinyjs' hands, sorry!
Thanks for your comment though!
Hi Dean,
I solved the problem for me with.... guess.... shinyjs :-) I manually override the source of event_data("plotly_click")
at a certain event (closing a modal in my case). This source is input[[".clientValue-plotly_click-A']]
(where A is the plotly plot source string). Here is the code just in case:
```{r}
library(shiny)
library(plotly)
library(shinyjs)
ui <- shinyUI(
fluidPage(
useShinyjs(),
# code to reset plotlys event_data() to NULL -> executed upon action button click
# note that "A" needs to be replaced with plotly source string if used
extendShinyjs(text = "shinyjs.resetClick = function() { Shiny.onInputChange('.clientValue-plotly_click-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_click")
})
observeEvent(input$reset, {
js$resetClick()
})
})
shinyApp(ui, server)
```
Most helpful comment
Hi Dean,
I solved the problem for me with.... guess.... shinyjs :-) I manually override the source of
event_data("plotly_click")
at a certain event (closing a modal in my case). This source isinput[[".clientValue-plotly_click-A']]
(where A is the plotly plot source string). Here is the code just in case:```{r}
library(shiny)
library(plotly)
library(shinyjs)
ui <- shinyUI(
fluidPage(
useShinyjs(),
# code to reset plotlys event_data() to NULL -> executed upon action button click
# note that "A" needs to be replaced with plotly source string if used
extendShinyjs(text = "shinyjs.resetClick = function() { Shiny.onInputChange('.clientValue-plotly_click-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_click")
})
observeEvent(input$reset, {
js$resetClick()
})
})
shinyApp(ui, server)
```