【问题标题】:How to reset event_data with a shiny button?如何使用闪亮的按钮重置 event_data?
【发布时间】:2019-06-24 13:14:02
【问题描述】:

在交互式 plot_ly 中 event_data 保存选定的数据。 使用下面的代码,可以通过双击绘图来重置 event_data

 output$brush <- renderPrint({
    d <- event_data("plotly_selected")
    if (is.null(d)) "Click and drag events (i.e., select/lasso) appear here (double-click to clear)" else d
  })

但是如何用闪亮的按钮重置 event_data?

有解决办法吗?

【问题讨论】:

    标签: r shiny r-plotly


    【解决方案1】:

    如果绘图的source 参数设置为XXX(默认为A),那么您必须将输入plotly_selected-XXX 设置为NULL。这可以在shinyjs的帮助下完成:

    library(shiny)
    library(plotly)
    library(shinyjs)
    
    ui <- fluidPage(
      useShinyjs(),
      actionButton("reset", "Reset"),
      plotlyOutput("plot")
    )
    
    server <- function(input, output){
      output[["plot"]] <- renderPlotly({
        df <- data.frame(
          x = c(1,2,1), 
          y = c(1,2,1)
        )
        df %>%
          plot_ly(
            x = ~x,
            y = ~y,
            source = "A",
            type = 'scatter',
            mode = 'markers',
            marker = list(size = 20),
            showlegend = FALSE
          ) 
      })
    
      observeEvent(input[["reset"]], {
        runjs("Shiny.setInputValue('plotly_selected-A', null);")
      })
    
      observe({ # just to test
        print(event_data("plotly_selected", source = "A"))
      })
    
    }
    
    shinyApp(ui, server)
    

    【讨论】:

      猜你喜欢
      • 2018-05-22
      • 2020-08-12
      • 2019-06-18
      • 2018-11-27
      • 2019-08-15
      • 2021-12-05
      • 2014-08-07
      • 2019-02-02
      • 1970-01-01
      相关资源
      最近更新 更多