Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

R shinydashboard dynamic menu selection

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 dynamic mode.

How can I make sure I have control on which menuItem's contents is shown at startup in this dynamic mode?

I have been searching all over through similar posts. Could not find anything that work so far. updateTabItems() did not seem to work.

Any ideas? thanks from advance.

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

Edit : Indentation problem that occurs with Romain's anwser hsh

like image 383
Servet Avatar asked Apr 20 '16 11:04

Servet


People also ask

Is it possible to create dynamic sibebar menus in R shinydashboard?

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 dynamic mode.

How do I record which menu item is selected in shinytest?

For shinytest to be able to record and replay which menu item is selected, the sidebarMenu () must have an id, and the menuItem s and menuSubItem s must have have tabName s. Another shinydashboard-specific component that is not seen in vanilla Shiny apps is the dropdownMenu (), one or more of which can be placed in the dashboard header.

What is dropdownmenu in shiny dashboard?

Another shinydashboard-specific component that is not seen in vanilla Shiny apps is the dropdownMenu (), one or more of which can be placed in the dashboard header. Like a sidebarMenu (), a dropdownMenu () can be either rendered statically in the UI, or dynamically in ther server function.

What are shiny inputs and outputs in a shinydashboard app?

In addition to all the normal Shiny inputs and outputs that can be present in a shinydashboard app, there are a few things that are specific to the structure of such apps. In particular, most shinydashboard apps have a sidebar. As of the 0.6 shinydashboard release, app authors can access the entire state of the sidebar as Shiny inputs.


1 Answers

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

like image 73
Romain Avatar answered Sep 16 '22 12:09

Romain