3
votes

I want to develop an app with a layout similar to Radiant in the Shiny gallery (https://shiny.rstudio.com/gallery/radiant.html). In that app, the sidebarPanel changes for each tabPanel that exists in the mainPanel. How is that accomplished? This picture shows my layout, where I want the sidebarPanel (empty now) to change based on which tab (Metadata, Raw Data, QC Data) is chosen by the user. Does someone either know how to do this or can you point me to where the ui code in the Radiant app is located?

enter image description here

EDIT: After receiving the answer below, I edited the code to look like this, rather than put the sidebar in a new function. It is not working yet, however. Shouldn't it? What's still wrong?

    ui <- navbarPage(title = "SD Mesonet Quality Control", id = "navbarPage",
                     tabPanel(title = 'Data',
                              sidebarLayout(
                                sidebarPanel(
                                  conditionalPanel(condition="input.tabselected == 1",
                                                   actionButton('bt','button Tab 1')
                                  ),
                                  conditionalPanel(condition="input.tabselected == 2",
                                                   selectInput('select','choice',choices=c("A","B"))
                                  )
                                ),
                                mainPanel(
                                  tabsetPanel(type = "tabs", id = "tabselected",
                                              tabPanel("Instrumentation", value = 1, plotOutput("plot")),
                                              tabPanel("Metadata", value = 2, plotOutput("plot"))
                                  )
                                )
                              ),
                     )
    )
    
    
    server <- function(input,output){
      
    }
    
    shinyApp(ui,server)
1

1 Answers

2
votes

This is a conditional panel, see this use case or this demo.
To answer your question, you can link the condition of the panel to the id of the tab :

library(shinydashboard)
library(shiny)
sidebar <- dashboardSidebar(
    conditionalPanel(condition="input.tabselected==1",
                     actionButton('bt','button Tab 1')
                     ),

    conditionalPanel(condition="input.tabselected==2",
                     selectInput('select','choice',choices=c("A","B"))
                     )
    )
)

# Header ----
header <- dashboardHeader(title="Test conditional panel")

# Body ----
body <- dashboardBody(
  mainPanel(
    tabsetPanel(
      tabPanel("tab1", value=1,
                h4("Tab 1 content")),
      tabPanel("tab2", value=2,
               h4("Tab 2 content")),
      id = "tabselected"
    )
  )
)
ui <- dashboardPage(header, sidebar, body)

shinyApp(ui=ui,server=server)

Tab 1 selected : enter image description here Tab 2 selected : enter image description here