【问题标题】:Hide dropdownButton in shiny dashboardHeaderPlus在闪亮的仪表板HeaderPlus中隐藏下拉按钮
【发布时间】:2021-03-02 05:23:29
【问题描述】:

我有一个shinydashboardPlus,标题中有两个dropdownButtons。每个按钮对应一个选项卡,我想隐藏未选择选项卡的按钮。我在下面提供了一个最小的工作示例。提前谢谢!

library(shiny)
library(shinydashboard)
library(shinydashboardPlus)
library(shinyjs)

ui <- shinyUI(
  dashboardPagePlus(
    dashboardHeaderPlus(
      left_menu = tagList(
        dropdownButton(
          inputId = "select_company_button",
          label = "Select Company",
          icon = icon("store"),
          circle = FALSE,
          selectizeInput(
            inputId = "select_company",
            label = "Company",
            multiple = TRUE,
            choices = c("ALL Companies" = ""),
            selected = NULL,
            options = list(
              closeAfterSelect = TRUE, 
              plugins = list("remove_button")))
        ),
        dropdownButton(
          inputId = "select_industry_button",
          label = "Select Industry",
          icon = icon("industry"),
          circle = FALSE,
          selectizeInput(
            inputId = "select_industry",
            label = "Industry",
            multiple = TRUE,
            choices = c("ALL Industries" = ""),
            selected = NULL,
            options = list(
              closeAfterSelect = TRUE, 
              plugins = list("remove_button")))
        )
      )
    ),
    
    dashboardSidebar(
      sidebarMenu(
        id = "tabs",
        menuItem(
          text = "Company", 
          tabName = "company",
          icon = icon("store")
        ),
        menuItem(
          text = "Industry", 
          tabName = "industry",
          icon = icon("industry")
        )
      )
    ),
    dashboardBody(
      tabItems(
        tabItem("company"),
        tabItem("industry")
      )
    )
  )
)

server <- shinyServer(function(input, output, session) {
  observe({ 
    
    if(input$tabs == "company") {
      #show company dropdown button
      #hide industry dropdown button
    } else{
      #hide company dropdown button
      #show industry dropdown button
    }
  })
})

shinyApp(ui = ui, server = server)

【问题讨论】:

    标签: r shiny shinydashboard shinyjs


    【解决方案1】:

    使用renderUI 应该可以做到。试试这个

    library(shiny)
    library(shinydashboard)
    library(shinydashboardPlus)
    library(shinyjs)
    
    ui <- shinyUI(
      dashboardPagePlus(
        dashboardHeaderPlus(
          left_menu = tagList(
            uiOutput("tab1")
          )
        ),
        
        dashboardSidebar(
          sidebarMenu(
            id = "tabs",
            menuItem(
              text = "Company", 
              tabName = "company",
              icon = icon("store")
            ),
            menuItem(
              text = "Industry", 
              tabName = "industry",
              icon = icon("industry")
            )
          )
        ),
        dashboardBody(
          tabItems(
            tabItem("company"),
            tabItem("industry")
          )
        )
      )
    )
    
    server <- shinyServer(function(input, output, session) {
      output$tab1 <- renderUI({
        if(input$tabs == "company") {
          dropdownButton(
            inputId = "select_company_button",
            label = "Select Company",
            icon = icon("store"),
            tabName = "company",
            circle = FALSE,
            selectizeInput(
              inputId = "select_company",
              label = "Company",
              multiple = TRUE,
              choices = c("ALL Companies" = ""),
              selected = NULL,
              options = list(
                closeAfterSelect = TRUE, 
                plugins = list("remove_button")))
          )
          #show company dropdown button
          #hide industry dropdown button
        } else{
          dropdownButton(
            inputId = "select_industry_button",
            label = "Select Industry",
            icon = icon("industry"),
            circle = FALSE,
            selectizeInput(
              inputId = "select_industry",
              label = "Industry",
              multiple = TRUE,
              choices = c("ALL Industries" = ""),
              selected = NULL,
              options = list(
                closeAfterSelect = TRUE, 
                plugins = list("remove_button")))
          )
          #hide company dropdown button
          #show industry dropdown button
        }
      })
      
    })
    
    shinyApp(ui = ui, server = server)
    

    【讨论】:

    • 是否有另一种方法可以在每次用户切换选项卡时不使用 renderUI 来做到这一点?我仍然希望 selectizeInputs 存在,以便我可以访问它们的值,但我希望它们对用户隐藏。在我的真实应用程序中,我有四个选项卡和 selectizeInput,所以通过 javascript 隐藏它们会很棒。
    • 我对javascript不太熟悉。用户输入可以在服务器端使用 selectizeInput 完成。
    猜你喜欢
    • 2020-02-21
    • 2019-08-28
    • 2016-12-09
    • 2015-12-12
    • 2018-02-24
    • 2019-08-19
    • 1970-01-01
    • 1970-01-01
    • 2016-01-06
    相关资源
    最近更新 更多