rstudio / shinydashboard

Shiny Dashboarding framework
https://rstudio.github.io/shinydashboard/
Other
893 stars 298 forks source link

input$sidebarmenu doesn't return the selected menuItem when generated dynamically. #346

Closed divyansh997 closed 4 years ago

divyansh997 commented 4 years ago

Here's a reproducible example:

library(shiny)
library(shinydashboard)

ui <- dashboardPage(
  dashboardHeader(), 
  dashboardSidebar(
    sidebarMenu(
      # Setting id makes input$tabs give the tabName of currently-selected tab
      id = "tabs",
      menuItem("Dashboard", tabName = "dashboard", icon = icon("dashboard")),
      menuItem("Widgets", icon = icon("th"), tabName = "widgets"),
      lapply(1:3, function(i) {
        shinydashboard::menuItem(id = paste0("item_", i),
                                 text = paste0("new_", i),
                                 icon = icon("dashboard"),
                                 tabName = paste0("new_", i))
      }),
      menuItem("Charts", icon = icon("bar-chart-o"),
               menuSubItem("Sub-item 1", tabName = "subitem1"),
               menuSubItem("Sub-item 2", tabName = "subitem2")
      )
    ),
    textOutput("res")
  ),
  dashboardBody(
    tabItems(
      tabItem("dashboard", "Dashboard tab content"),
      tabItem("widgets", "Widgets tab content"),
      tabItem("new_2", "Dynamic menuItem Content"),
      tabItem("subitem2", "Sub-item 2 tab content") 
    )
  )
)

server <- function(input, output, session) {
  output$res <- renderText({
    paste("You've selected:", input$tabs)
  })
}

shinyApp(ui, server)

The dynamically generated menuItems(with id = item_i) aren't returned via input$tabs even when they are childless. This has something to do with shinydashboard recognizing them as childfull even when they are not. This can be seen via dropdown signs being displayed on dynamically generated menuItems when you run the app:

image

Can you please confirm the issue or help me with correcting something in my code?

divyansh997 commented 4 years ago

I was wrongly passing in an id to menuItem when it doesn't support one. Removing it solves the problem.