1

The code below takes the user's input file (filename1) and uploads it to the same directory with a new name (example: userFile_filename1) in the same directory. I'd want to upload the input file to a separate location. In R shiny, how can I set a path of my choice?

library(shiny) 

ui <- shinyUI(fluidPage(

  sidebarLayout(
    sidebarPanel(
      fileInput('file1', 'Select file to upload' )  
    ),
    mainPanel(
        h4('List of uploaded files:')
       ,verbatimTextOutput('fileList')
    )
  ))
)

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

        observe({  
          if (is.null(input$file1) ) {    return(NULL)  }  
          file.copy(from = input$file1$datapath, to =  paste0('userFile_',input$file1$name )  ) 
        }) 

        output$fileList <- renderText({  
          input$file1
          dir(pattern = 'userFile_') 
        })
})

shinyApp(ui, server)

Kumar
  • 55
  • 5
  • So are you saying currently the `file.copy` isn't doing anything? Or are you getting an error or something? – MrFlick May 20 '21 at 06:38
  • @MrFlick, This code produces no errors; when I browse and input a file, it saves the same file in the same place (from where it was obtained); however, I want to save the file in a different place. – Kumar May 20 '21 at 06:43
  • 1
    The `to=` parameter takes a file path. You can specify either an absolute directory or a relative path (relative to your current working directory). It's not clear to me where you want this "different place" to be exactly. – MrFlick May 20 '21 at 06:45
  • 1
    you would have to change the `to=` part to somehting like this: `paste0('C:/path_to_location/','userFile_',input$file1$name )` – MGP May 20 '21 at 06:48
  • @MGP, yes, it works – Kumar May 20 '21 at 06:59
  • 3
    Try using `file.path()` to construct the file path. This way you don't have to worry about and (missing) trailing slashes. – Roman Luštrik May 20 '21 at 07:33

0 Answers0