基于 shinydashboard 中选择的选项卡在框中的条件显示

Conditional display in box based on tabbox selected in shinydashboard

我使用 shiny 和 shinydashboard。我有一个带有两个 tabPanel 的选项卡。然后还有另一个框,如果选择了 tabbox 中的 tab1,则应显示 textOutput("a"),如果选择 tab2,则应显示 textOutput("b")。

我提供了完整的代码以实现可重现性,但要注意显示重要部分所在的注释。

library(shiny)
library(shinydashboard)

ui <- dashboardPage(
  skin = "red",
  dashboardHeader(title = "lalala", titleWidth = 450),
  sidebar <- dashboardSidebar(width = 400,
                              sidebarMenu(
                                menuItem(
                                  text = strong("First tab"),
                                  tabName = "first",
                                  icon = icon("dashboard")
                                )
                              )),
  body <- dashboardBody(fluidRow(
    tabBox(
      title = "First tabBox",          
      id = "tabset1",
      height = "250px",
      ############## based on which of this tab is selected
      tabPanel("Tab1", "First tab content"),
      tabPanel("Tab2", "Tab content 2")
    ),
    box(
      title = "Selection criteria for chart",
      height = "700px",
      width = 4,
      solidHeader = TRUE,
      status = "danger",
      ############## I want in this box to display either textouput "a" or "b"
      textOutput("a")

    )
  ))
)

server <- function(input, output) {
  output$a <- renderText(a <- "ahoj")
  output$b <- renderText(b <- "cau")
}

input$tabset1 returns 当前所选标签的 ID(因此 Tab1Tab2)。然后你可以根据这个return值,用一个if/else语句打印你喜欢的内容。