如何让我的R Shiny App上传和下载多个文件到本地存储库?

时间:2017-10-03 15:47:53

标签: r download upload shiny

在Shiny应用程序中,我希望用户能够将文件上传到应用程序文件夹中的文件夹,任何用户都可以下载这些文件。此外,文件将根据用户输入上传到文件夹,因此可能需要创建文件夹,因此应用程序应该能够在必要时创建文件夹。任何人都可以帮助我使用我目前的代码吗?

这是我的简化示例代码:

服务器:

library(shiny)

shinyServer(function(input, output, session) {


 observe({

    if(!is.null(input$file_support)){
      dest = paste0("data/unit_processes/")
      ifelse(!dir.exists(file.path(dest, "")), dir.create(file.path(dest, "")), FALSE)
      files = file.rename(input$file_support$datapath, paste0(dest,input$file_support$name))
      a = proc_files()

    }
  })

      proc_files = reactive({
      proc_path =paste0("data/unit_processes/")

        files =  list.files(proc_path,full.names=TRUE) #drop_dir(path = proc_path)
        if(length(files)==0){
          select_file = list(selectInput("support_files","Choose files:",choices = "no support files",multiple = TRUE) )
          f_names ="no files"
          files = f_names
          shinyjs::disable("downloadSupportfiles")

        }else{
          f_names =sub('.*\\/', '', files) #was files$path
          select_file = list(selectInput("support_files","Choose files:",choices=f_names,multiple = TRUE) )
          files = data.frame(files, f_names = f_names)
          shinyjs::enable("downloadSupportfiles")
        }


        select_files = list()
        select_files[[1]] = select_file
        select_files[[2]] = files
        select_files
      })


      output$file_list = renderUI({
        selected_files = proc_files()
        selected_files[[1]]

      })

      output$downloadSupportfiles <- downloadHandler(
        filename = function() {
          paste("output", "zip", sep=".")
        },

        content = function(fname){
          files = proc_files()
          files = data.frame(files[[2]],stringsAsFactors = FALSE)
          f_names = input$support_files
          n_files = length(f_names)
          if(f_names[1]=="no files"){
            shinyjs::disable("downloadSupportfiles")
          }
          paths = c()
          filesToSave = f_names
          for(i in 1:n_files){
              paths[i] = as.character(files$files[which(files$f_names %in% f_names[i])])
            }

          zip(zipfile=fname, files = paths) #filesToSave)

        },

        contentType = "application/zip"
      )

})

用户界面:

fluidPage(

h4("Upload supporting files:"),
fileInput("file_support", "Choose Files:",
          accept = c(
            "text/csv",
            "text/comma-separated-values,text/plain",
            ".csv"),multiple = TRUE),
hr(),


h4("Download supporting files:"),

uiOutput("file_list"),


downloadButton('downloadSupportfiles','Download Selected Files (as zip)')

)

0 个答案:

没有答案