如何更改主面板中每个 tabPanel 的 sidebarPanel

use*_*029 5 r 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)
Run Code Online (Sandbox Code Playgroud)

Wal*_*ldi 5

这是一个条件面板,请参阅此用例或此演示
要回答您的问题,您可以将面板的条件链接到选项卡的 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)
Run Code Online (Sandbox Code Playgroud)

已选择选项卡 1: 在此输入图像描述 已选择选项卡 2: 在此输入图像描述

  • 您的代码应该可以工作,但由于[this](/sf/ask/4384482021/)而无法工作。不要犹豫,点赞吧:知道的人越多越好!您使用了两次plotOutput('plot')...只需将其中一个输出重命名为“plot2”,然后再试一次;) (2认同)