在 R 的 Shiny 中保存刷过的点

save brushed points in Shiny in R

我有一个代码如下,我想在刷过的点上加一个Save按钮。非常感谢。

library(shiny)
ui <- basicPage(
  plotOutput("plot1", brush = "plot_brush"),
  verbatimTextOutput("info")
)
server <- function(input, output) {
  x <- NULL
  output$plot1 <- renderPlot({
    plot(mtcars$wt, mtcars$mpg)
  })
  output$info <- renderPrint({
    x <<- brushedPoints(mtcars, input$plot_brush, xvar = "wt", yvar = "mpg")
    x
  })
}

shinyApp(ui, server)

单击时添加 actionButton 以保存刷过的数据框。还将 brushedPoints 输出为 reactive,因此我们可以在代码中多次使用它。

library(shiny)

ui <- basicPage(
  plotOutput("plot1", brush = "plot_brush"),
  verbatimTextOutput("info"),
  actionButton("save", "Save")
)

server <- function(input, output) {
  output$plot1 <- renderPlot({
    plot(mtcars$wt, mtcars$mpg)
  })
  
  data <- reactive({
    brushedPoints(mtcars, input$plot_brush, xvar = "wt", yvar = "mpg")
    })
  
  output$info <- renderPrint({data()})
  
  observeEvent(input$save, {
    write.csv(data(), 'brushed_data.csv', row.names = FALSE)
  })
}

shinyApp(ui, server)