如何为主面板中的每个选项卡面板更改侧边栏面板。

5
我想开发一个应用程序,其布局类似于Shiny gallery中的Radiant (https://shiny.rstudio.com/gallery/radiant.html)。 在该应用程序中,sidebarPanel会针对mainPanel中存在的每个tabPanel进行更改。如下图所示,我希望sidebarPanel(现在为空)根据用户选择的选项卡(Metadata、Raw Data、QC Data)进行更改。请问有人知道如何实现这一点,或者能否指导我在Radiant应用程序中找到ui代码的位置?

enter image description here

编辑:在接收到下面的答案后,我将代码进行了编辑,使其看起来像这样,而不是将sidebar放在一个新的函数中。 但它还没有起作用。这不应该吗?还有什么问题吗?
    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)
1个回答

4
这是一个条件面板,可查看此用例或此演示
要回答您的问题,您可以将面板的条件链接到选项卡的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之类的东西 - 我不知道。我必须像你的代码一样使用函数才能使其工作吗? - user8229029
2
你的代码应该是可以工作的,但因为这个问题而无法正常运行。不要犹豫,点赞吧:知道得越多,就越好!你使用了两次plotOutput('plot')...只需将其中一个输出重命名为plot2,然后再试一次即可 ;) - Waldi

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接