首页 > 解决方案 > 如何更改主面板中每个 tabPanel 的 sidebarPanel

问题描述

我想在 Shiny 画廊 ( 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)

标签: rshiny

解决方案


这是一个条件面板,请参阅此用例或此演示
要回答您的问题,您可以将面板的条件链接到选项卡的 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: 在此处输入图像描述


推荐阅读