oska boska
oska boska

Reputation: 231

Upload a user picked directory in R Shiny

I have an R shiny app that requires multiple files (all found within the same folder) to be uploaded. I also know the names of the files to look for.

Is there any way to upload these files other than via fileInput(..., multiple = TRUE)? Ideally, I would have the user upload the whole directory.

Upvotes: 2

Views: 2526

Answers (1)

Wilmar van Ommeren
Wilmar van Ommeren

Reputation: 7689

You can use the library shinyFiles to let the user pic a folder somewhere on their local disk. Then in the server you can use the user input to load all the files that you need from this folder.

Example

library(shiny)
library(shinyFiles)

### UI
ui <- fluidPage(
  shinyDirButton('directory_select', 'Select a directory', title='Select a directory'),
  textOutput('directory_name')
)

### Server
server <- function(input, output, session) {
  volumes <- getVolumes()
  shinyDirChoose(input, 'directory_select', roots=volumes, session=session)
  dirname <- reactive({parseDirPath(volumes, input$directory_select)})

  ## Observe input dir. changes
  observe({
    if(!is.null(dirname)){
      print(dirname())
      output$directory_name <- renderText(dirname())
      ## Load files here
      # csv <- read.csv(paste0(dirname(), '/filename1.csv'))
      # rdata <- load(paste0(dirname(), '/filename2.Rdata'))
      # etc.
    }
  })
}

shinyApp(ui = ui, server = server)

Upvotes: 4

Related Questions