【问题标题】:How to change sidebarPanel for every tabPanel in the Main Panel如何更改主面板中每个 tabPanel 的 sidebarPanel
【发布时间】:2021-01-16 11:04:55
【问题描述】:

我想开发一个布局类似于闪亮画廊 (https://shiny.rstudio.com/gallery/radiant.html) 中的 Radiant 的应用。在该应用程序中,sidebarPanel 会针对 mainPanel 中存在的每个 tabPanel 进行更改。这是如何实现的?这张图片显示了我的布局,我希望侧边栏面板(现在为空)根据用户选择的选项卡(元数据、原始数据、QC 数据)进行更改。是否有人知道如何执行此操作,或者您能否指出 Radiant 应用程序中的 ui 代码所在的位置?

编辑:收到下面的答案后,我将代码编辑为如下所示,而不是将侧边栏放在新功能中。但是,它还没有工作。不应该吗?还有什么问题?

    ui <- navbarPage(title = "SD Mesonet Quality Control", id = "navbarPage",
                     tabPanel(title = 'Data',
                              sidebarLayout(
                                sidebarPanel(
                                  conditionalPanel(condition="input.tabselected == 1",
                                                   actionButton('bt','button Tab 1')
                                  ),
                                  conditionalPanel(condition="input.tabselected == 2",
                                                   selectInput('select','choice',choices=c("A","B"))
                                  )
                                ),
                                mainPanel(
                                  tabsetPanel(type = "tabs", id = "tabselected",
                                              tabPanel("Instrumentation", value = 1, plotOutput("plot")),
                                              tabPanel("Metadata", value = 2, plotOutput("plot"))
                                  )
                                )
                              ),
                     )
    )
    
    
    server <- function(input,output){
      
    }
    
    shinyApp(ui,server)

【问题讨论】:

    标签: r shiny


    【解决方案1】:

    这是一个条件面板,请参阅this use case 或此demo
    要回答您的问题,您可以将面板的条件链接到选项卡的 id:

    library(shinydashboard)
    library(shiny)
    sidebar <- dashboardSidebar(
        conditionalPanel(condition="input.tabselected==1",
                         actionButton('bt','button Tab 1')
                         ),
    
        conditionalPanel(condition="input.tabselected==2",
                         selectInput('select','choice',choices=c("A","B"))
                         )
        )
    )
    
    # Header ----
    header <- dashboardHeader(title="Test conditional panel")
    
    # Body ----
    body <- dashboardBody(
      mainPanel(
        tabsetPanel(
          tabPanel("tab1", value=1,
                    h4("Tab 1 content")),
          tabPanel("tab2", value=2,
                   h4("Tab 2 content")),
          id = "tabselected"
        )
      )
    )
    ui <- dashboardPage(header, sidebar, body)
    
    shinyApp(ui=ui,server=server)
    

    选项卡 1 已选择: 选项卡 2 已选择:

    【讨论】:

    • 谢谢,这很有帮助。我编辑了我的问题以包含我的新代码,该代码不起作用。我正在尝试将 conditionalPanels 嵌入到普通代码中,而不是为其编写函数。也许是因为我正在尝试使用 navbarPage 而不是 shinyDashboard 或其他东西 - 我不知道。我是否必须使用像你的代码这样的函数才能让它工作?
    • 您的代码应该可以工作,但因为this 而不能工作。不要犹豫,投票:知道的人越多越好!您使用了两次 plotOutput('plot')... 只需将其中一个输出重命名为 plot2 并重试;)
    猜你喜欢
    • 2016-07-21
    • 2020-12-03
    • 1970-01-01
    • 2016-11-10
    • 1970-01-01
    • 1970-01-01
    • 2015-04-01
    相关资源
    最近更新 更多