shinydashboard:: switch tab

Published by onesixx on

https://rdrr.io/cran/shinydashboard/man/updateTabItems.html

 

 

## Only run this example in interactive R sessions
library(shiny)
library(shinydashboard)

ui <- dashboardPage(
  dashboardHeader(title = "Simple tabs"),
  dashboardSidebar(
    sidebarMenu( id = "tabs",
      menuItem("Dashboard", tabName = "dashboard",icon = icon("dashboard") ),  
      menuItem("Widgets",   tabName = "widgets",  icon = icon("th")) 
    ),
    actionButton('btn_switchtab', 'Switch tab')
  ),
  dashboardBody(
    tabItems(
      tabItem(tabName = "dashboard",
        h2("Dashboard tab content"),
        verbatimTextOutput("dashboard_tab")
        
      ),
      tabItem(tabName = "widgets",
        h2("Widgets tab content"),
        verbatimTextOutput("widgets_tab")
      )
    )
  )
)

server <- function(input, output, session) {
  observeEvent(input$btn_switchtab, {
    newtab <- switch(input$tabs,
      "dashboard" = "widgets",
      "widgets"   = "dashboard"
    )
    updateTabItems(session, "tabs", newtab)
  })
    
  output$dashboard_tab <- renderText({ input$tabs })
  output$widgets_tab   <- renderText({ input$tabs })
    
  
}

shinyApp(ui, server)

 

 

Categories: R-Shiny

onesixx

Blog Owner

Subscribe
Notify of
guest

0 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
0
Would love your thoughts, please comment.x
()
x