3

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)
DeanAttali
  • 25,268
  • 10
  • 92
  • 118
CClaire
  • 423
  • 7
  • 17
  • 1
    Rather than using the renderMenu functions, it's much easier to just use htmlOutput, and renderUI, then you can put whatever you want in there. – Shape May 04 '16 at 16:51
  • Given code worked for me – Kush Patel May 04 '16 at 18:09
  • @Shape Yes but I have the same problem : `ui <- dashboardPage( dashboardHeader(title = "Dynamic sidebar"), dashboardSidebar( uiOutput("menu") ), dashboardBody(tabItems( tabItem(tabName = "dashboard", h2("Dashboard tab content")) )) ) server <- function(input, output) { output$menu <- renderUI({ sidebarMenu(id="mytabs", menuItem("Menu item", tabName="dashboard", icon = icon("calendar"))) }) } shinyApp(ui, server)` – CClaire May 09 '16 at 07:44
  • @kppatel-patel Yes it work but it's not what I want, the user doesnt click on Menu item but move directly to content Menu item. – CClaire May 09 '16 at 07:44

1 Answers1

7

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

Community
  • 1
  • 1
Romain
  • 440
  • 4
  • 17