简体   繁体   English

使用条件面板在闪亮的仪表板中添加动态选项卡

[英]Add dynamic tabs in shiny dashboard using conditional panel

I would like to have dynamic tabs for my shiny app. 我想为我的闪亮应用提供动态标签。 I tried the below code 我尝试了以下代码

## app.R ##
library(shiny)
library(shinydashboard)

ui <- dashboardPage(
  dashboardHeader(),
  dashboardSidebar(
    checkboxGroupInput("Tabs", 
                       label = h4("tabpanel"), 
                       choices = list("tabs" = "tabs"),
                       selected = NULL),
    checkboxGroupInput("moreTabs", 
                       label = h4("moretabpanel"), 
                       choices = list("moretabs" = "moretabs"),
                       selected = NULL)
  ),
  dashboardBody(
    conditionalPanel(
      condition = "input.Tabs == 'tabs'",
      tabBox(
        title = "intro",
        id= "ttabs", width = 8, height = "420px",
        tabPanel("Files", dataTableOutput("Files")),
        conditionalPanel(
          condition = "input.moreTabs == 'moretabs'",
          tabPanel("Files1", dataTableOutput("Files1"))
        )
  )
)
)
)
server <- function(input, output) { }

shinyApp(ui, server)

However, I am not successful to use the tab panel dynamically. 但是,我不能成功地动态使用选项卡面板。 It shows only one tab, and on checking, its supposed to show the second tab. 它仅显示一个选项卡,并且在检查时应显示第二个选项卡。

You can dynamically create the ui as per example below. 您可以按照以下示例动态创建ui

Example 1: Using RenderUI 示例1:使用RenderUI

rm(list = ls())
library(shiny)
library(shinydashboard)

ui <- dashboardPage(
  dashboardHeader(),
  dashboardSidebar(
    checkboxGroupInput("Tabs", label = h4("tabpanel"), choices = list("tabs" = "tabs"),selected = NULL),
    checkboxGroupInput("MoreTabs", label = h4("moretabpanel"), choices = list("moretabs" = "moretabs"),selected = NULL)
  ),
  dashboardBody(
    uiOutput("ui")
  )
)
server <- function(input, output) { 

  output$ui <- renderUI({

    check1 <- input$Tabs == "tabs"
    check2 <- input$MoreTabs == "moretabs"

    if(length(check1)==0){check1 <- F}
    if(length(check2)==0){check2 <- F}

    if(check1 && check2){
      tabBox(title = "intro",id= "ttabs", width = 8, height = "420px",
             tabPanel("Files", dataTableOutput("Files")),
             tabPanel("Files1", dataTableOutput("Files1"))
      )
    }
    else if(check1){
      tabBox(title = "intro",id= "ttabs", width = 8, height = "420px",tabPanel("Files", dataTableOutput("Files")))
    }
    else{return(NULL)}
  })
}

shinyApp(ui, server)

在此处输入图片说明

Example 2: Using conditionalPanel 示例2:使用conditionalPanel

rm(list = ls())
library(shiny)
library(shinydashboard)

ui <- dashboardPage(
  dashboardHeader(),
  dashboardSidebar(
    checkboxGroupInput("Tabs", label = h4("tabpanel"), choices = list("tabs" = "tabs"),selected = NULL),
    checkboxGroupInput("MoreTabs", label = h4("moretabpanel"), choices = list("moretabs" = "moretabs"),selected = NULL)
  ),
  dashboardBody(

    conditionalPanel(
      condition = "input.MoreTabs == 'moretabs' && input.Tabs == 'tabs'",
      tabBox(
        title = "intro",
        id= "ttabs", width = 8, height = "420px",
        tabPanel("Files",value=1, dataTableOutput("Filesa")),
        tabPanel("Files1",value=2, dataTableOutput("Files1a"))
      )
    ),

    conditionalPanel(
      condition = "input.Tabs == 'tabs' && input.MoreTabs != 'moretabs'",
      tabBox(
        title = "intro",
        id= "ttabs", width = 8, height = "420px",
        tabPanel("Files",value=3, dataTableOutput("Files"))
      ))  
))
server <- function(input, output) { }
shinyApp(ui, server)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM