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?
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)
