菜单项的选项卡,在 Shinydashboard 中,将项目放入其中时不起作用

Tabs of the menuItem, in Shinydashboard, not working when put items inside

让我们以引用为例:https://rstudio.github.io/shinydashboard/structure.html#sidebar-menu-items-and-tabs。当在 menuItem() 中放置更多项目时,您的关联选项卡将不再起作用。我在下面的示例中尝试了这个简单的修改,只显示了小部件的选项卡:

sidebar <- dashboardSidebar(
  sidebarMenu(
    menuItem("Dashboard",
             tabName = "dashboard",
             icon = icon("dashboard"),
             selected = TRUE,
             startExpanded = TRUE,
             numericInput("num1",
                          "Put the First Number",
                          value = 1,
                          min = 0),
             numericInput("num2",
                          "Put the Second Number",
                          value = 1,
                          min = 0)
             ),
    
    menuItem("Widgets",
             icon = icon("th"),
             tabName = "widgets")
  )
)

body <- dashboardBody(
  tabItems(
    tabItem(tabName = "dashboard",
            h2("Dashboard tab content"),
            fluidRow(
              valueBoxOutput("box1", width = 6),
              valueBoxOutput("box2", width = 6)
            )
    ),
    
    tabItem(tabName = "widgets",
            h2("Widgets tab content")
    )
  )
)

# Put them together into a dashboardPage
ui <- dashboardPage(
  skin = "green",
  dashboardHeader(title = "Example"),
  sidebar,
  body
)

server <- function(input, output){
  
  output$box1 <- renderValueBox({
    valueBox(input$num1,
             "First Number",
             color = "aqua",
             icon = icon("chart-line"))
  })
  output$box2 <- renderValueBox({
    valueBox(input$num2,
             "Second Number",
             color = "aqua",
             icon = icon("chart-line"))
  })

}

shinyApp(ui, server)

这是因为 childfull menuItem 的行为与上述 here 不同。因此,您需要在该 dashboard 页面中定义一个 menuItemmenSubItem,以便显示您想要的内容。

试试这个

  sidebarMenu(id = "tabs",
    menuItem("Dashboard",
             tabName = "dashboard",
             icon = icon("tachometer-alt"),
             selected = TRUE,
             startExpanded = TRUE,
             #icon = icon(fontawesome:::fa_tbl[[1]][505]),
             menuItem("Sub-item 1", tabName = "subitem1"),
             ### menuSubItem("Sub-item 1", tabName = "subitem1"),  ## it can be menuSubItem instead of menuItem
             numericInput("num1",
                          "Put the First Number",
                          value = 1,
                          min = 0),
             numericInput("num2",
                          "Put the Second Number",
                          value = 2,
                          min = 0)
    ),
    
    menuItem("Widgets",
             icon = icon("th"),
             tabName = "widgets")
  )
)

body <- shinydashboard::dashboardBody(
  tabItems(
    tabItem(tabName = "subitem1",
            h2("Sub item1 tab content in Dashboard"),
            fluidRow(
              valueBoxOutput("box1", width = 6),
              valueBoxOutput("box2", width = 6)
            )
    ),
    
    tabItem(tabName = "widgets",
            h2("Widgets tab content")
    )
  )
)

# Put them together into a dashboardPage
ui <- shinydashboard::dashboardPage(
  skin = "green",
  shinydashboard::dashboardHeader(title = "Example"),
  sidebar,
  body
)

server <- function(input, output, session){
  
  output$box1 <- renderValueBox({
    valueBox(input$num1,
             "First Number",
             color = "aqua",
             icon = icon("chart-line"))
  })
  output$box2 <- renderValueBox({
    valueBox(input$num2,
             "Second Number",
             color = "aqua",
             icon = icon("chart-line"))
  })
  observe({print(input$tabs)})
}

shinyApp(ui, server)