连接两个操作按钮,在闪亮的仪表板中显示相同的 tabItem

Connect two actionbuttons with display of the same tabItem in shiny dashboard

我有下面闪亮的应用程序,在 header 部分有 3 个操作按钮。单击后的每一个现在都会导致对应的选项卡。我想要实现的是使操作按钮 hometab1 指向相同的选项卡 home 而操作按钮 tab2 应该指向选项卡 tab2,因为现在是。

ui <- dashboardPage(
  dashboardHeader(title = "Demo", tags$li(class = "dropdown", actionButton("home", "Home")),
                                  tags$li(class = "dropdown", actionButton("tab1", "Tab1")),
                                  tags$li(class = "dropdown", actionButton("tab2", "Tab2"))),
  dashboardSidebar(width="0px",sidebarMenu(id = "sidebar", # id important for updateTabItems
                               menuItem("Home", tabName = "home", icon = icon("house")),
                               menuItem("Tab1", tabName = "tab1", icon = icon("table")),
                               menuItem("Tab2", tabName = "tab2", icon = icon("line-chart"))
                               )
  ),
  
  dashboardBody(
    tabItems(
      tabItem("home", "This is the home tab"),
      tabItem("tab1", "This is Tab1"),
      tabItem("tab2", "This is Tab2")
    ))
)
server = function(input, output, session){
  observeEvent(input$home, {
    updateTabItems(session, "sidebar", "home")
  })
  observeEvent(input$tab1, {
    updateTabItems(session, "sidebar", "tab1")
  })
  observeEvent(input$tab2, {
    updateTabItems(session, "sidebar", "tab2")
  })
}
shinyApp(ui, server)

这能满足您的需求吗?我只更改了 observeEvent(input$tab1....

中的第三个参数
library(shiny)
library(shinydashboard)

ui <- dashboardPage(
  dashboardHeader(title = "Demo", tags$li(class = "dropdown", actionButton("home", "Home")),
                  tags$li(class = "dropdown", actionButton("tab1", "Tab1")),
                  tags$li(class = "dropdown", actionButton("tab2", "Tab2"))),
  dashboardSidebar(width="0px",sidebarMenu(id = "sidebar", # id important for updateTabItems
                                           menuItem("Home", tabName = "home", icon = icon("house")),
                                           menuItem("Tab1", tabName = "tab1", icon = icon("table")),
                                           menuItem("Tab2", tabName = "tab2", icon = icon("line-chart"))
  )
  ),
  
  dashboardBody(
    tabItems(
      tabItem("home", "This is the home tab"),
      tabItem("tab1", "This is Tab1"),
      tabItem("tab2", "This is Tab2")
    ))
)

server = function(input, output, session){
  observeEvent(input$home, {
    updateTabItems(session, "sidebar", "home")
  })
  observeEvent(input$tab1, {
    updateTabItems(session, "sidebar", "home")
  })
  observeEvent(input$tab2, {
    updateTabItems(session, "sidebar", "tab2")
  })
}

shinyApp(ui, server)