【问题标题】:download CSV using filtered dataframe RShiny使用过滤的数据框 R Shiny 下载 CSV
【发布时间】:2019-03-13 10:41:41
【问题描述】:

我制作了一个闪亮的应用程序,我在其中使用一些值过滤数据集,然后我希望能够下载该过滤后的数据集。但是,我很难理解如何将过滤后的数据集传递给 csv 下载器。这是一个非常大的数据集,因此无法使用 renderDataTable 中可用的按钮(我认为?)有人知道我该怎么做吗?

示例应用:

### data ###
egDf <- data.frame(col1 = sample(letters,10000,replace=T), col2 = sample(letters,10000, replace=T))

### modules ###
chooseCol1UI <- function(id){
  ns <- NS(id)
  uiOutput(ns('chooserCol1'))
}
chooseCol1 <- function(input, output, session, data){
  output$chooserCol1 <- renderUI({
    ns <- session$ns
    pickerInput(inputId = ns('chosenCol1'),
              label = 'Col1',
              choices = paste(sort(unique(egDf$col1))),
              options = list(`actions-box` = TRUE),
              multiple = TRUE)
  })

  return(reactive(input$chosenCol1))

}

csvDownloadUI <- function(id, label = "Download CSV") {
  ns <- NS(id)
  downloadButton(ns("downloadData"), label)
}
csvDownload <- function(input, output, session, data) {
  output$downloadData <- downloadHandler(
    filename = function() {
      paste(names(data), Sys.Date(), '.csv', sep='')
    },
    content = function(file) {
      write.csv(data, file, row.names = FALSE)
    }
  )
}

displayTableUI <- function(id){
  ns <- NS(id)
  DT::dataTableOutput(ns('displayer'))
}
displayTable <- function(input, output, session, data, col1Input){
  output$displayer <- DT::renderDataTable(egDf %>% filter(col1 %in% col1Input()))
}

### server ###
server <- function(input,output){
  chosenCol1 <- callModule(chooseCol1,
                               id = 'appChooseCol1', data = egDf)
  callModule(module = displayTable, id = "appDisplayTable",
             col1Input = chosenCol1)
}

### ui ###
ui <- fluidPage(
  sidebarPanel(
  chooseCol1UI("appChooseCol1")),
mainPanel(displayTableUI("appDisplayTable")))

### app ###
shinyApp(ui = ui, server = server)

【问题讨论】:

  • 基本上,您是在问如何使用闪亮的下载按钮。将数据保存在变量中并将其传递给downloadHandler()。查看示例 here,从 R 运行示例应用程序:runExample("10_download")
  • 我认为问题更多是关于如何从 displayTable 中获取过滤后的数据 - 对吧?
  • @ill 正是,这就是我想要做的

标签: r shiny shiny-reactivity shinymodules


【解决方案1】:

几年前我做了一个带有这样一个按钮的应用程序。在我的例子中,我在server.R 文件中创建了一个反应式表达式,该文件被传递给downloadHandler

这里是the app,这里是the github code。前往server.R 文件并搜索“下载”字符串。

在应用程序中,您会在“数据”标签中找到一个蓝色的下载按钮。该应用程序可让您应用适用于数据表的过滤器,您可以通过按钮下载。

编辑:这是感兴趣的代码的服务器部分:

#data download button
   output$idDwn <- downloadHandler(
    filename = function() { 
     paste('uCount ', format(Sys.time(), "%Y-%m-%d %H.%M.%S"), '.csv', sep='')
    },
    content = function(file) {
     write.csv(datasetInputFilters(), file)
    }
   )

【讨论】:

    【解决方案2】:

    我会创建eventReactive 函数,允许您的col1Input

      # Reactive function based on input
      react_df <- eventReactive(input$chosenCol1, {
    
        return(egDf %>% filter(col1 %in% input$chosenCol1))
    
      })
    
      output$displayer <- renderDataTable(react_df())
    
      # Download box
      output$downloadData <- downloadHandler(
        filename = function() {
          paste("data-", Sys.Date(), ".csv", sep="")
        },
        content = function(file) {
    
          output_d <- react_df()
    
          write.csv(output_d, file, row.names = FALSE)
    
        }
      )
    

    【讨论】:

      猜你喜欢
      • 2021-11-20
      • 1970-01-01
      • 2019-04-07
      • 2021-06-18
      • 1970-01-01
      • 1970-01-01
      • 2017-05-26
      • 2020-08-14
      • 2017-03-24
      相关资源
      最近更新 更多