单击相同的绘图标记两次不会触发事件两次 [英] clicking same plotly marker twice does not trigger events twice
问题描述
在用户单击散点图中的标记后,我正在使用 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: https://github.com/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)
这篇关于单击相同的绘图标记两次不会触发事件两次的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!