Home > Enterprise >  conditionalpanel in shiny tabBox
conditionalpanel in shiny tabBox

Time:02-02

In the following shiny app I would like to have the second tab inside the box if user selection is sh, So I was expecting the conditionalPanel command does the trick ! but it is not working:

library(shiny)
library(shinydashboard)

ui <- dashboardPage(
  skin = "green",
  dashboardHeader(title = "conditional tabBox"),
  dashboardSidebar(
    width = 250,
    sidebarMenu(
      id = "tabs",
      
      radioButtons(inputId = "layout_status",
                   label = "",
                   inline = TRUE,
                   choices = c("Layout" = "ly","Shape file" = "sh"), 
                   selected = "ly")
      
    )
  ),
  dashboardBody(
      tabBox(width = 12,
        tabPanel(
          id = "p1",
          title = HTML("<p style='color:#2071B5'><b>TAB 1</b></p>")
        ),
        conditionalPanel(condition = " input.layout_status=='sh' ",
        tabPanel(
          id = "p2",
          title = HTML("<p style='color:#2071B5'><b>TAB 2</b></p>")
        )
      )
    )
  )
)

server <- function(input, output) { }
shinyApp(ui, server)

CodePudding user response:

tabBox expects tabPanel elements to be passed to its ... argument - conditionalPanel elements are not allowed.

However you can use hideTab / showTab instead:

library(shiny)
library(shinydashboard)

ui <- dashboardPage(
  skin = "green",
  dashboardHeader(title = "conditional tabBox"),
  dashboardSidebar(
    width = 250,
    sidebarMenu(
      id = "tabs",
      radioButtons(inputId = "layout_status",
                   label = "",
                   inline = TRUE,
                   choices = c("Layout" = "ly","Shape file" = "sh"), 
                   selected = "ly")
    )
  ),
  dashboardBody(
    tabBox(
      tabPanel(
        value = "p1",
        title = HTML("<p style='color:#2071B5'><b>TAB 1</b></p>")
      ),
      tabPanel(
        value = "p2",
        title = HTML("<p style='color:#2071B5'><b>TAB 2</b></p>")
      ),
      id = "tabBoxID", width = 12)
  )
)

server <- function(input, output, session) {
  observeEvent(input$layout_status, {
    if(input$layout_status == 'sh'){
      showTab(inputId = "tabBoxID", target = "p2", select = TRUE)
    } else {
      hideTab(inputId = "tabBoxID", target = "p2")
    }
  })
}

shinyApp(ui, server)
  • Related