我正在尝试在闪亮的应用程序的上下文中使用情节点击事件.在 官方演示之后,我正在使用这段代码来更新日期选择器和点击后跳转到我的应用中的另一个标签:
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
选项卡.I'm assuming that this happens because the event is never cleared, ie d
is not null
when the tab gets changed and so the condition in the if
-clause 计算结果为 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
.
那么,如何以编程方式清除点击事件?将 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