【问题标题】:R shiny: download multiple .csv filesR 闪亮:下载多个 .csv 文件
【发布时间】:2017-07-26 10:59:50
【问题描述】:

我有一个关于 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 <- 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 个矩阵,只有在第二个选项中,才会显示下载按钮。 我有点困惑,因为tableOutput我在主面板中收到了正确的表格,也下载了physical review第一个下载部分工作正常。 但是第二个在谷歌浏览器中失败并出现下载错误:

download_classify
Failed - Server problem

虽然它的构造方式相同。

【问题讨论】:

    标签: r csv shiny


    【解决方案1】:

    您忘记在 qmethod 参数中加上引号""。您的分类下载处理程序应该是这样的:

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

    【讨论】:

      猜你喜欢
      • 2017-11-03
      • 1970-01-01
      • 1970-01-01
      • 2017-01-13
      • 2019-09-27
      • 2019-07-09
      • 2021-09-12
      • 2016-02-26
      • 2016-05-02
      相关资源
      最近更新 更多