单击相同的绘图标记两次不会触发事件两次

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

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

小例子:

图书馆(情节)ui <-流体页面(情节输出(情节"))服务器 <- 功能(输入,输出,会话){输出$plot <- renderPlotly({mtcars %>% plot_ly(x=~disp, y=~cyl)})# 点击图中的标记后做一些事情观察事件(事件数据(plotly_click"),{print("do some stuff now") # 第二次点击同一个标记后不执行})}闪亮的应用程序(用户界面,服务器)

我尝试了使用 shinyjs 的 onclick 的变通方法,但无济于事(它在绘图的空白区域效果很好,但在单击标记时却不行):

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

我也尝试使用存储最后一次点击并在之后立即重置的反应值(例如通过 event_data("plotly_hover")),但所有尝试都失败了,因为 event_data("plotly_click") 保持原来的值.

谁能帮忙?

解决方案

问题终于在 Plotly 端得到解决:

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.

Minimal example:

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)

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"))

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.

Can anyone help?

解决方案

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

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.

Minimal example using 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:18:17,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1584217.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:两次   单击   标记   事件

发布评论

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

>www.elefans.com

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