【问题标题】:create list of file paths in R Shiny without uploading files在 R Shiny 中创建文件路径列表而不上传文件
【发布时间】:2018-06-28 13:52:41
【问题描述】:

我有一个 R 脚本,它通过循环将函数应用于文件夹内的每个文件。该函数将带有文件名和扩展名的完整路径作为输入。

循环的输入以及要循环的每个文件的完整文件路径是通过以下方式创建的:

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

然后循环:

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

我想在一个闪亮的应用程序中重新创建它,用户可以选择要循环的文件夹或选择文件夹内的所有文件。

到目前为止,我根据这里的另一个问题尝试了这个:

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)

但是这个函数会尝试上传所有文件,这是不可能的,因为它们太大了,也没有必要。

这个答案:Interactive directory input in Shiny app (R) 让我可以选择目录,但不能创建一个可以用作循环输入的列表。

我在 R 方面有一些经验,但对 Shiny 应用程序非常陌生。任何人都可以帮助我走上正确的道路吗?

【问题讨论】:

    标签: r shiny


    【解决方案1】:

    你可以这样做:

    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)
    

    您可以使用来自shinyFiles 包的shinyDirChoose 选择目录。该文件不会在此处上传,您将获得该目录中的所有文件名。

    希望对你有帮助!

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2012-04-22
      • 2021-03-24
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-07-29
      • 2021-03-13
      • 1970-01-01
      相关资源
      最近更新 更多