单击相同的情节标记两次不会触发事件两次

编程入门 行业动态 更新时间:2024-10-09 19:16:16
本文介绍了单击相同的情节标记两次不会触发事件两次的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

在用户单击散点图中的标记后,我正在使用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方面得到解决: github/ropensci/plotly/issues/1043

The issue has finally been fixed on Plotly side: github/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)

更多推荐

单击相同的情节标记两次不会触发事件两次

本文发布于:2023-11-13 11:17:48,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1584216.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:两次   单击   标记   情节   事件

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!