R Shiny - Conditional panel within conditional panel

天大地大妈咪最大 提交于 2019-12-08 19:12:20

问题


I am wondering if it is possible to have a conditional panel within another conditional panel.

For example if I have a drop down list with two options: 1 and 2

selecting 1 will display one set of options and selecting 2 will display a different set of options.

But is it possible to have a conditional panel nested within these conditional panel so that I could have another drop down list within the inputs for option 1.

Here is some code for an example of what I am trying to do but this does not work

 selectInput("n", label = h3("Select Option"), 
                choices = list("1" = 1, "2" = 2),
                selected = 1),
  #1
  conditionalPanel(
    condition = "input.n == '1'",
    titlePanel("1 Options"),
    selectInput("b", label = h4("Select Option"), 
                choices = list("A" = 1, "B" = 2),
conditionalPanel(
condition = "input.b == '1'",
    titlePanel("1 Options")
),

conditionalPanel(
condition = "input.b == '2'",
    titlePanel("2 Options")
),

    )),

回答1:


Yes, you can easily nest conditional panels, more or less as you were attempting to. In your code you just had a few misplaced parentheses and extra commas. Here is a working app that does what you're asking, I think:

ui <- fluidPage(
  selectInput("n", label = h3("Select Option"), 
        choices = list("1" = 1, "2" = 2),
        selected = 1),
  conditionalPanel(
    condition = "input.n == '1'",
    titlePanel("1 Options"),
    selectInput("b", label = h4("Select Option"), 
          choices = list("A" = 1, "B" = 2)),
    conditionalPanel(
          condition = "input.b == '1'",
          titlePanel("1 Options")
      ),
      conditionalPanel(
          condition = "input.b == '2'",
          titlePanel("2 Options")
      )      
  )
)

server <- function(input, output){}

shinyApp(ui, server)


来源:https://stackoverflow.com/questions/36771669/r-shiny-conditional-panel-within-conditional-panel

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!