R shiny:下载多个.csv文件

时间:2017-03-06 14:18:51

标签: r csv shiny

我有一个关于Shiny on R的问题。我有一个函数,它返回一个包含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 / FALSE值,函数physical_check被调用为true或false并返回矩阵和NULL或2个matrizes,并且仅在第二个选项中显示下载按钮。 我有点困惑,因为tableOutput我在主面板中收到了正确的表格,同时下载第一个下载部分的physical review也能正常工作。 但第二个在谷歌浏览器中失败,出现下载错误:

download_classify
Failed - Server problem

虽然它的构造方式相同。

1 个答案:

答案 0 :(得分:0)

您忘记在""参数中加上引号qmethod。您的classify下载处理程序应为:

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