Shiny with tabsetpanels and conditionpanel not hiding what it should

I have a shiny app with tabsetpanels and conditional panels in the sidebar panel. The problem is that one of the conditional panels also appears when it shouldn't. Below is the code: I show the name of the panel in the sidebar panel. If I click "Assumptions and scenarios" and then on "Assumptions" in the sidebar panel I expect to only see "Assumptions" but I also get "Car aggregate". I have studied the code for over two hours, playing around with it, but I can't find the error (or pehaps it is a bug). I would very much appreciate some help. This is the code

ui <- fluidPage(
  sidebarLayout(
    sidebarPanel(
      conditionalPanel(
        condition = "input.tabs_a == 't_ass'",
        h4("Assumptions")
      ),
      conditionalPanel(
        condition = "input.tabs_c == 't_cara'",
        h4("Car aggregate")
      ), # End conditionalPanel
      conditionalPanel(
        condition = "input.tabs_c == 't_carf'",
        h4("Car fuel")
      ), # End conditionalPanel
      conditionalPanel(
        condition = "input.tabs_c == 't_carfp'",
        h4("Car fuel power:")
      ) # End conditionalPanel
    ),    # End sidebarPanel
    mainPanel(
      tabsetPanel(
        type = "tabs", 
        tabPanel( 
          "Assumptions and Scenarios",
          tabsetPanel(
            type = "tabs",  id = "tabs_a",
            tabPanel(
              "Assumptions",  value = "t_ass"
            ), # Close tabpanel t_ass,
            tabPanel(
              "Scenarios", value = "t_scen"
            ) # Close tabpanel t_scen
          ) # Close tabsetpanel
        ), # Close tabPanel t_Ass
        tabPanel(
          "Transport results",
          tabsetPanel(
            type = "tabs", id = "tabs_c",
            tabPanel(
              "Cars: Aggregated", value = "t_cara"
            ),  # Close tabpanel t_cara
            tabPanel(
              "Cars: Fuel",  value = "t_carf"
            ), # Close tabpanel
            tabPanel(
              id = "t_carfp",
              "Cars: Fuel vs Power",  value = "t_carfp",
            ) # Close tabpanel t_carfp
          ) # Close tabset tabs_c
        ) # Close tabpanel Transport results
      ) # Close tabset tabs_a
    ) # End mainPanel
  )  # End sidebarLayout
) # End fluidPage

server <- function(input, output) {
}

shinyApp(ui = ui, server = server)

Any help would be aprreciated
Renger

you could try adding additional logical conditions to change the behavioud, in this example I only show the tabs_c related panels when the tabs_a is set to t_scen (and not t_ass)

library(shiny)
ui <- fluidPage(
  sidebarLayout(
    sidebarPanel(
      conditionalPanel(
        condition = "input.tabs_a == 't_ass'",
        h4("Assumptions")
      ),
      conditionalPanel(
        condition = "input.tabs_c == 't_cara' &&  input.tabs_a == 't_scen'",
        h4("Car aggregate")
      ), # End conditionalPanel
      conditionalPanel(
        condition = "input.tabs_c == 't_carf' &&  input.tabs_a == 't_scen'",
        h4("Car fuel")
      ), # End conditionalPanel
      conditionalPanel(
        condition = "input.tabs_c == 't_carfp' &&  input.tabs_a == 't_scen'",
        h4("Car fuel power:")
      ) # End conditionalPanel
    ),    # End sidebarPanel
    mainPanel(
      tabsetPanel(
        type = "tabs", 
        tabPanel( 
          "Assumptions and Scenarios",
          tabsetPanel(
            type = "tabs",  id = "tabs_a",
            tabPanel(
              "Assumptions",  value = "t_ass"
            ), # Close tabpanel t_ass,
            tabPanel(
              "Scenarios", value = "t_scen"
            ) # Close tabpanel t_scen
          ) # Close tabsetpanel
        ), # Close tabPanel
        tabPanel(
          "Transport results",
          tabsetPanel(
            type = "tabs", id = "tabs_c",
            tabPanel(
              "Cars: Aggregated", value = "t_cara"
            ),  # Close tabpanel t_cara
            tabPanel(
              "Cars: Fuel",  value = "t_carf"
            ), # Close tabpanel
            tabPanel(
              id = "t_carfp",
              "Cars: Fuel vs Power",  value = "t_carfp",
            ) # Close tabpanel t_carfp
          ) # Close tabset tabs_c
        ) # Close tabpanel Transport results
      ) # Close tabset tabs_a
    ) # End mainPanel
  )  # End sidebarLayout
) # End fluidPage

server <- function(input, output) {
}

shinyApp(ui = ui, server = server)

Hi
Thanks, but when adding more conditional panels, the problem returns. E.g. adding

      conditionalPanel(
        condition = "input.tabs_a == 't_scen'",
        h4("Scenarios")
      ),
      

I tried several && and != but nothing seems to work.
To me it looks like a bug. What is the idea behind the &&-condtion.

Cheers
Renger

I found the solution: It needs two conditions and one should aim at the tabsetpanel-id and the next at the tabpanel-value. Here is the code:

ui <- fluidPage(
  sidebarLayout(
    sidebarPanel(
      conditionalPanel(
        condition = "input.tabs == 'a1' && input.tabs_a == 't_ass'", ns = NS(NULL),
        h4("Assumptions")
      ),
      conditionalPanel(
        condition = "input.tabs == 'a1' && input.tabs_a == 't_scen'",  ns = NS(NULL),
        h4("Scenarios")
      ),
      conditionalPanel(
        condition = "input.tabs == 't1' && input.tabs_c == 't_cara' ",  ns = NS(NULL),
        h4("Car aggregate")
      ), # End conditionalPanel
      conditionalPanel(
        condition = "input.tabs == 't1' && input.tabs_c == 't_carf' ",  ns = NS(NULL),
        h4("Car fuel")
      ), # End conditionalPanel
      conditionalPanel(
        condition = "input.tabs == 't1' && input.tabs_c == 't_carfp' ",
        h4("Car fuel power:")
      ) # End conditionalPanel
    ),    # End sidebarPanel
    mainPanel(
      tabsetPanel(
        type = "tabs", id = "tabs",
        tabPanel(id = 'a1', value = 'a1', 
                 "Assumptions and Scenarios",
                 tabsetPanel(
                   type = "tabs",  id = "tabs_a", 
                   tabPanel(
                     "Assumptions",  value = "t_ass"
                   ), # Close tabpanel t_ass,
                   tabPanel(
                     "Scenarios", value = "t_scen"
                   ) # Close tabpanel t_scen
                 ) # Close tabsetpanel
        ), # Close tabPanel t_Ass
        tabPanel(id = "t1", value = "t1",
                 "Transport results",
                 tabsetPanel(
                   type = "tabs", id = "tabs_c",
                   tabPanel(
                     "Cars: Aggregated", value = "t_cara"
                   ),  # Close tabpanel t_cara
                   tabPanel(
                     "Cars: Fuel",  value = "t_carf"
                   ), # Close tabpanel
                   tabPanel(
                     id = "t_carfp",
                     "Cars: Fuel vs Power",  value = "t_carfp",
                   ) # Close tabpanel t_carfp
                 ) # Close tabset tabs_c
        ) # Close tabpanel Transport results
      ) # Close tabset tabs_a
    ) # End mainPanel
  )  # End sidebarLayout
) # End fluidPage

server <- function(input, output) {
}

shinyApp(ui = ui, server = server)

Cheers
Renger

1 Like

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.