shinydashboard sidebar menu dynamically created not work with conditionalPanel

670 views
Skip to first unread message

Jian Wang

unread,
Apr 30, 2016, 10:50:17 PM4/30/16
to Shiny - Web Framework for R
conditionalPanel works fairly reliably in the shinydashboard sidebar as excellently documented here.  However, to my chagrin, it fails me or more likely nowadays I fail it.  Here is the code to illustrate my point.  

The sidebarMenu is given an id="sbm", I wish to display a hr() if the "Tab 2" is clicked.  The conditionalPanel can be put in either ui or server function, the hr won't display.  In my browser inspect id="sbm" is nowhere to be found.  Instead, the "menu" appears to be the id (see the attachment).  The hr() still refuses to appear even I use the menu as id.  

I tried to do the same thing not using dynamically generated sidebar menu, I can see id="sbm" exactly where I expected to see.

So my questions are

  • why the sidebarMenu id couldn't be assigned, instead the name of ouput$xxx object is used as id
  • how to create conditionalPanel in sidebar menu when sidebar menu is dynamically generated

Any insight and advice is greatly appreciated.

==================================
library(shiny)
library(shinydashboard)

ui <- dashboardPage(
  dashboardHeader(title = "Dynamic sidebar"),
  dashboardSidebar(
    sidebarMenuOutput("menu")
    ,conditionalPanel(condition = "input.sbm == 'tab2'", hr())
  ),
  dashboardBody()
)

server <- function(input, output) {
  output$menu <- renderMenu({
    sidebarMenu(id = "sbm",
      menuItem("Home", tabName="tabHome", icon = icon("home")),
      menuItem("Tab 2", tabName="tab2", icon = icon("calendar"))
      # ,conditionalPanel(condition = "input.sbm == 'tab2'", hr())
      # ,conditionalPanel(condition = "input$sbm == 'tab2'", hr())
    )
  })
}



sbm.png

Jian Wang

unread,
May 10, 2016, 4:17:47 PM5/10/16
to Shiny - Web Framework for R
Could someone kindly take a look at this post?  Any help is greatly appreciated.

On Saturday, April 30, 2016 at 10:50:17 PM UTC-4, Jian Wang wrote:
conditionalPanel works fairly reliably in the shinydashboard sidebar as excellently documented here.  However, to my chagrin, it fails when sidebar menu is created dynamically.  Here is the code to illustrate my point.  

Jian Wang

unread,
May 10, 2016, 8:59:52 PM5/10/16
to Shiny - Web Framework for R
It turns out that the id was put in the wrong place.  This corrected minimal example works.

library(shiny)
library(shinydashboard)

ui <- dashboardPage(
  dashboardHeader(title = "Dynamic sidebar"),
  dashboardSidebar(
    sidebarMenu(id = "sbm",
    sidebarMenuOutput("menu"))
    ,conditionalPanel(condition = "input.sbm == 'tab2'", hr())
  ),
  dashboardBody()
)

server <- function(input, output) {
  output$menu <- renderMenu({
    sidebarMenu(
                menuItem("Home", tabName="tabHome", icon = icon("home")),
                menuItem("Tab 2", tabName="tab2", icon = icon("calendar"))
                # ,conditionalPanel(condition = "input.sbm == 'tab2'", hr())
                # ,conditionalPanel(condition = "input$sbm == 'tab2'", hr())
    )
  })
}

shinyApp(ui, server)
Reply all
Reply to author
Forward
0 new messages