【问题标题】:How to make conditionalPanel show menu item based on active tab in shinydashboard如何根据闪亮仪表板中的活动选项卡使条件面板显示菜单项
【发布时间】:2020-07-21 19:34:21
【问题描述】:

我正在尝试用一堆不同的选项卡制作一个闪亮的仪表板,这些选项卡显示不同类型的数据。我想要的是选择某个TabItem时,在侧边栏中选择选择inptinput项目。 (最终我希望在多个选项卡上发生这种情况,但我现在只处理一个选项卡。)

这是我想要的一个可执行示例:


library(shiny)
library(shinythemes)
library(shinydashboard)
library(tidyverse)

options(warn=-1)
data(iris)
data(mtcars)

tabset1 = tabsetPanel(id = "mtcars",
                            tabPanel(id = "mtplots","mtcars plots",
                                     fluidRow(box(title = "Plot1", plotOutput("mtcarsplot1"))
                            )),
                            
                            
                            tabPanel(id = "mttable","MTcars tables",
                                     fluidRow(box(title = "Table 1",  tableOutput("mtcarstable1")))
                            ))



tabset2 = tabsetPanel(id = "iris",
                      tabPanel(id = "iris","iris plots",
                               fluidRow(box(title = "Plot1", plotOutput("irisplot1"))
                               )),
                      
                      
                      tabPanel(id = "mttable","iris tables",
                               fluidRow(box(title = "Table 1",  tableOutput("iristable1")))
                      ))




# Define UI for application that draws a histogram
ui <- dashboardPage(

  
  dashboardHeader(),
  
  
  dashboardSidebar(
    sidebarMenu(
    menuItem("MTCARS", tabName = "mt", icon = icon("user-tie")),
    selectInput("mtvar", "Choose a variable", choices = colnames(mtcars)),
    sliderInput("mtlines", "Number of lines", 1,50,10),
    

# **I would like a conditionalPanel here such that if the tab mtplots is selected, a selectInput as below shows up - but only is visible for that tab **

#selectInput("colorvar", "choose a color", choices = c("red", "yellow", "green", "blue"))


menuItem("IRIS", icon = icon("envelope-open-text"), tabName = "ir"),    
    selectInput("irvar", "Choose a variable", choices = colnames(iris)),
    sliderInput("irislines", "Number of lines", 1,50,10)
    )
  ),
  
  dashboardBody(
    tabItems(
      tabItem("ir", tabset2),
      tabItem("mt", tabset1)
      )
      
    )
  )






# Begin Server ----------------------------------------------

server <- function(input, output, session) {
 
  output$mtcarsplot1=renderPlot({
    
    
    ggplot(mtcars, aes_string(x = input$mtvar)) + geom_histogram()
    
    
  })
  
  output$irisplot1=renderPlot({
    ggplot(iris, aes_string(x = input$irvar)) + geom_histogram()
    
    
  })
  
  
  output$mtcarstable1=renderTable({
    head(mtcars, input$mtlines)
    
  })
 
  
  output$iristable1=renderTable({
    head(iris, input$irislines)
    
  })
  
  
  
  
}

shinyApp(ui, server)

【问题讨论】:

    标签: r shiny shinydashboard


    【解决方案1】:

    您可以使用input$mtcars 来确定tabsetPanel 中的哪个选项卡处于活动状态。要呈现动态/条件 UI 元素,您可以使用 uiOutput/renderUI。在renderUI 中,我使用req 仅在选择了正确的tabPanel 时才呈现它:

    library(shiny)
    library(shinythemes)
    library(shinydashboard)
    library(tidyverse)
    
    data(iris)
    data(mtcars)
    
    tabset1 = tabsetPanel(id = "mtcars",
                          tabPanel(id = "mtplots","mtcars plots",
                                   fluidRow(box(title = "Plot1", plotOutput("mtcarsplot1"))
                                   )),
                          
                          
                          tabPanel(id = "mttable","MTcars tables",
                                   fluidRow(box(title = "Table 1",  tableOutput("mtcarstable1")))
                          ))
    
    
    
    tabset2 = tabsetPanel(id = "iris",
                          tabPanel(id = "iris","iris plots",
                                   fluidRow(box(title = "Plot1", plotOutput("irisplot1"))
                                   )),
                          
                          
                          tabPanel(id = "mttable","iris tables",
                                   fluidRow(box(title = "Table 1",  tableOutput("iristable1")))
                          ))
    
    
    
    
    # Define UI for application that draws a histogram
    ui <- dashboardPage(
      
      
      dashboardHeader(),
      
      
      dashboardSidebar(
        sidebarMenu(
          menuItem("MTCARS", tabName = "mt", icon = icon("user-tie")),
          selectInput("mtvar", "Choose a variable", choices = colnames(mtcars)),
          sliderInput("mtlines", "Number of lines", 1,50,10),
          
          
          # **I would like a conditionalPanel here such that if the tab mtplots is selected, a selectInput as below shows up - but only is visible for that tab **
          
          
          uiOutput("UI_conditional_input"),
          
          menuItem("IRIS", icon = icon("envelope-open-text"), tabName = "ir"),    
          selectInput("irvar", "Choose a variable", choices = colnames(iris)),
          sliderInput("irislines", "Number of lines", 1,50,10)
        )
      ),
      
      dashboardBody(
        tabItems(
          tabItem("ir", tabset2),
          tabItem("mt", tabset1)
        )
        
      )
    )
    
    
    
    
    
    
    # Begin Server ----------------------------------------------
    
    server <- function(input, output, session) {
      
      output$mtcarsplot1=renderPlot({
        
        
        ggplot(mtcars, aes_string(x = input$mtvar)) + geom_histogram()
        
        
      })
      
      output$irisplot1=renderPlot({
        ggplot(iris, aes_string(x = input$irvar)) + geom_histogram()
        
        
      })
      
      
      output$mtcarstable1=renderTable({
        head(mtcars, input$mtlines)
        
      })
      
      
      output$iristable1=renderTable({
        head(iris, input$irislines)
        
      })
      
      output$UI_conditional_input <- renderUI({
        req(input$mtcars == "mtcars plots")
        selectInput("colorvar", "choose a color", choices = c("red", "yellow", "green", "blue"))
        
      })
      
      
      
      
    }
    
    shinyApp(ui, server)
    

    【讨论】:

    • 谢谢!我快到了,我使用的是 tabPanel id 而不是标题......知道为什么这不起作用吗?
    • 我认为它在文档中有点隐藏;我了解tabsetPanel 帮助页面将在返回值中使用value 参数;在tabPanel 文档中,它说“当 tabsetPanel 报告此选项卡被选中时应该发送的值。如果省略并且 tabsetPanel 有一个 id,那么将使用标题。”
    猜你喜欢
    • 2016-12-20
    • 2019-08-19
    • 2021-10-06
    • 2021-12-14
    • 2020-07-24
    • 1970-01-01
    • 1970-01-01
    • 2019-02-23
    • 1970-01-01
    相关资源
    最近更新 更多