【问题标题】:Removing plotly click event data删除情节点击事件数据
【发布时间】:2017-08-17 04:52:33
【问题描述】:

我正在设计一个包含plotly 散点图的闪亮应用程序。我希望用户能够单击图表以使用event_data 函数记录事件,然后能够通过单击actionButton 清除该事件。一些示例代码如下所示:

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 对象本身中。有什么想法可以覆盖它吗?

我遇到的唯一类似的事情是Clear plotly click event,但它非常hacky,似乎不适合我。

【问题讨论】:

    标签: r shiny plotly


    【解决方案1】:

    在您的示例中,e 仅在renderPrintobserveEvent 中定义,而不是全局定义,因此即使eobserveEvent 中更改,它也不会触发@987654326 中的任何内容@。

    您可以为此使用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 更改时都会更新。

    【讨论】:

      【解决方案2】:

      上一个答案部分解决了问题,但是,用户无法再次单击相同的绘图标记,至少不会触发更新。这个问题可以通过用shinyjs手动重置event_data("plotly_click")的源来解决:

      library(shiny)
      library(plotly)
      library(shinyjs)
      
      ui <- shinyUI(
        fluidPage(
          useShinyjs(),
          # code to reset plotlys event_data("plotly_click", source="A") to NULL -> executed upon action button click
          # note that "A" needs to be replaced with plotly source string if used
          extendShinyjs(text = "shinyjs.resetClick = function() { Shiny.onInputChange('.clientValue-plotly_click-A', 'null'); }"),
          actionButton("reset", "Reset plotly click value"),
          plotlyOutput("plot"),
          verbatimTextOutput("clickevent")
        )
      )
      
      
      server <- shinyServer(function(input, output) {
      
        output$plot <- renderPlotly({
          plot_ly(mtcars, x=~cyl, y=~mpg)
        })
      
        output$clickevent <- renderPrint({
          event_data("plotly_click")
        })
      
        observeEvent(input$reset, {
          js$resetClick()
        })
      })
      
      shinyApp(ui, server)
      

      【讨论】:

      • 有没有办法调整它以使其适用于 plotly_selected 事件?
      • 使用新版本的 plotly 更改为 extendShinyjs(text = "shinyjs.resetClick = function() { Shiny.onInputChange('plotly_click-A', 'null'); }") 以使其工作,并使用 'plotly_selected-A' 使其适用于选择
      • 所以总结起来你应该从extendShinyjs(text = "shinyjs.resetClick = function() { Shiny.onInputChange('.clientValue-plotly_click-A', 'null'); }")中删除.clientValue-
      • 如果您尝试再次修复单击同一绘图标记的问题。这是solution。他们把它固定在阴谋的一边。
      猜你喜欢
      • 2019-06-23
      • 1970-01-01
      • 2019-10-05
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-11-07
      • 1970-01-01
      • 2021-02-07
      相关资源
      最近更新 更多