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

Keep the choice made as constant in a shiny widget that exists inside a dynamic tabsetPanel()

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 :

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

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