Add dynamic tabs in shiny dashboard using conditional panel

后端 未结 1 1460
孤城傲影
孤城傲影 2021-01-03 03:55

I would like to have dynamic tabs for my shiny app. I tried the below code

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

ui <- dashboardPage(
  dash         


        
相关标签:
1条回答
  • 2021-01-03 04:38

    You can dynamically create the ui as per example below.

    Example 1: Using 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

    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)
    
    0 讨论(0)
提交回复
热议问题