简体   繁体   中英

shinyfiles and renderUI don't work properly

I'm trying to use the shinyFiles library in my shinyApp, in order to give the user the possibility to select a group of files or a directory. My idea is to use a uiOutput that changes depending on a checkbox selection.

Here I report the code, that maybe is more explicative than words

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)

The problem borns when the "shinyFiles" button is pressed: the popup window doesn't load the roots, in both cases (shinyDirButton and shinyFilesButton).

If I don't use the uiOutput function everything works well... But in that case I cannot change my UI dinamically...

Thanks a lot for your replies,

Inzirio

It seems I can't get it to work either with renderUI() . Instead I implemented the same behavior using conditionalPanel() to show alternative buttons. This seems to work. Here is the code:

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)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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