Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Dynamic sidebar menu RShiny

I have a problem with my dashboard. I want create a dynamic sidebar menu, but by default, Menu item don't work. The user has to clic on it to show it. I have find an example on this problem https://github.com/rstudio/shinydashboard/issues/71 but the solution don't work. If you have ideas... thank you in advance

library(shiny)
library(shinydashboard)

ui <- dashboardPage(
  dashboardHeader(title = "Dynamic sidebar"),
  dashboardSidebar(
    sidebarMenuOutput("menu")
  ),
  dashboardBody(tabItems(
    tabItem(tabName = "dashboard", h2("Dashboard tab content"))
  ))
)

server <- function(input, output) {
  output$menu <- renderMenu({
    sidebarMenu(id="mytabs",
      menuItem("Menu item", tabName="dashboard", icon = icon("calendar"))
    )
  })
}

shinyApp(ui, server)
like image 766
CClaire Avatar asked Jun 07 '26 01:06

CClaire


1 Answers

Here is a solution using updateTabItems.

library(shiny)
library(shinydashboard)

ui <- dashboardPage(
  dashboardHeader(title = "Dynamic sidebar"),
  dashboardSidebar(
    sidebarMenu(id="mytabs",
      sidebarMenuOutput("menu")
    )
  ),
  dashboardBody(tabItems(
    tabItem(tabName = "dashboard", h2("Dashboard tab content"))
  ))
)

server <- function(input, output, session) {
  output$menu <- renderMenu({
    sidebarMenu(
              menuItem("Menu item", tabName="dashboard", icon = icon("calendar"))
    )
  })
  isolate({updateTabItems(session, "mytabs", "dashboard")})
}

shinyApp(ui, server)

To extend to dynamic menu you can see this exemple. R shinydashboard dynamic menu selection

Edit : I think the isolate is not needed but I like to put it in a way to improve the reading of the code

like image 161
Romain Avatar answered Jun 09 '26 12:06

Romain