【问题标题】:How to hide sidebarPanel of a Shiny app for a particular tab如何隐藏特定选项卡的闪亮应用程序的侧边栏面板
【发布时间】:2020-07-22 01:08:56
【问题描述】:

我的 Shiny 应用有一个通用的侧边栏面板。我想为一个特定的选项卡隐藏它,即每当用户导航到该选项卡时,sidebarPanel 就会折叠。我正在尝试的代码如下-

用户界面-

library(shiny)
shinyUI(fluidPage (
  theme = shinytheme("superhero"),
  headerPanel("COVID-19 Data Visualizer"),
  sidebarPanel(
    width = 2,
    selectInput(
      "countries",
      label = "Select Countries",
      choices =
        c("B", "C", "A"),
      selected = c("A"),
      multiple = T
    ),
    submitButton(text = "View")
  ),
  mainPanel (h1(""),
             tabsetPanel(
               tabPanel(
                 "Global Status",
                 div(id="Main"),
                 plotlyOutput("figG"),
                 br(),
                 plotlyOutput("global_time"),
                 br(),
                 plotlyOutput("global_cfr"),
                 br(),
                 plotlyOutput("global_p"),
                 br(),
                 plotlyOutput("global_recov_dead")
               ),
               tabPanel(
                 "Comparative Charts",
                 plotlyOutput("fig_confirm"),
                 br(),
                 plotlyOutput("fig_dead"),
                 br(),
                 plotlyOutput("fig_recov")
               ),
               tabPanel(
                 "Ratio Analysis",
                 plotlyOutput("fig_confirm_S"),
                 br(),
                 plotlyOutput("fig_confirm_D"),
                 br(),
                 plotlyOutput("fig_Ratio"),
                 br(),
                 plotlyOutput("fig_cfr_print")
               )
             ))
))

服务器部分-

server <- function(input, output) {
  observeEvent(input$tabs == "Global Status", {
    shinyjs::hide(id = "Main")
  })

}

我真的不想使用 navbarPage 并且希望所有输入都使用单个 sidebarPanel。

我得到的输出截图-

提前致谢。

【问题讨论】:

    标签: r shiny shinyapps


    【解决方案1】:

    这是一个例子:

    library(shiny)
    library(shinyjs)
    
    ui <- fluidPage(
      useShinyjs(), 
    
      sidebarLayout(
    
        sidebarPanel(
          id="sidebar",
          selectInput(
            "countries", label = "Select Countries",
            choices = c("B", "C", "A"), selected = "A",
            multiple = TRUE
          )
        ),
    
        mainPanel(
          tabsetPanel(
            tabPanel(
              "Global status",
              sliderInput("s1", "slider 1", 0, 100, 20)
            ),
            tabPanel(
              "Comparative charts",
              sliderInput("s2", "slider 2", 0, 100, 50)
            ),
            tabPanel(
              "Ratio analysis",
              sliderInput("s3", "slider 3", 0, 100, 80)
            ),
            id = "tabset"
          ), 
          id="main"
        )
      )
    )
    
    server <- function(input, output){
    
      observeEvent(input[["tabset"]], {
        if(input[["tabset"]] == "Comparative charts"){
          hideElement(selector = "#sidebar")
          removeCssClass("main", "col-sm-8")
          addCssClass("main", "col-sm-12")
        }else{
          showElement(selector = "#sidebar")
          removeCssClass("main", "col-sm-12")
          addCssClass("main", "col-sm-8")
        }
      })
    
    }
    
    shinyApp(ui = ui, server = server)
    

    【讨论】:

    • 太好了。但是我需要在我的侧边栏面板上有一个提交按钮,每当我插入一个时,所有选项卡的整个侧边栏面板都会消失。请您再帮忙一点好吗?
    猜你喜欢
    • 2016-11-24
    • 2018-06-02
    • 2017-06-28
    • 1970-01-01
    • 2020-06-30
    • 1970-01-01
    • 2017-04-23
    • 2016-02-26
    • 2013-04-19
    相关资源
    最近更新 更多