我正在尝试在闪亮的应用程序环境中使用按图点击事件。在正式演示之后,我将使用以下代码来更新日期选择器,并在单击时跳转到我的应用程序中的另一个选项卡:
observe({
d <- event_data("plotly_click", source = 'plot')
if(!is.null(d) & (input$navPanel == 'overview')) {
d %>% filter(curveNumber == 0) %>% select(x) -> selected_date
updateDateInput(session, "date", value = lubridate::ymd(selected_date$x))
updateTabsetPanel(session, "navPanel", selected = "details")
}
Run Code Online (Sandbox Code Playgroud)
然而,当我再尝试从切换details
到overview
标签,我立即得到后仰的details
标签。我猜想,这是因为该事件不会被清零,即d
是不是null
当标签被改变,因此在条件if
-clause评估为TRUE
。
因此,如何以编程方式清除click事件?添加d <- NULL
到条件的末尾似乎没有做到这一点。
event_data("plotly_click")
用户单击散点图中的标记后,我正在使用Plotly的东西(打开模式)。之后(例如,关闭模态),event_data("plotly_click")
当然不会改变,因此单击相同的标记不会再次触发相同的动作。
最小示例:
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)
Run Code Online (Sandbox Code Playgroud)
我已经尝试了使用Shinyjs的变通办法onclick
,但无济于事(它在绘图的空白区域工作良好,但在单击标记时无效):
shinyjs::onclick(id="plot", print("clicked"))
Run Code Online (Sandbox Code Playgroud)
我也尝试过使用反应性值来存储最后的点击,然后立即将其重置(例如,通过event_data("plotly_hover")
),但是所有尝试均会失败,因为它event_data("plotly_click")
仍然保持其旧值。
有人可以帮忙吗?