2017-02-20 115 views
1

在我的應用程序,用戶需要選擇一個文件夾,該文件夾中,他需要選擇一個文件(文件名後綴爲「.seg」)依賴閃亮的物體

此代碼工作 -

library(shiny) 
ui <- shinyUI(fluidPage(
    # select a folder 
    column(2, absolutePanel(fixed = TRUE, width = '180px', 
          selectInput("pick_a_folder", label = '', selected='choose a folder', 
             choices = setNames(as.list(c('choose a folder', 
                    basename(list.dirs(recursive = FALSE)))), 
                  c('choose a folder', 
                  basename(list.dirs(recursive = FALSE))))))), 
    # select a file 
    column(2, absolutePanel(fixed = TRUE, width = '180px', 
          conditionalPanel(condition='!(input.pick_a_folder=="choose a folder")', 
              uiOutput('fileselection')))) 
)) 
server <- shinyServer(function(input, output) { 
    # dinamic file selection. find the files list after folder is choosen 
    output$fileselection <- renderUI({ 
    selectInput('pick_file', '', selected = 'choose a file', 
       choices=setNames(as.list(c('choose a file',basename(list.files(path=input$pick_a_folder,recursive=FALSE, pattern='\\.seg$')))), 
           c('choose a file',basename(list.files(path = input$pick_a_folder, recursive = FALSE, pattern='\\.seg$'))))) 
    }) 
}) 

shinyApp(ui = ui, server = server) 

問題是,如果我在運行代碼後向工作目錄添加文件夾,則不會顯示該文件夾。 於是,我就夾選擇移動到服務器,並使其依賴於一個刷新按鈕,但我得到一個錯誤的list.files

錯誤:無效的「路徑」的說法 這是我的代碼 -

library(shiny) 
ui <- shinyUI(fluidPage(
    # refresh butten for root directory 
    column(1, absolutePanel(fixed=TRUE, actionButton("refresh_wd", "refresh"))), 

    # select a folder 
    column(2, absolutePanel(fixed = TRUE, width = '180px', uiOutput('folderselection'))), 

    # select a file 
    column(2, absolutePanel(fixed = TRUE, width = '180px', 
          conditionalPanel(condition='!(input.pick_a_folder=="choose a folder")', 
              uiOutput('fileselection')))) 
)) 

server <- shinyServer(function(input, output) { 
    # refresh root directory 
    wd_folders <- eventReactive(input$refresh_wd, { 
    basename(list.dirs(recursive = FALSE)) 
    }) 

    output$folderselection <- renderUI({ 
    selectInput('pick_a_folder', '', selected = 'choose a folder', 
       choices = setNames(as.list(c('choose a folder', wd_folders())), 
            c('choose a folder', wd_folders()))) 
    }) 

    # dinamic file selection. find the file list after folder is choosen 
    output$fileselection <- renderUI({ 
    selectInput('pick_a_file', '', selected = 'choose a file', 
       choices=setNames(as.list(c('choose a file',basename(list.files(path=input$pick_a_folder,recursive=FALSE, pattern='\\.seg$')))), 
           c('choose a file',basename(list.files(path = input$pick_a_folder, recursive = FALSE, pattern='\\.seg$'))))) 
    }) 
}) 

shinyApp(ui = ui, server = server) 

因爲你使用eventReactive()將只顯示文件夾,甚至文件夾選擇列表中點擊某人後o任何幫助將不勝感激

+0

只是爲了確保:您是否希望用戶在服務器端選擇文件,還是從自己的計算機中選擇文件? –

+0

從我的腳本在哪裏,在服務器 –

+0

所以'fileInput()'不是你正在尋找的然後:http://shiny.rstudio.com/reference/shiny/latest/fileInput.html –

回答

1

'刷新'按鈕。您可以通過使用ignoreNULL = FALSE避免這種情況:

wd_folders <- eventReactive(input$refresh_wd, { 
    basename(list.dirs(recursive = FALSE)) 
    }, ignoreNULL = FALSE) 

如果你不這樣做,的wd_folders()值將是NULL下手,讓你對你的conditionalPanel條件滿足(這不是「選擇文件夾」 ),因此您的應用程序嘗試讀取目錄NULL中的文件。這給你你的錯誤。

如果你想成爲額外的安全,你可以添加validate(need())到UI渲染爲好,如:

output$fileselection <- renderUI({ 
    validate(need(input$pick_a_folder, label = "Pick a folder first")) 
    validate(need(dir.exists(input$pick_a_folder), 
        label = "Something went wrong. Contact me.")) 
    selectInput('pick_a_file', '', selected = 'choose a file', 
       ...) 
    }) 

這是沒有必要解決您的問題,但我覺得在閃亮的好習慣。

+1

謝謝!現在工作正常。在我開始使用它之前,我會閱讀關於'validate(need())'的內容。非常感謝! –

1

以下是每5秒鐘自動刷新一次文件夾的最小示例。 由於@JoriMeys解釋的原因,它仍然會產生關於path無效的初始警告。

library(shiny) 
ui <- shinyUI(fluidPage(

    column(1, 
      absolutePanel(fixed=TRUE, 

         textOutput('wd'), 

         uiOutput('folderselection'), 
         conditionalPanel(
          condition='!(input.pick_a_folder=="choose a folder")', 
          uiOutput('fileselection')) 
      ) 
    ) 
) 
) 


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

    output$wd <- renderText(basename(
     list.files(path = input$pick_a_folder, 
        recursive=FALSE) 
    ) 
    ) 
    button <- reactiveTimer(intervalMs = 5000) 
    # refresh root directory 
    wd_folders <- reactive({ 
     button() 
     basename(list.dirs(recursive = FALSE)) 
    }) 

    output$folderselection <- renderUI({ 
     selectInput('pick_a_folder', '', 
        choices = c('choose a folder', wd_folders() 
        ) 
     ) 
    }) 

    # dinamic file selection. find the file list after folder is choosen 
    output$fileselection <- renderUI({ 
     selectInput('pick_a_file', '', 
        selected = 'choose a file', 
        choices=c('choose a file', 
           basename(list.files(path = input$pick_a_folder,recursive=FALSE)))) 
    }) 
}) 
shinyApp(ui = ui, server = server) 
+0

謝謝,我會看看你做了什麼。 –