TobiSonne
TobiSonne

Reputation: 1107

Get original names of files with a function like fileInput (R shiny)

I want to select some files in the browser like with fileInput in Shiny but I only need their paths as a character and nothing else. I do not want to upload them (but it's no problem if it is done anyway). When I use fileInput the result is a data.frame containing the paths of the files in a temporary folder with the names i.e. 0.csv, 1.txt, 2.pdf ... But I need the original filenames (with or without the full path). Is there any way to achieve this in a fast and 'non-hacky' way?

Upvotes: 1

Views: 1648

Answers (2)

shghm
shghm

Reputation: 304

The original names are saved in the variable

input$file1$name

However the "real" data (which is renamed as OP correctly pointed out) can be accessed via

input$file1$datapath

where file1 is the InputId of the function fileInput()

Upvotes: 2

DSGym
DSGym

Reputation: 2867

There is a very important reason why this is not possible: Security

JavaScript has no accress to the file System, so you will not to able to get the full paths of the user. One option is your force your user to use a path, but well... he can lie there of course. Maybe do it like this

You could only use it like this:

library(shiny)


ui <- fluidPage(
  tags$h1("Test"),
  fileInput("file1", "Choose CSV File",
            accept = c(
              "text/csv",
              "text/comma-separated-values,text/plain",
              ".csv")
  ),
  textInput("path", "Please enter the full path of your file"),
  tableOutput("pathtable")
)


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


  testdf <- reactive({

      data.frame(
        ID = 1,
        file = input$path
      )
  })

  output$pathtable <- renderTable({

    if(input$path == "") {
      return(NULL)
    } else {
      testdf()
    }

  })


}

shinyApp(ui = ui, server = server)

Upvotes: 1

Related Questions