清除剧情点击事件 [英] Clear plotly click event

查看:51
本文介绍了清除剧情点击事件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试在闪亮的应用程序环境中使用按图点击事件.在官方演示之后,我正在使用这段代码来更新日期选择器并点击以下按钮跳转到我的应用中的另一个标签:

I'm trying to use plotly click events in the context of a shiny app. Following the official demo I'm using this bit of code to update a date picker and jump to another tab in my app on click:

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

但是,当我然后尝试从details切换回overview选项卡时,我立即被扔回到details选项卡.我假设发生这种情况是因为从未清除过该事件,即,在更改选项卡时d不是null,因此if子句中的条件评估为TRUE.

However, when I then try to switch back from the details to the overview tab, I get immediately thrown back to the details tab. I'm assuming that this happens because the event is never cleared, i.e. d is not null when the tab gets changed and so the condition in the if-clause evaluates to TRUE.

那么,如何以编程方式清除click事件?在条件的末尾添加d <- NULL似乎不行.

So, how do I clear the click event programmatically? Adding d <- NULL to the end of the conditional doesn't seem to do it.

推荐答案

我也遇到了同样的问题,而我发现的解决方法是将旧状态存储在全局变量中,并且仅在该变量发生更改时执行更新,并且不在!is.null()

I have same problem, and the workaround I've found is to store the old state in a global variable, and do the updates only when that variable changes and not on the !is.null()

selected_date <- 0 # declare outside the server function

server <- function(input, output, session) {
  observe({
    d <- event_data("plotly_click")
    new_value <- ifelse(is.null(d),"0",d$x) # 0 if no selection
    if(selected_date!=new_value) {
      selected_date <<- new_value 
      if(selected_date !=0 && input$navPanel == 'overview')
        updateDateInput(session, "date", value = lubridate::ymd(selected_date))
    }
  })
...
}

这还允许您在未选择元素时添加行为

This also allows you to add a behaviour whenever the element is unselected

这篇关于清除剧情点击事件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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