shinyfiles和renderUI无法正常工作

我正在尝试在shinyApp中使用shinyFiles库,以便用户可以选择一组文件或目录.

我的想法是使用一个根据复选框选择而改变的uiOutput.

在这里,我报告代码,可能比单词更具解释性

UtilityUI <- fluidPage(
  titlePanel("page1"),
  fluidRow(
    column(2, 
      wellPanel(
                tags$p("Check the box below if you want to choose an entire directory"),
                checkboxInput(inputId = 'directory_flag', label = 'Directory path?', value = FALSE),
                uiOutput("input_selection_ui")
            )
    ),
    column(8
           #...
           )
  )
)

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

  output$input_selection_ui <- renderUI({
    if(input$directory_flag == TRUE) {
      shinyDirButton(id = "infiles", label = "Choose directory", title = "Choose a directory")
    } else {
      shinyFilesButton(id = "infiles", label = "Choose file(s)", title = "Choose one or more files", multiple = TRUE)
    }
  })


  shinyFileChoose(input, 'infiles', roots=getVolumes(), session=session, restrictions=system.file(package='base'))
  shinyDirChoose(input, 'infiles', roots=getVolumes(), session=session, restrictions=system.file(package='base'))
}

shinyApp(UtilityUI, UtilityServer)

当按下“shinyFiles”按钮时,问题就出现了:在两种情况下,弹出窗口都不会加载根(shinyDirButton和shinyFilesButton).

如果我不使用uiOutput函数,一切都运行良好……但在这种情况下,我不能改变我的UI dinamically …

非常感谢您的回复,

Inzirio

最佳答案 似乎我无法使用renderUI()工作.相反,我使用conditionalPanel()实现相同的行为来显示替代按钮.这似乎有效.这是代码:

ui <- shinyUI(fluidPage(
  checkboxInput(
    inputId = 'directory_flag',
    label = 'Directory path?',
    value = FALSE
  ),

  conditionalPanel(
    "input.directory_flag == 0",
    shinyFilesButton(
      id = "infile",
      label = "Choose file(s)",
      title = "Choose one or more files",
      multiple = TRUE
    )
  ),
  conditionalPanel(
    "input.directory_flag == 1",
    shinyDirButton(id = "indir", label = "Choose directory", title = "Choose a directory")
  )
))

server <- shinyServer(function(input, output, session) {
  shinyFileChoose(
    input,
    'infile',
    roots = getVolumes(),
    session = session,
    restrictions = system.file(package = 'base')
  )
  shinyDirChoose(
    input,
    'indir',
    roots = getVolumes(),
    session = session,
    restrictions = system.file(package = 'base')
  )
})

shinyApp(ui, server)
点赞