Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Getting file path from Shiny UI (Not just directory) using browse button without uploading the file

Tags:

r

shiny

I need to deal with a huge file (>500mb) in R. So instead of loading such heavy file in R environment, I process the file in chunks of specific number of rows and finally get the aggregate values.

I need user to specify the file (using some kind of browse functionality) so that I can feed the file path to my algorithm

fileConnection <-file( "../output/name.txt", open="w")

Is there any way to get only file path from Shiny UI based on the address specified by user? I tried ShinyFiles package, but it gives only directory to choose, not file.

like image 635
Naresh Ambati Avatar asked Mar 22 '17 08:03

Naresh Ambati


People also ask

What is a fileinput in shiny?

Most inputs return simple vectors, but fileInput () returns a data frame with four columns: name: the original file name on the user’s computer. size: the file size, in bytes. By default, the user can only upload files up to 5 MB. You can increase this limit by setting the shiny.maxRequestSize option prior to starting Shiny.

What is a datapath in shiny apps?

This is a formal specification of the file type that is usually derived from the extension and is rarely needed in Shiny apps. datapath: the path to where the data has been uploaded on the server. Treat this path as ephemeral: if the user uploads more files, this file may be deleted.

Why does shiny have a file management interface?

This is an unusual interface, but it allows Shiny to control where the file should be saved (so it can be placed in a secure location) while you still control the contents of that file. Next we’ll put these pieces together to show how to transfer data files or reports to the user.

How do I use content content in shiny?

content should be a function with one argument, file, which is the path to save the file. The job of this function is to save the file in a place that Shiny knows about, so it can then send it to the user.


1 Answers

This functionality is available in the shinyFiles package. Have a look at this minimal example:

library(shiny)
library(shinyFiles)
  
  
  ui <- fluidPage(
    shinyFilesButton("Btn_GetFile", "Choose a file" ,
                                    title = "Please select a file:", multiple = FALSE,
                          buttonType = "default", class = NULL),
                   
              textOutput("txt_file")     
                   )

  
  server <- function(input,output,session){
    
    volumes = getVolumes()
    observe({  
    shinyFileChoose(input, "Btn_GetFile", roots = volumes, session = session)
    
    if(!is.null(input$Btn_GetFile)){
      # browser()
      file_selected<-parseFilePaths(volumes, input$Btn_GetFile)
      output$txt_file <- renderText(as.character(file_selected$datapath))
    }
  })
  }
  shinyApp(ui = ui, server = server)
like image 92
SBista Avatar answered Sep 20 '22 13:09

SBista