shinydashboard icon indicating copy to clipboard operation
shinydashboard copied to clipboard

Feature request: allow NULL menu items in the UI

Open daattali opened this issue 3 years ago • 2 comments

This comes up when you want to include a conditional.

For example

library(shiny)
library(shinydashboard)

show_tab_2 <- FALSE

ui <- dashboardPage(
  dashboardHeader(),
  dashboardSidebar(
    sidebarMenu(
      menuItem("tab1", tabName = "tab1"),
      if (show_tab_2) menuItem("tab2", tabName = "tab2")
    )
  ),
  dashboardBody(
    tabItems(
      tabItem("tab1", "tab1"),
      if (show_tab_2) tabItem("tab2", "tab2")
    )
  )
)
server <- function(input, output) {}
shinyApp(ui, server)

This results in an error since the if statement results in a NULL ui item. It would be useful if shinydashboard knew to just ignore NULL items

daattali avatar Feb 12 '21 18:02 daattali

The problems seems to be with the tabItems() rather than sidebarMenu()

daattali avatar Feb 12 '21 18:02 daattali

This would be useful!

For those interested in a workaround:

library(shiny)
library(shinydashboard)

show_tab_2 <- FALSE

ui <- dashboardPage(
  dashboardHeader(),
  dashboardSidebar(
    sidebarMenu(
      menuItem("tab1", tabName = "tab1"),
      if (show_tab_2){menuItem("tab2", tabName = "tab2")}
    )
  ),
  dashboardBody(
    tabItems(
      tabItem("tab1", "tab1"),
      if (show_tab_2){tabItem("tab2", "tab2")} else {
        div(class = 'tab-pane')
      }
    )
  )
)
server <- function(input, output) {}
shinyApp(ui, server)

ismirsehregal avatar Sep 28 '21 12:09 ismirsehregal