Can I reference a tabItem in a conditional panel?

I'm currently working with the library bs4Dash, in my ui I have something like this:

tabItem( tabName = "one", ), tabItem( tabName = "two", ))

In the sidebar, i want to add a conditionalPanel that has the next condition: if i'm seeing the tab "one", show h2("Hello")

conditionalPanel( condition = 'tabItem == "one"', h2("Hello"))

Is there any way i can reference the tabItem in this conditionalPanel? The main idea is to have different texts in the sidebar when clicking different tabs. If not, is there any other way to do something like this?

I tried with 'input.all_panels, since that's how it works with other shiny R apps.

Upvotes: 1

Views: 90

Answers (1)

TimTeaFan
TimTeaFan

Reputation: 18581

You need to give the sidebarMenu and id argument which allows you to use this as an input that can be used in a conditionalPanel:

library(shiny)
library(bs4Dash)

ui <- bs4Dash::dashboardPage(
  
  dashboardHeader(title = "Simple Dashboard"),
  
  dashboardSidebar(
    
    sidebarMenu(
      id = "tabs", # <- This `id` is important
      menuItem("Tab 1", tabName = "one"),
      menuItem("Tab 2", tabName = "two")
    ),
    
    conditionalPanel("input.tabs === 'two'", # <- we use the `id` here!
      selectInput("variable", "Variable:",
                c("Cylinders" = "cyl",
                  "Transmission" = "am",
                  "Gears" = "gear")))
    
  ),
  
  dashboardBody(
    
    tabItems(
      
      tabItem("one",
              "This is the content of Tab 1."
               ),
      
      tabItem("two",
              "This is the content of Tab 2."
              )
    )
  )
)

server <- function(input, output) {
  
}

shinyApp(ui, server)

Upvotes: 0

Related Questions