【问题标题】:save brushed points in Shiny in R在R中的Shiny中保存刷点
【发布时间】:2021-05-07 12:48:14
【问题描述】:

我有如下代码,我想在刷过的点上添加一个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)

【问题讨论】:

  • 点击保存按钮后,您是希望在您的环境中将刷过的行保存为数据框还是将它们写为 csv?
  • 你好,将它们导出为 csv 会很好。

标签: r shiny shinyapps


【解决方案1】:

单击时添加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)

【讨论】:

    猜你喜欢
    • 2021-11-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-07-05
    • 1970-01-01
    • 2018-08-20
    • 1970-01-01
    相关资源
    最近更新 更多