代码之家  ›  专栏  ›  技术社区  ›  firmo23

在闪亮的面板中使用actionButton()将tabItem从一个选项卡移动到另一个选项卡

  •  1
  • firmo23  · 技术社区  · 2 年前

    我正在尝试添加 actionButton() 在第一个选项卡项中,按下时将移动到名为的第二个选项卡项 widgets 。我不明白我做错了什么。

    ## 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)
    
    1 回复  |  直到 2 年前
        1
  •  2
  •   stefan    2 年前

    问题是您添加了 tabItems 在服务器中而不是内部 dashboardBody 。此外,要从 dashboard widgets 你必须做 selected="widgets" 在里面 updateTabItems 最后,您的服务器应该有一个参数 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)