2017-02-20 63 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

谢谢,我会看看你做了什么。 –