Search code examples
rshinyshinydashboardaction-button

How to navigate between different tabItems in shinydashboard through buttons


I have made a shiny app (in shinydashboard) with many menu and submenu items. Like the following tree structure


    sidebarMenu(
      menuItem("Item 1", tabName = "item1", icon = icon("dashboard")),
      menuItem("Item 2", tabName = "item2", icon = icon("th"),
               menuSubItem("Subitem1", tabName = "subitem1"),
               menuSubItem("Subitem2", tabName = "subitem2")),
      menuItem("Item 3", tabName = "item3", icon = icon("bolt"),
               menuSubItem("Subitem3", tabName = "subitem3"),
               menuSubItem("Subitem4", tabName = "subitem4")
               )
    )

In first page I have four actionbuttons. I want that when any actionbutton is clicked corresponding "subitem#" page may be opened. I am unable to do it with shinydashboard::updateTabItems. Please help. Example code/app is

library(shiny)
library(shinydashboard)

ui <- dashboardPage(
  dashboardHeader(title = "Dynamic Tab Example"),
  dashboardSidebar(
    sidebarMenu(
      menuItem("Item 1", tabName = "item1", icon = icon("dashboard")),
      menuItem("Item 2", tabName = "item2", icon = icon("th"),
               menuSubItem("Subitem1", tabName = "subitem1"),
               menuSubItem("Subitem2", tabName = "subitem2")),
      menuItem("Item 3", tabName = "item3", icon = icon("bolt"),
               menuSubItem("Subitem3", tabName = "subitem3"),
               menuSubItem("Subitem4", tabName = "subitem4")
               )
    )
  ),
  dashboardBody(
    tabItems(
      tabItem(
        "item1",
        actionButton("button1", "navigate to subitem1"),
        actionButton("button2", "navigate to subitem2"),
        actionButton("button3", "navigate to subitem3"),
        actionButton("button4", "navigate to subitem4")
      ),
      tabItem("subitem1",
              box(
                title = "Item 1",
                actionButton("button1", "Button 1")
              )
      ),
      tabItem("subitem2",
              box(
                title = "Item 2 Content",
                "This is the content for Item 2."
              )
      ),
      tabItem("subitem3",
              box(
                title = "Item 3 Content",
                "This is the content for Item 3."
              )
      ),
      tabItem("subitem4",
              box(
                title = "Item 4 Content",
                "This is the content for Item 4."
              )
      )
    )
  )
)

server <- function(input, output, session) {
  

}

shinyApp(ui, server)

Solution

  • Here is a proposition : remove the actionButton "button1" under subitem1 to avoid having two buttons with same id / add an id to your menu, here "tabs" / use updateTabItems with detection of the id of clicked button

    library(shiny)
    library(shinydashboard)
    library(purrr)
    
    ui <- dashboardPage(
      dashboardHeader(title = "Dynamic Tab Example"),
      dashboardSidebar(
        sidebarMenu(id = "tabs",
          menuItem("Item 1", tabName = "item1", icon = icon("dashboard")),
          menuItem("Item 2", tabName = "item2", icon = icon("th"),
                   menuSubItem("Subitem1", tabName = "subitem1"),
                   menuSubItem("Subitem2", tabName = "subitem2")),
          menuItem("Item 3", tabName = "item3", icon = icon("bolt"),
                   menuSubItem("Subitem3", tabName = "subitem3"),
                   menuSubItem("Subitem4", tabName = "subitem4")
          )
        )
      ),
      dashboardBody(
        tabItems(
          tabItem(
            "item1",
            actionButton("button1", "navigate to subitem1"),
            actionButton("button2", "navigate to subitem2"),
            actionButton("button3", "navigate to subitem3"),
            actionButton("button4", "navigate to subitem4")
          ),
          tabItem("subitem1",
                  box(
                    title = "Item 1"
                  )
          ),
          tabItem("subitem2",
                  box(
                    title = "Item 2 Content",
                    "This is the content for Item 2."
                  )
          ),
          tabItem("subitem3",
                  box(
                    title = "Item 3 Content",
                    "This is the content for Item 3."
                  )
          ),
          tabItem("subitem4",
                  box(
                    title = "Item 4 Content",
                    "This is the content for Item 4."
                  )
          )
        )
      )
    )
    
    server <- function(input, output, session) {
      
      
      paste0('button', 1:4) %>% 
        map(~ observeEvent(input[[.x]], {
          number = gsub("\\D", "", .x)
          updateTabItems(session, "tabs", paste0("subitem", number))
        }))
      
    }
    
    shinyApp(ui, server)