删除剧情点击事件数据 [英] Removing plotly click event data

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

问题描述

我正在设计一个包含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只是在renderPrintobserveEvent中定义的,而不是全局定义的,即使eobserveEvent,它不会触发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屋!

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