Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

How to dynamically show and hide a tabPanel in a navbarPage?

I am creating a shiny app using a navbarPage structure and I have one case that I can’t resolve.
I have two tabPanels:

  • tab1,
  • tab2.

tab1 should be available all the time, but the visibility of tab2 should depend on the value of the checkbox located in tab1.

I know how to add a tab using appendTab, but how to hide/remove it?

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

Below is the code that I created when trying to find a solution.

library(shiny)

ui <- navbarPage(
  id = "navbarPage_id",
  "Dynamic TabPanel Example",
  tabPanel("Tab 1", value = "tab1", uiOutput("CHECKBOX"))
)


server <- function(input, output) {
  output$CHECKBOX <- renderUI({
    checkboxInput(inputId = "checkboxInput_id", label = "Show", value = TRUE)
  })
  
  observeEvent(input$checkboxInput_id, {
    if(input$checkboxInput_id){
      appendTab(inputId = "navbarPage_id", tabPanel("Tab 2", value = "tab2"))
    }
  })
}

shinyApp(ui, server)

>Solution :

Here is an example using showTab and hideTab.

library(shiny)

ui <- navbarPage(
    id = "tabs",
    "Dynamic TabPanel Example",
    tabPanel("Tab 1", value = "tab1", uiOutput("CHECKBOX")),
    tabPanel("Tab 2", value = "tab2")
)

server <- function(input, output) {
    output$CHECKBOX <- renderUI({
        checkboxInput(inputId = "checkboxInput_id",
                      label = "Show",
                      value = TRUE)
    })
    
    observeEvent(input$checkboxInput_id, {
        if (input$checkboxInput_id) {
            showTab(inputId = "tabs", target = "tab2")
        } else {
            hideTab(inputId = "tabs", target = "tab2")
        }
    })
    
}

shinyApp(ui, server)

enter image description here

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading