0

Lets take the example of the reference: https://rstudio.github.io/shinydashboard/structure.html#sidebar-menu-items-and-tabs. When put more items in the menuItem(), your associate tab don't works anymore. I tried in this simple modification in example below and just shown the widgets' tab:

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)
  • Please see [here](https://stackoverflow.com/questions/48210709/show-content-for-menuitem-when-menusubitems-exist-in-shiny-dashboard/48212169#48212169) for alternative answers. – YBS May 04 '22 at 00:26

1 Answers1

0

That is because childfull menuItem behaves differently as noted here. Therefore, you need to define a menuItem or a menSubItem within that dashboard page so that your desired content can be displayed.

Try this

  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)
YBS
  • 19,324
  • 2
  • 9
  • 27
  • Is it possible to define a `tabItem` to be displayed when the `menuItem(tabName = 'dashboard')` is selected? – Cris Jun 14 '22 at 16:14
  • I have not tried it. I think you can do it. Perhaps you can post your code, if you cannot do it. – YBS Jun 14 '22 at 16:34
  • Thank you, I did a better search and the issue was already discussed in this [SO question](https://stackoverflow.com/a/62834634/7612904) – Cris Jun 14 '22 at 16:39