rshinyshinydashboard

Move from one to the other tabItem by using actionButton() in shiny dashboard


Im trying to add an actionButton() in the fist tabItem that when pressed will move to the second tabItem named widgets. I cannot understand what am I doing wrong.

## app.R ##
library(shinydashboard)

ui <- dashboardPage(
  dashboardHeader(title = "Basic dashboard"),
  dashboardSidebar(
    sidebarMenu(id="inTabset",
      menuItem("Dashboard", tabName = "dashboard", icon = icon("dashboard")),
      menuItem("Widgets", tabName = "widgets", icon = icon("th"))
    )
  ),
  dashboardBody(
    
  )
)

server <- function(input, output) {
  tabItems(
    # First tab content
    tabItem(tabName = "dashboard",
            "dashboard",
            actionButton("nextt","Next") 
                         
    ),
    
    # Second tab content
    tabItem(tabName = "widgets",
            "widgets"
    )
  )
  observeEvent(input$nextt, {
    updateTabItems(session, "inTabset",selected = "dashboard")
  })
}

shinyApp(ui, server)

Solution

  • The issue is that you added the tabItems in the server instead of inside dashboardBody. Additionally, to switch from dashboard to widgets you have to do selected="widgets" in updateTabItems and finally your server should have an argument session:

    library(shinydashboard)
    library(shiny)
    
    ui <- dashboardPage(
      dashboardHeader(title = "Basic dashboard"),
      dashboardSidebar(
        sidebarMenu(
          id = "inTabset",
          menuItem("Dashboard", tabName = "dashboard", icon = icon("dashboard")),
          menuItem("Widgets", tabName = "widgets", icon = icon("th"))
        )
      ),
      dashboardBody(
        tabItems(
          tabItem(
            tabName = "dashboard",
            actionButton("nextt", "Next")
          ),
          tabItem(
            tabName = "widgets"
          )
        )
      )
    )
    
    server <- function(input, output, session) {
      observeEvent(input$nextt, {
        updateTabItems(session, "inTabset", selected = "widgets")
      })
    }
    
    shinyApp(ui, server)