将包含多个文件的zip上传到闪亮的应用程序中

时间:2018-01-15 10:56:28

标签: r shiny zip

我正在尝试通过我的闪亮应用程序上传一个zip文件,其中包含一堆xml文件,然后处理它们。我的代码看起来像这样:

library(shiny)

parse_xml <-function(FileName) {
  a <- xmlParse(FileName)
  xmlToDataFrame(a, stringsAsFactors = FALSE) %>%
    fill(Description, Min, Max, id, StartDateTime,
         EndDateTime, .direction = "up") %>%
    na.omit()
}

ui <- fluidPage(
  column(width = 4,
         fileInput("upload", "Upload", accept = ".zip")),
  column(width = 4,
         br(),
         actionButton("process", "Process uploaded data"))
)

server <- function(input, output) {
  observeEvent(input$process, {
    files <- unzip(input$upload, list = TRUE)

    Data <- ldply(unz(input$upload, files$Name[2:length(files$Name)]), parse_xml)
  })
}

shinyApp(ui = ui, server = server)

在尝试解压缩文件的第一步时,我已经收到错误:

Warning: Error in unzip: invalid zip name argument

有没有人知道如何解决这个问题?

UPD:通过将input$upload更改为input$upload$datapath来修复第一个错误。新错误:

Error in unz: invalid 'description' argument

2 个答案:

答案 0 :(得分:3)

如果其他人需要,这就是我打算做的事情:

library(shiny)
library(dplyr)

parse_xml <-function(FileName) {
  a <- xmlParse(FileName)

  print(1)

  xmlToDataFrame(a, stringsAsFactors = FALSE) %>%
    fill(Description, Min, Max, id, StartDateTime,
         EndDateTime, .direction = "up") %>%
    na.omit()
}

ui <- fluidPage(
  column(width = 4,
         fileInput("upload", "Upload", accept = ".zip")),
  column(width = 4,
         br(),
         actionButton("process", "Process uploaded data"))
)

server <- function(input, output) {
  observeEvent(input$process, {
    files <- unzip(input$upload$datapath, list = TRUE)

    unzip_xml <- function(i) {parse_xml(readLines(unz(input$upload$datapath, files$Name[i])))}

    data <- lapply(1:length(files$Name), unzip_xml)

    data1 <- bind_rows(data)
  })
}

shinyApp(ui = ui, server = server)

答案 1 :(得分:2)

应该是files <- unzip(input$upload$name, list = TRUE)而不是unzip(input$upload, list = TRUE)