Im trying to create a shiny app in which the user displays or not a secret tab. The issue is that I want the radioButtons() inside the main panel to keep the choice that I give to it if I change it and return to the selected one if I choose to hide or display the secret tab.
library(shiny)
ui <- fluidPage(
sidebarLayout(
sidebarPanel(
radioButtons("displaySecretTab", "Display Secret Tab", choices = c("Yes", "No"))
),
mainPanel(
uiOutput("tabsets")
)
)
)
server <- function(input, output, session) {
output$tabsets<-renderUI({
if(input$displaySecretTab=="Yes"){
tabsetPanel(
id="tabs",
tabPanel("tab 1",
radioButtons("ftype", "Choose file type", choices = c("CSV" = "csv", "Excel (.xlsx)" = "xlsx", "Excel (.xls)" = "xls"), selected = "csv")),
tabPanel("secret"))
}
else{
tabsetPanel(
id="tabs",
tabPanel("tab 1",
radioButtons("ftype", "Choose file type", choices = c("CSV" = "csv", "Excel (.xlsx)" = "xlsx", "Excel (.xls)" = "xls"), selected = "csv")),
)
}
})
}
shinyApp(ui, server)
>Solution :
I would use insertTab/removeTab:
library(shiny)
ui <- fluidPage(
sidebarLayout(
sidebarPanel(
radioButtons("displaySecretTab", "Display Secret Tab", choices = c("Yes", "No"))
),
mainPanel(
tabsetPanel(
id="tabs",
tabPanel("tab 1",
radioButtons("ftype", "Choose file type", choices = c("CSV" = "csv", "Excel (.xlsx)" = "xlsx", "Excel (.xls)" = "xls"), selected = "csv")),
)
)
)
)
server <- function(input, output, session) {
observeEvent(input$displaySecretTab, {
if(input$displaySecretTab=="Yes"){
insertTab(
"tabs",
tabPanel("secret")
)
} else {
removeTab("tabs", "secret")
}
})
}
shinyApp(ui, server)