Search code examples
rshinyshinydashboard

Press actionButton and go to another page on shinydashboard


I tried triggering a button to migrate to another page of my app, but I can't do it in shinydashboard.

When I clicked on btn1, it should go to page D (tabName = "four"):

In the first application (normal shiny), it works because it's on the same page, but I would like to change the pages when clicking the btn1 button in shinydashboard shiny).

I'd just like to click btn1 (inside Page 1 > B) and switch pages.

library(shiny)
library(shinydashboard)
library(ggplot2)

##################################normal shiny##################################
ui_1 <- fluidPage(
  fluidRow(box(
    actionButton("btn1", "Go to Next Table", 
                 onclick = "location.href='#table2';"),
    tableOutput("tbl1")
  )),
  fluidRow(id = "table2", box(
    tableOutput("tbl2")
  ))
)

server_1 <- function(input, output, session) {
  output$tbl1 <- renderTable(mtcars)
  output$tbl2 <- renderTable(mpg)
}

shinyApp(ui_1, server_1)

##################################shinydashboard shiny##################################
header <- dashboardHeader(title = "Dashboard")

sidebar <- dashboardSidebar(
  sidebarMenu(
    menuItem(
      text = "Page 1",
      menuSubItem(text = "A", tabName = "one"), 
      menuSubItem(text = "B", tabName = "two")
    ), 
    menuItem(
      text = "Page 2", 
      menuSubItem(text = "C", tabName = "three"), 
      menuSubItem(text = "D", tabName = "four")
    )
  )
)

body <- dashboardBody(
  tabItems(
    tabItem(
      tabName = "one", titlePanel(title = "A")
    ), 
    tabItem(
      tabName = "two", titlePanel(title = "B"), 
      actionButton("btn1", "Go to Next Table", 
                   onclick = "location.href='#shiny-tab-four';")
    ), 
    tabItem(
      tabName = "three", titlePanel(title = "C")
    ), 
    tabItem(
      tabName = "four", titlePanel(title = "D")
    )
  )
)

ui_2 <- dashboardPage(header, sidebar, body)

server_2 <- function(input, output, session) {
  
}

shinyApp(ui_2, server_2)

Solution

  • You can use updateTabItems inside an observer to move around. Try this

    header <- dashboardHeader(title = "Dashboard")
    
    sidebar <- dashboardSidebar(
      sidebarMenu( id = "tabs",
        menuItem(
          text = "Page 1",
          menuSubItem(text = "A", tabName = "one"),
          menuSubItem(text = "B", tabName = "two")
        ),
        menuItem(
          text = "Page 2",
          menuSubItem(text = "C", tabName = "three"),
          menuSubItem(text = "D", tabName = "four")
        )
      )
    )
    
    body <- dashboardBody(
      tabItems(
        tabItem(
          tabName = "one", titlePanel(title = "A")
        ),
        tabItem(
          tabName = "two", titlePanel(title = "B"),
          actionButton("btn1", "Go to tab four" )#,
                       # onclick = "location.href='#shiny-tab-four';")
        ),
        tabItem(
          tabName = "three", titlePanel(title = "C")
        ),
        tabItem(
          tabName = "four", titlePanel(title = "D"),
          actionButton("btn2", "Go to tab one" )
        )
      )
    )
    
    ui_2 <- dashboardPage(header, sidebar, body)
    
    server_2 <- function(input, output, session) {
      observeEvent(input$btn1, {
        updateTabItems(session, "tabs","four")
      })
      observeEvent(input$btn2, {
        updateTabItems(session, "tabs","one")
      })
    }
    
    shinyApp(ui_2, server_2)