0

I am trying to build a front end using Shiny to pass some parameters to a markdown document. One parameter I want to pass is a user selected file name, including the path. I would like to use a file open type dialogue box for the user to navigate through directories and select the file. I do not need/want to actually open the file, that happens in the markdown file, I just want to pass the path and file name of the selected file out of the shiny app. I have this set up using fileInput() but of course this opens the file and makes a copy in a temp directory and the associated path is to the temp directory not the original directory. There have been some related questions about this and the only answers are that this is a security issue related to the server-based nature of shiny. Again, I don't want to open the file, just grab the original path and name. Any thoughts on how to achieve this? Here's the code stripped down to just this issue ...

library(shiny)

ui <- fluidPage(
  titlePanel("Input"),
  mainPanel(
    fileInput(inputId = "rtffile", "Choose RTF File", accept = ".rtf", ),
)

server <- function(input, output, session) {  
  observe({
    filename <<- input$rtffile
  })  
}

shinyApp(ui = ui, server = server)
bmacwilliams
  • 165
  • 6

1 Answers1

1

In general, you can’t get a web browser to give you the path to a file on the user’s local machine.

However, it’s possible to get a path to a file on the server. If the server and the local machine happen to be the same, you can use e.g. shinyFiles to pick a path:

library(shiny)
library(shinyFiles)

ui <- fluidPage(
  titlePanel("Input"),
  mainPanel(
    shinyFilesButton("file", "Choose File", "Choose a file", multiple = FALSE),
    verbatimTextOutput("file")
  )
)

server <- function(input, output, session) {  
  roots <- getVolumes()
  shinyFileChoose(input, "file", roots = roots)
  file <- reactive(parseFilePaths(roots, input$file))
  output$file <- renderPrint(file())
}

shinyApp(ui = ui, server = server)
Mikko Marttila
  • 10,972
  • 18
  • 31
  • Thanks. I had previously tried this. I am setting the root to a known starting directory, which is quite large (>5000 folders), and generating/updating the list of folders takes quite a long time with this option, to the point where it is impractical. Should have mentioned this previously. Not sure why this is so sluggish and if there are options to try to speed this up. – bmacwilliams Mar 04 '22 at 17:29