本文介绍了使用条件面板在闪亮的仪表板中添加动态选项卡的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我希望为我闪亮的应用程序设置动态标签。我尝试了以下代码

## 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)
但是,我不能成功地动态使用选项卡面板。它只显示一个选项卡,检查后,它应该显示第二个选项卡。

推荐答案

您可以按照下面的示例动态创建ui

示例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)

示例2:使用条件面板

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)

这篇关于使用条件面板在闪亮的仪表板中添加动态选项卡的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-22 23:04