删除剧情点击事件数据 [英] Removing plotly click event data
问题描述
我正在设计一个包含plotly
散点图的Shiny应用程序.我希望用户能够单击图形以使用event_data
函数记录事件,但随后单击actionButton
即可清除该事件.下面是一些示例代码:
I am designing a Shiny app which contains a plotly
scatter plot. I would like for the user to be able to click on the graph to record an event using the event_data
function, but then be able to clear that event on the click of an actionButton
. Some example code can be seen below:
library(shiny)
library(plotly)
ui <- fluidPage(
actionButton("clearEvent", label = "clear event"),
verbatimTextOutput("plotVal"),
plotlyOutput('plot1')
)
server <- function(input, output, session) {
output$plot1 <- renderPlotly({
d <- diamonds[sample(nrow(diamonds), 1000), ]
plot_ly(d, x = ~carat, y = ~price, color = ~carat,
size = ~carat, text = ~paste("Clarity: ", clarity))
})
output$plotVal <- renderPrint({
e <- event_data("plotly_click")
if (is.null(e)) {
NULL
} else {
e
}
})
observeEvent(input[["clearEvent"]], {
e <- NULL
})
}
shinyApp(ui = ui, server = server)
但是,这并不能清除事件.查看event_data
的代码表明,这可能是因为它存储在session
对象本身内.有什么想法可以覆盖吗?
This doesn't clear the event like I would expect, however. Looking into the code for event_data
shows that this is probably because it is stored within the session
object itself. Any ideas how I can overwrite it?
我遇到的唯一类似的事情是清除密谋单击事件,但是它很hacky似乎不适合我.
The only similar thing I have come across is Clear plotly click event but it's very hacky and doesn't seem to work for me.
推荐答案
在您的示例中,e
只是在renderPrint
和observeEvent
中定义的,而不是全局定义的,即使e
在observeEvent
,它不会触发renderPrint
中的任何内容.
In your example, e
is just defined in the renderPrint
and in the observeEvent
and not globally so even if e
is changed in the observeEvent
, it does not trigger anything in the renderPrint
.
您可以为此使用reactiveValues
:
data <- reactiveValues(e=NULL)
observe({
data$e <- event_data("plotly_click")
})
output$plotVal <- renderPrint({
e <- data$e
if (is.null(e)) {
NULL
} else {
e
}
})
observeEvent(input[["clearEvent"]], {
data$e <- NULL
})
每当用户单击图或按钮时,都会更改
data$e
,并且由于renderPrint
中对data$e
的依赖性,因此只要更改data$e
都会更新.
data$e
is changed whenever the user click the plot or the button, and since there is a dependency on data$e
in the renderPrint
, that gets updated whenever data$e
is changed.
这篇关于删除剧情点击事件数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!