简体   繁体   English

在R Shiny中创建文件路径列表而不上传文件

[英]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. 我有一个R脚本,它通过循环将函数应用于文件夹内的每个文件。 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. 这个答案: Shiny app(R)中的交互式目录输入允许我选择目录,但不能创建可用作循环输入的列表。

I have some experience in R but is very new to Shiny app. 我有一些R方面的经验,但是对于Shiny应用程序来说还是很新的。 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. 您可以使用shinyFiles包中的shinyDirChoose选择目录。 The file won't be uploaded here and you'll get all the file names in that directory. 该文件将不会在这里上传,您将在该目录中获得所有文件名。

Hope it helps! 希望能帮助到你!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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