【问题标题】:Jumping to Tab Items using an ActionButton in R Shiny在 R Shiny 中使用 ActionButton 跳转到选项卡项
【发布时间】:2020-04-11 06:02:42
【问题描述】:

我正在使用 bs4Dash 包构建应用程序,我想在主页中包含操作按钮,以允许用户跳转到相应的页面。但是,这些按钮不执行任何操作。

这与this question 非常相似。我认为这里的问题是 updatebs4TabItems 需要 TabSetPanel inputId...除了我不希望在此处包含选项卡集面板。

library(shiny)
library(bs4Dash)

ui <- bs4DashPage(
  # Sidebar -------------------------------------------------------------
  sidebar = bs4DashSidebar(
    bs4SidebarMenu(
      bs4SidebarMenuItem(
        "Welcome",
        tabName = "item0"
      ),
      bs4SidebarMenuItem(
        "Page 1",
        tabName = "item1"
      ),
      bs4SidebarMenuItem(
        "Page 2",
        tabName = "item2"
      )
    )
  ),
  # Body -------------------------------------------------------------
  body = bs4DashBody(
    bs4TabItems(
      bs4TabItem(
        tabName = "item0",
        fluidRow(
          actionButton("JumpToV1", "Go to Page 1"),
          actionButton("JumpToV2", "Go to Page 2")
        )
      ),
      bs4TabItem(
        tabName = "item1",
        fluidRow(
          bs4Callout(
            title = "This is Page 1",
            elevation = 4,
            status = "danger"
          )
        )
      ),
      bs4TabItem(
        tabName = "item2",
        fluidRow(
          bs4Callout(
            title = "This is Page 2",
            elevation = 4,
            status = "danger")
        )
      )
    )
  )
)

server <- function(input, output, session) {
  observeEvent(input$JumpToV1, {
    updatebs4TabItems(session, "item0", selected = "item1")
  })

  observeEvent(input$JumpToV2, {
    updatebs4TabItems(session, "item0", selected = "item2")
  })
}

shinyApp(ui, server)

【问题讨论】:

    标签: r shiny bs4dash


    【解决方案1】:

    你已经接近了!有两件事需要调整。从文档中,请注意 updatebs4TabItems 中的 selected 参数采用整数(所选选项卡的位置),而不是字符串(因此不是您拥有的 id 名称)。此外,updatebs4TabItems 的 inputID 参数将引用您需要设置的 sidebarID。下面的代码应该可以按照您的意愿工作。

    library(shiny)
    library(bs4Dash)
    
    ui <- bs4DashPage(
        # Sidebar -------------------------------------------------------------
        sidebar = bs4DashSidebar(
            bs4SidebarMenu(
                id = "sidebarID", #note the new ID here
                bs4SidebarMenuItem(
                    "Welcome",
                    tabName = "item0"
                ),
                bs4SidebarMenuItem(
                    "Page 1",
                    tabName = "item1"
                ),
                bs4SidebarMenuItem(
                    "Page 2",
                    tabName = "item2"
                )
            )
        ),
        # Body -------------------------------------------------------------
        body = bs4DashBody(
            bs4TabItems(
                bs4TabItem(
                    tabName = "item0",
                    fluidRow(
                        actionButton("JumpToV1", "Go to Page 1"),
                        actionButton("JumpToV2", "Go to Page 2")
                    )
                ),
                bs4TabItem(
                    tabName = "item1",
                    fluidRow(
                        bs4Callout(
                            title = "This is Page 1",
                            elevation = 4,
                            status = "danger"
                        )
                    )
                ),
                bs4TabItem(
                    tabName = "item2",
                    fluidRow(
                        bs4Callout(
                            title = "This is Page 2",
                            elevation = 4,
                            status = "danger")
                    )
                )
            )
        )
    )
    
    server <- function(input, output, session) {
        observeEvent(input$JumpToV1, {
        #changed ID and selected here and below
            updatebs4TabItems(session, inputId = "sidebarID", selected = 2)
        })
    
        observeEvent(input$JumpToV2, {
            updatebs4TabItems(session, inputId = "sidebarID", selected = 3)
        })
    }
    
    shinyApp(ui, server)
    

    【讨论】:

      猜你喜欢
      • 2018-02-03
      • 2016-08-05
      • 2022-01-21
      • 2017-04-06
      • 2013-02-27
      • 2016-12-07
      • 2015-04-17
      • 2017-01-04
      • 2021-05-05
      相关资源
      最近更新 更多