简体   繁体   English

R Shiny:下载多个.csv文件

[英]R shiny: download multiple .csv files

I have an issue regarding Shiny on R. I have a function which returns a list with 2 objects as output, both as a matrix. 我有一个关于Shiny on R的问题。我有一个函数,该函数返回一个列表,其中包含2个对象作为矩阵输出。 The first one is always created and is always available for download. 第一个始终创建,并且始终可以下载。 The second one, is combined with a condition displayed as a checkbox. 第二个条件与显示为复选框的条件结合在一起。

global 全球

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: 服务器:

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)

So as you can see in the code I mentioned a conditional download button for the text classification, if the checkbox is triggered. 因此,您可以在代码中看到,如果触发了复选框,我提到了用于文本分类的条件下载按钮。 According to this TRUE/FALSE value the function physical_check is called with true or false and returns a matrix and NULL or 2 matrizes and only in the second option, the download button is displayed. 根据此TRUE / FALSE值,将使用true或false调用函数physical_check ,并返回一个矩阵和NULL或2个矩阵,并且仅在第二个选项中显示下载按钮。 I'm a little bit confused, because with tableOutput I receive the correct tables in the main panel, also downloading the physical review with the first downloading part works fine. 我有点困惑,因为使用tableOutput可以在主面板中收到正确的表,并且在下载的第一部分中下载physical review也可以。 But the second one fails in google chrome with a download error: 但是第二个失败在谷歌浏览器中,出现下载错误:

download_classify
Failed - Server problem

Although it's constructed the same way. 尽管它的构造方式相同。

You forgot to put quotes "" in the qmethod argument. 您忘记在qmethod参数中加上引号"" Your download handler for classify should be this: 您用于分类的下载处理程序应为:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM