简体   繁体   中英

create list of file paths in R Shiny without uploading files

I have an R script that applies a function to every file inside a folder through a loop. The function takes the full path with file name and extension as input.

The input to the loop with the full file path of each file to loop through is created with:

listBands <- list.files(myInputDir1, full.names = T)

Then the loop:

for (i in 1:12) { 
obj <- listbands[i]
function(obj) 
and so on..
}

I would like to recreate this in a Shiny app, where the user selects either the folder to loop through or selects all the files inside the folder.

So far I tried this, based on another question in here:

ui <- fluidPage(
  shinyFilesButton("Btn_GetFile", "Choose a file" ,
               title = "Please select a file:", multiple = T,
               buttonType = "default", class = NULL),

textOutput("txt_file")
)


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

volumes = getVolumes()
observe({
  shinyFileChoose(input, "Btn_GetFile", roots = c(home = '~'), 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)

However this function tries to upload all the files, which is not possible as they are too big, and also not necessary.

This answer: Interactive directory input in Shiny app (R) lets me choose the directory, but not create a list that I can use as input to my loop.

I have some experience in R but is very new to Shiny app. Anyone that can help me on the right path?

You could do something like this:

library(shinyFiles)
library(shiny)

ui <- fluidPage(
  shinyDirButton("Btn_GetFolder", "Choose a folder" ,
                   title = "Please select a folder:",
                   buttonType = "default", class = NULL),

  textOutput("txt_file")
)


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

  volumes = getVolumes()
  observe({

    shinyDirChoose(input, "Btn_GetFolder", roots = volumes, session = 
                      session)
    if(!is.null(input$Btn_GetFolder)){
       # browser()
      myInputDir1 <- parseDirPath(volumes, input$Btn_GetFolder)
      listBands <- list.files(myInputDir1, full.names = T)
      output$txt_file <- renderText(listBands)

   #Call your function here..... 
    }
  })
}

shinyApp(ui = ui, server = server)

You can select the directory using shinyDirChoose from shinyFiles package. The file won't be uploaded here and you'll get all the file names in that directory.

Hope it helps!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM