R shinydashboard dynamic menu selection

前端 未结 2 553
逝去的感伤
逝去的感伤 2021-01-01 01:16

I have created dynamic sibebar menus in R shinydashboard. Even though I use selected = TRUE, no menuItem associated with a menu gets selected at startup in this

相关标签:
2条回答
  • 2021-01-01 01:24

    Why not using an observer which is called only once at app init

    observe({ # called only once at app init
      updateTabItems(session, "tabs", "m2")
    })
    
    0 讨论(0)
  • 2021-01-01 01:37

    You have effectively to use updateTabItems(). To do so you have to set up an id for the sidebarMenu and update the corresponding menuItem or menuSubItem.

    For your specific case you should do something like this:

    library(shiny)
    library(shinydashboard)
    ui <- dashboardPage(
      dashboardHeader(title = "Dynamic sidebar"),
      dashboardSidebar(
        sidebarMenu(id="tabs",
        sidebarMenuOutput("menu")
        )
      ),
      dashboardBody(
        tabItems(
          tabItem(tabName = "m1", p("Menu content 1") ),
          tabItem(tabName = "m2", p("Menu content 2") )
        )
      )
    )
    server <- function(input, output,session) {
    
      output$menu <- renderMenu({
        sidebarMenu(
               menuItem("Menu item1", tabName="m1", icon = icon("calendar")),
               menuItem("Menu item2", tabName="m2", icon = icon("database"))
               )
      })
      isolate({updateTabItems(session, "tabs", "m2")})
    }
    shinyApp(ui, server)
    

    Edited version to remove the indentation problem

    0 讨论(0)
提交回复
热议问题