r Shiny:下载多个.csv文件



我对R上的Shiny有一个问题。我有一个函数,该函数返回一个以2个对象为输出的列表,均为矩阵。第一个始终是创建的,并且始终可供下载。第二个,将显示为复选框显示的条件结合。

全局

physical_check <- function(file_path, x,y,z, classification)
input: String, int, int, int, boolean
output: list(matrix1 = result, matrix2 = rating)

ui:

ui <- fluidPage(   
   # Application title
   titlePanel("Review"),
   # Sidebar with a slider input for number of bins 
   sidebarLayout(
     sidebarPanel(
       fileInput('file1', 'Choose .txt File',
                 accept=c('text/csv','text/comma-separated,text/plain')),
       hr(),
       checkboxInput("classification", "Use Text-Classification", value = FALSE),
       hr(),
       downloadButton("download_physic", "Physical Review"),
       br(),
       conditionalPanel(condition = "input.classification == true", 
                        downloadButton("download_classify", "Text Classification"))
     ),
     mainPanel(
       h3("Review"),
       tableOutput("rating"),
       tableOutput("result_shiny")
     )
   )
)

服务器:

server <- function(input, output) {
  inFile <- reactive({
    input$file1
  })
  result <- reactive({NULL})
    classify <-  reactive({
    input$classification
  })
  observe({
    if (!is.null(inFile())) {
      result <- reactive({
        withProgress({
          setProgress(message = "Processing review...")
          physical_check(as.String(inFile()$datapath),15,8,3,classify())
        })
      })
    }  
  output$result_shiny <- renderTable({
    result()$result
  })
    output$rating <- renderTable({
    result()$rating
  })
  output$download_physic <- downloadHandler(
    filename = function() {
      sub(pattern = "\.txt",replacement = "_result.csv",inFile()$name)
    },
    content = function(file) {
      write.table(
        result()$result,
        file,
        sep = ";",
        col.names = NA,
        qmethod = "double"
      )
    }
  )
  output$download_classify <- downloadHandler(
    filename = function() {
      paste("rating", ".csv")
    },
    content = function(file) {
      write.table(
        result()$rating,
        file,
        sep = ";",
        col.names = NA,
        qmethod = double
      )
    }
  )
  }) 
}
# Run the application 
shinyApp(ui = ui, server = server)

因此,如您在代码中看到的,如果触发了复选框,我提到了一个条件下载按钮,用于文本分类。根据此true/fals值,函数 physical_check用真或false调用,并返回矩阵和null或2个矩阵,仅在第二个选项中,显示下载按钮。我有点困惑,因为使用tableOutput,我在主面板中收到了正确的表格,也下载第一个下载零件的physical review也可以。但是第二个在Google Chrome中失败,下载错误:

download_classify
Failed - Server problem

尽管它以相同的方式构造。

您忘了在qmethod参数中将引号""放置。您的下载处理程序分类应该是:

output$download_classify <- downloadHandler(
      filename = function() {
        paste0("rating", ".csv")
      },
      content = function(file) {
        write.table(
          result()$rating,
          file,
          sep = ";",
          col.names = NA,
          qmethod = "double"
        )
      }
    )