Select active menu item dynamically in shinydashboards

525 views
Skip to first unread message

Isam Haddad

unread,
Aug 13, 2015, 10:22:42 AM8/13/15
to Shiny - Web Framework for R
Hey there,

I try to control in a shinydashboard app which menuItem is selected by listening to reactive values. But this seems to have no effect. Here is a minimal example:

library(shinydashboard)

ui
<- dashboardPage(
  dashboardHeader
(title = "Sidebar inputs"),
  dashboardSidebar
(
    sidebarMenu
(
      menuItemOutput
( "mi_a" ),
      menuItemOutput
( "mi_b" ),
      menuItemOutput
( "mi_c" )
   
),
    selectInput
("selected", "Select active", c("a", "b", "c")),
    checkboxGroupInput
("available", "Select available", c("a", "b", "c"))
 
),
  dashboardBody
(
    tabItems
(
      tabItem
(tabName = "a", h2("A")),
      tabItem
(tabName = "b", h2("B")),
      tabItem
(tabName = "c", h2("C"))
   
)
 
)
)

server
<- function(input, output) {
  observe
({
   
print(paste( "Selected: ", input$selected ))
    output$mi_a
<- renderMenu({
      menuItem
("A", tabName = "a", selected = input$selected=="a")
   
})
    output$mi_b
<- renderMenu({
      menuItem
("B", tabName = "b", selected = input$selected=="b")
   
})
    output$mi_c
<- renderMenu({
      menuItem
("C", tabName = "c", selected = input$selected=="c")
   
})
 
})
}

shinyApp
(ui, server)

Any idea how to achieve what I want?

Another question in this regard: How can I achieve that based on another reactive value (input$available in this case) only selected tabItems are available at all. Ideally I do not want to only remove the menuItem, but the entire content in the body. I tried to play around with conditionalPanel but also without effect.

Many thanks,
Isam
Reply all
Reply to author
Forward
0 new messages