Please is there any simple way how to allow the user of R Shiny application (locally, not on server) to select a directory from a computer and then output the path? I cannot find an easy way such as fileInput for selecting files.
I want the user to be able to search the whole PC for folders and then select the folder, and the path to this folder will be displayed in the Shiny app, such as
C:\users\Jane\folder. In the answer below, I am able to search only the current working directory for folders, not the whole PC and the path to the folder is not displayed in the Shiny app.
2 Answers
Trending sort
Trending sort is based off of the default sorting method — by highest score — but it boosts votes that have happened recently, helping to surface more up-to-date answers.
It falls back to sorting by highest score if no posts are trending.
You could consider the shinyFiles package.
On server side you use
shinyDirChoose(input, id = 'folder', ...) and then can access the chosen folder via input$folder.
Reproducible example:
library(shiny)
library(shinyFiles)
shinyApp(
shinyUI(bootstrapPage(
shinyDirButton('folder', 'Select a folder', 'Please select a folder', FALSE)
)),
shinyServer(function(input, output) {
shinyDirChoose(input, 'folder', roots=c(wd='.'), filetypes=c('', 'txt'))
observe({
print(input$folder)
})
})
)
-
Thanks, but how do I do that I can search the whole PC after I click "Select a folder" button? Now, I cannot search the whole PC for folders, but only my working directory.– doremiMay 2, 2019 at 8:42
-
your OS is windows? Would "C:" directory be sufficient?
shinyDirChoose(input, 'folder', roots = c(wd = 'C:'), filetypes=c('', 'txt'))? May 2, 2019 at 11:07 -
Yes, my OS is Windows.
roots = c(wd = 'C:')is better, so it is not possible to select any directory from anywhere, you have to have thisrootsspecified and select a directory fromroots? And also, how do I output in the app the chosen directory? Withinput$folderI get the list and all its elements, but I want just to print out the directory likeC:\folder– doremiMay 2, 2019 at 11:48 -
1) To my knowledge you have to specify the drive, but i am no expert there. You could have a workaround and use a selectInput to specify which drive to use. 2) Look in your console. There the directory is printed, see:
observe({ print(input$folder) })May 2, 2019 at 12:47
Some folks are asking about how to choose your directory differently. You can do this by changing the specification for roots as I do below.
library(shiny)
library(shinyFiles)
ui <- fluidPage(
shinyDirButton('folder', 'Select a folder', 'Please select a folder', FALSE)
)
server <- function(input, output){
volumes = getVolumes() # this makes the directory at the base of your computer.
observe({
shinyDirChoose(input, 'folder', roots=volumes, filetypes=c('', 'txt'))
print(input$folder)
})
}
shinyApp(ui=ui, server=server)