Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

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 :

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

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)
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading