简体   繁体   中英

Shiny conditional panel

In my app I want the user to choose a folder, and the to choose a file from that folder.

I thought to use conditionalPanel() so the user will see only the first button until he pick's the folder. I wrote this code but I get this error message, 'object 'input' is not found', what would be the right way to do this? And is it a problem to put a conditional panel in an absolute panel?

library(shiny)
library(ggplot2)

ui <- shinyUI(fluidPage(
  titlePanel(""),
  fluidRow(
    # select input for selecting a folder
    column(2, absolutePanel(fixed = TRUE, width = '180px',
                      selectInput("pick_folder", label = '', selected='choose_a_folder',
                              choices = setNames(as.list(c('choose_a_folder', basename(setdiff(list.dirs(recursive = FALSE),'.')))), 
                                                 c('choose_a_folder', basename(setdiff(list.dirs(recursive = FALSE),'.'))))))),
    # select input for selecting a file absolutePanel then conditionalPanel
    column(2, absolutePanel(fixed = TRUE, width = '180px', 
                      conditionalPanel(condition="input.pick_folder==choose_a_folder",
                                 selectInput('pick_file', label = '', selected = 'choose_a_file',
                                           choices = setNames(as.list(c('choose_a_file', basename(setdiff(list.files(path=input$pick_folder ,recursive = FALSE),'.')))), 
                                                              c('choose a file', basename(setdiff(list.files(path=input$pick_folder ,recursive = FALSE),'.')))))))),
  ),
  fluidRow(
    #plot
    plotOutput('my_plot')

    )))

  # server
  server <- shinyServer(function(input, output) {
    output$my_plot <- renderPlot({
      dat <- read.table(file=paste(input$pick_folder, input$pick_file, sep='/'))
      # some plots over dat
    })

  })
  shinyApp(ui, server)

The probem arises from trying to dynamically create the choices for the file selection inside the ui part of your app. The way you should do this is to create the dynamic part of the ui (Your file selection) in your server part using uiOutput and renderUI

The following code seems to do what you describe you want:

library(shiny)
library(ggplot2)

ui <- shinyUI(fluidPage(
  titlePanel(""),
  fluidRow(
    # select input for selecting a folder
    column(2, absolutePanel(fixed = TRUE, width = '180px',
                            selectInput("pick_folder", label = '', selected='choose_a_folder',
                                        choices = setNames(as.list(c('choose_a_folder', basename(setdiff(list.dirs(recursive = FALSE),'.')))), 
                                                           c('choose_a_folder', basename(setdiff(list.dirs(recursive = FALSE),'.'))))))),
    # select input for selecting a file absolutePanel then conditionalPanel
    column(2, absolutePanel(fixed = TRUE, width = '180px', 
                            conditionalPanel(condition="input.pick_folder==choose_a_folder",
                                             # Insert a dynamic bit of UI
                                             uiOutput("fileselection") 
                                             )
                            )
           )
  ),
  fluidRow(
    #plot
    plotOutput('my_plot')

  )))

# server
server <- shinyServer(function(input, output) {
  output$my_plot <- renderPlot({
    dat <- read.table(file=paste(input$pick_folder, input$pick_file, sep='/'))
    # some plots over dat
  })

  output$fileselection <- renderUI({  #Define the dynamic UI
    selectInput('pick_file', label = '', selected = 'choose_a_file',
                choices = setNames(as.list(c('choose_a_file', basename(setdiff(list.files(path=input$pick_folder ,recursive = FALSE),'.')))), 
                                   c('choose a file', basename(setdiff(list.files(path=input$pick_folder ,recursive = FALSE),'.'))
                                   )
                )
    )
  })

})

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