基于shinydashboard中选择的tabbox的框中的条件显示

时间:2016-06-30 13:59:14

标签: r shiny conditional shinydashboard

我使用闪亮的shinydashboard。我有一个带有两个tabPanel的tabbox。然后还有另一个框,如果选中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")
}

1 个答案:

答案 0 :(得分:1)

input$tabset1会返回当前所选标签的ID(因此Tab1Tab2)。然后,您可以使用if / else语句根据此返回值打印您喜欢的内容。