black_sheep07
black_sheep07

Reputation: 2368

Shiny fileInput Does Not Keep File Name

I am writing a program in Shiny where the user uploads a file and it saves to a specified folder. The method to do this is taken from this question's answer.

library(shiny)

shinyApp(
    ui=shinyUI(bootstrapPage(
       fileInput("upload", "Upload", multiple = FALSE)
    )),

    server=shinyServer(function(input, output, session){               
        observe({
            if (is.null(input$upload)) return()
            file.copy(input$upload$datapath, "/some/other/path")
        })
    })
)

When I execute this, I find that the file has its file name stripped when it is saved to the specified folder. The program that reads the files in the folder requires that the file name is left in tact. How can I accomplish that?

Upvotes: 4

Views: 2922

Answers (2)

Geoff
Geoff

Reputation: 180

This works well: rename the files locally and then upload them via a forloop and then remove the local files. Here's some code that worked for me:

observe({
  if(!is.null(input$file_support)){
    browser()
    files = file.rename(input$file_support$datapath, paste0(input$file_support$name))
    files = paste0(input$file_support$name)
    n_files = length(files)
    for(i in 1:n_files){
      drop_upload(files[i], dest = "drop_test")
    }
  file.remove(files)
  }
  })

})

Upvotes: 0

Xiongbing Jin
Xiongbing Jin

Reputation: 12097

Change the file.copy line to

file.copy(input$upload$datapath, paste0("your_folder/", input$upload$name))

Upvotes: 4

Related Questions