【问题标题】:Downloadhandler with filtered data in Shiny在 Shiny 中具有过滤数据的下载处理程序
【发布时间】:2019-04-29 03:47:24
【问题描述】:

我尝试使用此处Download filtered data from renderDataTable() in Shiny 和此处R - Download Filtered Datatable 提供的代码语法。就我而言,我使用的是自己的 .csv 文件,而不是标准的“mtcars”数据。 出于某种原因,如果要下载该文件(我在浏览器中打开它),我无法找到该文件。 代码如下:

library(shiny)
library(ggplot2)
library(DT)
library(readr)

tbl <- read.csv(file.choose(new = FALSE), header = TRUE, sep = ",", stringsAsFactors=TRUE)     

# Define UI -------
ui <- navbarPage(
  title = "Data Table Options",

  tabPanel("Lot Dataset",
         DT::dataTableOutput("dt"),  #datatable

           div(h3("Download"), style = "color:blue"),
           helpText(" Select the download format"),
           radioButtons("type", "Format type:",
                        choices = c("Excel (CSV)", "Text (Space Separated)", "Doc")),
           br(),
           helpText(" Click on the download button to download the Lot Dataset"),
           p("Below are the row indices of the data."),
           verbatimTextOutput("filtered_row"),
           br(),
           helpText(" Click on the download button to download the Lot Dataset"),
           downloadButton("download_filtered", "Download Filtered Data"),   
           br()
         )
)

以及带有下载处理程序的服务器功能:

server <- function(input, output) {

  thedata <- reactive({datatable(tbl, filter = "top",options =  list(pageLength = 25))})

  output$dt <- DT::renderDataTable({
    thedata()
  })

  #bottom panel with row indices
  output$filtered_row <- 
    renderPrint({
      input[["dt_rows_all"]]
    })

  #file extension for download
  fileext <- reactive({
    switch(input$type,
           "Excel (CSV)" = "csv", "Text" = "txt", "Doc" = "doc")
  })
  #downloadHandler() for file download of Lot Dataset
  output$download_filtered <- downloadHandler(

    filename = function() {
      file_name <- paste("MLdataset_test", fileext(), sep=".")  #filename 
    },

    content = function(file) {
      write.csv(thedata()[input[["dt_rows_all"]], ],
            file)  
    }
   )

}

# Run the app ----
shinyApp(ui = ui, server = server)

我希望能够下载过滤后的数据表,但由于某种原因,当我要下载时找不到文件

每次我尝试下载它时,控制台都会出现以下错误:

Warning: Error in [: incorrect number of dimensions
  [No stack trace available]

.csv文件的维度:

暗淡(tbl) [1] 19100 56

我真的很感激任何帮助,我已经尝试修复这个问题几个小时但没有成功!

【问题讨论】:

    标签: r datatable download shiny


    【解决方案1】:

    不错的应用。您的问题主要是 thedata()DT::datatable 而不是实际数据。我已经对其进行了重新设计,现在对我有用,请参阅脚本中的 cmets:

    library(shiny)
    library(ggplot2)
    library(DT)
    library(readr)
    
    tbl <- read.csv(file.choose(new = FALSE), header = TRUE, sep = ",", stringsAsFactors=TRUE)     
    
    # Define UI ----
    ui <- navbarPage(
      title = "Data Table Options",
    
      tabPanel("Lot Dataset",
               DT::dataTableOutput("dt"),  #datatable
    
               div(h3("Download"), style = "color:blue"),
               helpText(" Select the download format"),
               radioButtons("type", "Format type:",
                            choices = c("Excel (CSV)", "Text (Space Separated)", "Doc")),
               br(),
               helpText(" Click on the download button to download the Lot Dataset"),
               p("Below are the row indices of the data."),
               verbatimTextOutput("filtered_row"),
               br(),
               helpText(" Click on the download button to download the Lot Dataset"),
               downloadButton("download_filtered", "Download Filtered Data"),   
               br()
      )
    )
    
    server <- function(input, output) {
    
      #!! I've moved the datatable directly in here as 'thedata()' was a bit redundant and confusing
      output$dt <- DT::renderDataTable({
        datatable(tbl,filter = "top",options =  list(pageLength = 25)) 
      })
    
      #bottom panel with row indices
      output$filtered_row <- 
        renderPrint({
          input[["dt_rows_all"]]
        })
    
      #file extension for download
      fileext <- reactive({
        switch(input$type,
               "Excel (CSV)" = "csv", "Text" = "txt", "Doc" = "doc")
      })
    
    
      #downloadHandler() for file download of Lot Dataset
      output$download_filtered <- downloadHandler(
    
        filename = function() {
          paste("MLdataset_test", fileext(), sep=".")  #filename
        },
    
        content = function(file) {
    
          #!! Use tbl and not 'thedata()' to filter. tbl is the data, the other was the datatable
          write.csv(tbl[input[["dt_rows_all"]], ],
                    file= file,
                    #!! assumed we don't want the row names
                    row.names=F)
        }
      )
    
    
    }
    
    shinyApp(ui, server)
    

    【讨论】:

      【解决方案2】:

      这也有效(不过可能有太多不必要的代码)

      server <- function(input, output) { 
        thedata <- reactive({
          datatable(tbl, filter = "top",options = list(pageLength = 25))
          })
      
        #editing the tbl dataset with the filtered rows only
        thedata_filtered <- reactive({
          tbl[c(input[["dt_rows_all"]]), ]
        })
      
        output$dt <- DT::renderDataTable({
          thedata()
        })
      
        #bottom panel with row indices
        output$filtered_row <- 
          renderPrint({
            input[["dt_rows_all"]]
          })
      
        #file extension for download
        fileext <- reactive({
          switch(input$type,
             "Excel (CSV)" = "csv", "Text" = "txt", "Doc" = "doc")
        })
        #downloadHandler() for file download of Lot Dataset
        output$download_filtered <- downloadHandler(
      
          filename = function() {
            file_name <- paste("MLdataset_test", fileext(), sep=".")  #filename 
          },
      
          content = function(file) {
            write.table(thedata_filtered(), file)  
          }
         )
      }
      

      【讨论】:

        猜你喜欢
        • 2017-06-15
        • 2018-07-29
        • 2021-11-20
        • 2019-04-27
        • 1970-01-01
        • 2018-07-31
        • 1970-01-01
        • 2022-07-22
        • 2019-11-01
        相关资源
        最近更新 更多