rshiny

Is there a way to upload a directory with multiple folders and read all the files within the folders in R Shiny?


I am developing an app where:

  1. A user can upload a directory of folders containing multiple files
  2. The files within each folder are extracted and read.

I don't know the names of the folders, but I do know that every file within the folders is the same file type (.json). Essentially, I want to bypass the folders and pull the files directly and read them to a list. For example, based on Upload a user picked directory in R Shiny (the code of which actually doesn't work, but it's the closest example I could find), it would look something like this:

library(shiny)
library(shinyFiles)

ui <- fluidPage(
 shinyDirButton("directory", "Select Directory", "Please select a directory"),
 textOutput("files")
)

server <- function(input, output, session) {
  volumes <- getVolumes()
  shinyDirChoose(input, "directory", defaultRoot = volumes, session = session)
  
  files_acquired_from_directory <- # some function to extract the files from the folders similar to fileInput()
  
  files <- files_acquired_from_directory()
  fileList <- map(files$datapath, read_json)
  output$files <- renderPrint(fileList())
}

shinyApp(ui, server)

When I try using shinyDirButton and shinyDirChoose, all I get is an empty user-interface, so this might not be the best way to do it - the above is just an example of what I'm looking for.

Thank you so much for any help/assistance/guidance!!


Solution

  • Thanks to @r2evans, I learned that I can put it in a zip file and read it from there.

    library(shiny)
    
    ui <- fluidPage(
      fileInput("file", "Upload file", accept = "zip"),
      tableOutput("check")
    )
    
    server <- function(input, output, session) {
      
      files <- reactive({
        d <- req(input$file)
        u <- unzip(d$datapath)
        u
      })
      
      dataList <- reactive({
        files <- files()
        jsons <- files$Name
        data <- purrr::map(jsons, read_json)
        data
      })
    
      output$check <- renderTable(dataList())
      
      
    }
    
    shinyApp(ui, server)