单击相同的情节标记两次不会触发事件两次 [英] clicking same plotly marker twice does not trigger events twice

查看:106
本文介绍了单击相同的情节标记两次不会触发事件两次的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在用户单击散点图中的标记后,我正在使用Plotly的event_data("plotly_click")进行操作(打开模式).之后(例如,关闭模式),event_data("plotly_click")当然不会更改,因此单击相同的标记不会再次触发相同的操作.

I am using Plotly's event_data("plotly_click") to do stuff (opening a modal) after the user clicked on a marker in a scatter plot. Afterwards (e.g. closing the modal), event_data("plotly_click") does of course not change and clicking on the same marker therefore does not trigger the same action again.

最小示例:

library(plotly)
ui <- fluidPage(  
  plotlyOutput("plot")
)
server <- function(input, output, session) {
  output$plot <- renderPlotly({
    mtcars %>% plot_ly(x=~disp, y=~cyl)
  }) 

  # Do stuff after clicking on a marker in the plot
  observeEvent(event_data("plotly_click"), {
    print("do some stuff now") # this is not executed after second click on same marker
  })  
}
shinyApp(ui, server)

我曾尝试使用Shinyjs的onclick解决方法,但无济于事(它在情节的空白区域工作良好,但在单击标记时无效):

I have tried workarounds with shinyjs's onclick, to no avail (it works well in empty areas of the plot but not when clicking on markers):

shinyjs::onclick(id="plot", print("clicked"))

我还尝试使用存储最后一次点击的反应式值,然后立即将其重置(例如,通过event_data("plotly_hover")重置),但是所有尝试均失败,因为event_data("plotly_click")仍保持其旧值.

I have also tried using a reactive Value that stores the last click and is reset immediately afterwards (e.g. by event_data("plotly_hover")), but all tries fail because event_data("plotly_click") remains in its old value.

任何人都可以帮忙吗?

推荐答案

问题终于在Plotly方面得到解决: https://github.com/ropensci/plotly/issues/1043

The issue has finally been fixed on Plotly side: https://github.com/ropensci/plotly/issues/1043

event_data("plotly_click", priority = "event")每次单击都会更新,不仅会根据闪亮的输入更改(与以前一样).从Plotly 4.9.0开始.

event_data("plotly_click", priority = "event") updates on every click, not only on shiny input change (as before). Working from Plotly 4.9.0 on.

使用 Plotly 4.9.0 的最小示例:

library(shiny)
library(plotly)

ui <- shinyUI(
  fluidPage(
    plotlyOutput("plot", height = 200),
    verbatimTextOutput("time_last_click")
  )
)


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

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


  output$time_last_click <- renderPrint({
    tmp <- event_data("plotly_click", priority = "event")
    Sys.time()
  })

})

shinyApp(ui, server)

这篇关于单击相同的情节标记两次不会触发事件两次的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆