JmO
JmO

Reputation: 572

Choose folder or folder directory inside shiny app

I have a problem using shiny. I want to choose the folder where all the files I want to use in my app are saved either 1) by setting the working directory to that folderpath or 2) by uploading all csv data inside this folder to my app for further processing. for 1) I found the shinyFiles package but it is very very slow -not due to my PC- as well as giving me the error:

Warning: Error in dir.create: invalid 'path' argument
Stack trace (innermost first):
    59: dir.create
    58: dirCreate
    57: observerFunc
     2: runApp
     1: shinyFilesExample

when I selected a folder and the create folder button becomes clickable and I am putting a name of the new folder into it and clicking on the "+" beneath that panel. Anybody knows why? Despite that this method works but is very very slow. code below:

library(shiny)
library(shinyFiles)

ui<-fluidPage(sidebarLayout(

  sidebarPanel(
    shinyDirButton("dir", "Chose directory", "Upload")
  ),

  mainPanel(
    h4("output$dir"),
    verbatimTextOutput("dir"), br()

  )

))


server <- function(input,output,session){

  # dir
  shinyDirChoose(input, 'dir', roots = getVolumes())
  dir <- reactive(input$dir)
  output$dir <- renderPrint(dir())




}
shinyApp(ui = ui, server = server)

Is there another option? Maybe to upload all csv data via the fileInput function? Or another way? It should not work only locally but on a server so choose.dir might be not the right way. Many thanks

Upvotes: 4

Views: 11194

Answers (3)

IVIM
IVIM

Reputation: 2367

If there are not too many files in your directory, you can just use fileInput with multiple=T. You can also filter them by extension.

 ui <- fluidPage(
    sidebarLayout(
      sidebarPanel(
        # ?fileInput
        fileInput("file1", "Choose HTML File(s)", multiple=T, accept = ".html")
      ),
      mainPanel(
        verbatimTextOutput("out.print")
      )
    )
  )

  
  server <- function(input, output) {
    
    output$out.print <- renderPrint({
      file <- input$file1
      ext <- tools::file_ext(file$datapath)
      
      req(file)
      validate(need(ext == "html", "Please upload  html  file(s)"))
      
      file 
    })
 
  }

  shinyApp(ui, server)

However, if you have too many files, it seems like library(shinyFiles) remains to be your option

Upvotes: 1

Livruen Nati
Livruen Nati

Reputation: 431

The funktion getwd() gets your current working directory.

server <- function(input,output,session){

  # dir
  shinyDirChoose(input, 'dir', roots = c(name=getwd()))
  dir <- reactive(input$dir)
  output$dir <- renderPrint(dir())
}

Upvotes: 1

Julien Colomb
Julien Colomb

Reputation: 563

so far, shinyfiles is the only way to input folders, as far as I know. It cannot work on a server, because browsers are not allowed to select folders (for security reasons).

The zipping way might be the only way to go if you want it to be working on a server (but I have no clue if it can actually be done)

Upvotes: 4

Related Questions