如何在闪亮的r中的bs4仪表板中从一个选项卡面板转到另一个选项卡面板

问题描述 投票:0回答:1

我想使用updatebs4TabSetPanel()使用bs4TabSetPanel中的单选按钮更新选项卡面板。我已经在闪亮的仪表板上完成了多次,但是无法在bs4Dash中完成。我正在上传示例代码。非常感谢您的帮助。

library(shiny)
library(shinydashboard)

home<-bs4TabSetPanel(
  id = "tabset1",
  side = "left",

  bs4TabPanel(
    tabName = "Tab 1",
    active = TRUE,
    fluidRow(
      box(radioButtons("abc", label = "Please select an option", 
                       choices = c("Go to tab 2" = "G2", "Go to tab 3" = "G3"), selected = character()))
    )
  ),
  bs4TabPanel(
    tabName = "Tab 2",
    active = FALSE,
    fluidRow(
      h1("Welcome to tab 2")
    )
  ),
  bs4TabPanel(
    tabName = "Tab 3",
    active = FALSE,
    fluidRow(
      h1("Welcome to tab 3")
    )
  )
)

ui<- bs4DashPage(
  navbar = bs4DashNavbar(),
  sidebar = bs4DashSidebar(),
  controlbar = bs4DashControlbar(),
  footer = bs4DashFooter(),
  title = "test",
  body = bs4DashBody(
    home
  )
)

server<- function(input, output, session){
  observeEvent(input$abc,{
    if (input$abc == "G2"){
      updatebs4TabSetPanel(session, "tabset1", selected = "Tab 2")
    } else{
      updatebs4TabSetPanel(session, "tabset1", selected = "Tab 3")
    } 
  })
}

shinyApp(ui,server)
r shiny
1个回答
0
投票

嗨,bs4Dash中的Bijurika,您需要使用选项卡的数字位置而不是选项卡名称。

这应该可行,我刚刚更改了'selected =“ Tab 2”'

library(shiny)
library(shinydashboard)
library(bs4Dash)

home<-bs4TabSetPanel(
  id = "tabset1",
  side = "left",

  bs4TabPanel(
    tabName = "Tab 1",
    active = TRUE,
    fluidRow(
      box(radioButtons("abc", label = "Please select an option", 
                       choices = c("Go to tab 2" = "G2", "Go to tab 3" = "G3"), selected = character()))
    )
  ),
  bs4TabPanel(
    tabName = "Tab 2",
    active = FALSE,
    fluidRow(
      h1("Welcome to tab 2")
    )
  ),
  bs4TabPanel(
    tabName = "Tab 3",
    active = FALSE,
    fluidRow(
      h1("Welcome to tab 3")
    )
  )
)

ui<- bs4DashPage(
  navbar = bs4DashNavbar(),
  sidebar = bs4DashSidebar(),
  controlbar = bs4DashControlbar(),
  footer = bs4DashFooter(),
  title = "test",
  body = bs4DashBody(
    home
  )
)

server<- function(input, output, session){
  observeEvent(input$abc,{
    if (input$abc == "G2"){
      updatebs4TabSetPanel(session, "tabset1", selected = 2)
    } else{
      updatebs4TabSetPanel(session, "tabset1", selected = 3)
    } 
  })
}

shinyApp(ui,server)
© www.soinside.com 2019 - 2024. All rights reserved.