我在使用 Shiny 仪表板侧边栏时遇到问题

时间:2021-05-21 17:42:30

标签: r shiny shinydashboard

我像这样创建了闪亮的仪表板侧边栏 我通过 MenuItemtabItem 链接到 tabName,并在每个 tabItem 中放置了 tabsetPanel

我希望,当我选择 A 组时,能够显示子组 A1、A2 和 A3 的不同面板。我想对 B 组及其子组做同样的事情。我不能这样做,因为只显示 B 组中的面板。我想保留这个仪表板侧边栏架构。

下面是我的脚本

ui <- dashboardPage(
  dashboardHeader(title = "DASHBOARD"),
  dashboardSidebar(width = 250,
                   sidebarMenu(
                     menuItem( "Group A",tabName = "gpA",
                               selectInput(inputId = "Var1",label="variable 1", choices = c("Total","cat1","cat2"),
                                                               selected="Total"),
                               selectInput(inputId = "Var2",label="variable 2", choices = c("Total","cat1","cat2"),
                                                               selected="Total")),
                     
                     menuItem("Group B",tabName = "gpB")
                     )
  
  ),
  dashboardBody(
    
    tabItems(
      
      tabItem(
        tabName = "gpA",
        fluidRow(box(selectInput(inputId = "Hospital1",choices = c(LETTERS),
                                 label = "Choice hospital 1"),
                     width = 6,height = 70,
                     solidHeader = TRUE),
                 box(selectInput(inputId = "Hopital2",choices = rev(c(LETTERS)),
                                 label = "Choice hospital 2"),
                     width = 6,height = 70,
                     solidHeader = TRUE)),
        
        fluidRow( tabsetPanel(
          tabPanel("Sub group A2", 
                   box(leafletOutput("")),box(leafletOutput(""))
                                      ),
          tabPanel("Sub group A3", box(leafletOutput("")),
                   box(leafletOutput(""))),
          tabPanel("Concurrence", leafletOutput(""))
                                    ))
        ),
      
      tabItem(
        tabName = "gpB",
        fluidRow(box(selectInput(inputId = "Commune",choices = c(letters),
                                 label = "choice a Zip code"),
                     width = 6,height = 70,
                     solidHeader = TRUE)),
        fluidRow( tabsetPanel(
          tabPanel("Sub group B1", 
                   leafletOutput("")
                   ),
          tabPanel("Sub group B2", 
                   leafletOutput(""))
                                    ))
        )
      )
    )
  )


server <- function(input, output) {

}

shinyApp(ui, server)

1 个答案:

答案 0 :(得分:1)

由于您在第一个 selectInput 项中使用多个 sidebarmenu(子项),因此您需要在其下使用 menuSubItem(或 menuItem)才能使用tabName。这里的 tabName gpA1 是您需要在 dashboardBody 中引用的内容。当您有子项目时,您无法使用 gpA 访问标签面板。试试这个

ui <- dashboardPage(
  dashboardHeader(title = "DASHBOARD"),
  dashboardSidebar(width = 250,
                   sidebarMenu( id="tabs",
                     menuItem( "Group A", tabName = "gpA",
                               selectInput(inputId = "Var1",label="variable 1", choices = c("Total","cat1","cat2"),
                                           selected="Total"),
                               selectInput(inputId = "Var2",label="variable 2", choices = c("Total","cat1","cat2"),
                                           selected="Total"),
                               menuSubItem("My Group A", tabName="gpA1",   ###   <---- refer to this tabName in dashboardBody
                                           icon = icon("line-chart"))
                               ),
                     
                     menuItem("Group B",tabName = "gpB")
                   )
                   
  ),
  dashboardBody(
    
    tabItems(
      
      tabItem(
        tabName = "gpA1",
        fluidRow(box(selectInput(inputId = "Hospital1",choices = c(LETTERS),
                                 label = "Choice hospital 1"),
                     width = 6,height = 70,
                     solidHeader = TRUE),
                 box(selectInput(inputId = "Hopital2",choices = rev(c(LETTERS)),
                                 label = "Choice hospital 2"),
                     width = 6,height = 70,
                     solidHeader = TRUE)),
        
        fluidRow( tabsetPanel(
          tabPanel("Sub group A2", 
                   box(leafletOutput("")),box(leafletOutput(""))
          ),
          tabPanel("Sub group A3", box(leafletOutput("")),
                   box(leafletOutput(""))),
          tabPanel("Concurrence", leafletOutput(""))
        ))
      ),
      
      tabItem(
        tabName = "gpB",
        fluidRow(box(selectInput(inputId = "Commune",choices = c(letters),
                                 label = "choice a Zip code"),
                     width = 6,height = 70,
                     solidHeader = TRUE)),
        fluidRow( tabsetPanel(
          tabPanel("Sub group B1",
                   leafletOutput("")
          ),
          tabPanel("Sub group B2",
                   leafletOutput(""))
        ))
      )
    )
  )
)


server <- function(input, output) {
  
}

shinyApp(ui, server)

output

相关问题