简体   繁体   English

R Shinydashboard:如何在conditionalPanel()条件参数中引用框

[英]R Shinydashboard: How to reference box in conditionalPanel() condition argument

I have a Shiny app where I would like to set a conditionalPanel() depending on whether the user selects a tabPanel from a tabBox . 我有一个Shiny应用程序,我想根据用户是否从tabBox选择tabPanel来设置conditionalPanel() My question is how can I reference the panel the user selected. 我的问题是如何引用用户选择的面板。 In my example the idea is that if the user selects "Panel1" there appears a conditional panel. 在我的示例中,想法是,如果用户选择“ Panel1”,则会出现一个条件面板。 As you will see I tried to set the condition in the conditionalPanel() call as condition = "input.tabs.tab1.box == 'p1'" . 如您所见,我试图在conditionalPanel()调用中将condition = "input.tabs.tab1.box == 'p1'"设置为condition = "input.tabs.tab1.box == 'p1'"

library(shiny)
library(shinydashboard)

ui <- dashboardPage(
  skin = "green",
  dashboardHeader(title = "Referencing Box"),
  dashboardSidebar(
    width = 250,
   sidebarMenu(
     id = "tabs",
     menuItem("First Tab", tabName =  "tab1",
              menuSubItem("Box", tabName = "box"))
   )
  ),
  dashboardBody(
    tabItem(
      tabName = "box",
      tabBox(
        id = "tb",
        title = "Two Panels",
        tabPanel(
          id = "p1",
          title = "Panel1"
        ),
        tabPanel(
          id = "p2",
          title = "Panel2"
        )
      ),
      conditionalPanel(
        condition = "input.tabs.tab1.box == 'p1'",
        box(
          id = "p3",
          title = "Conditional Panel"
        )
      )
    )
  )
)

server <- function(input, output) { }

shinyApp(ui, server)

I've seen some examples around where people referenced the upmost layer, so a menuItem with conditions like condition = "input.tabs == 'anova'" , but in this case the referenced item is within a tabItem and further within a tabBox call. 我已经看到了一些有关人们引用最上层的示例,因此menuItem的条件为condition = "input.tabs == 'anova'" ,但是在这种情况下,引用的项在tabItem内, tabBoxtabBox调用内。

In your case what you want to reference is what is the active tab of the "tabBox" with the id "tb". 在您的情况下,您要引用的是ID为“ tb”的“ tabBox”的活动标签。

So your condition for the conditional panel should be: 因此,您对条件面板的条件应为:

condition = "input.tb == 'p1'"

reference the tabbox that you want to know the active tab of. 引用要了解其活动选项卡的选项卡框。

Also in order to know which tabPanel is currently active you will also need to add a value parameter to the tabPanel, since id is sadly not enough. 同样,为了知道哪个tabPanel当前处于活动状态,您还需要向tabPanel添加一个value参数,因为可悲的是id不够。

Resulting in this code for the tabPanel 结果为tabPanel

 tabPanel(
      value = "p1",
      id = "p1",
      title = "Panel1"
    ),

All together here is a working solution: 总之,这是一个可行的解决方案:

library(shiny)
library(shinydashboard)

ui <- dashboardPage(
    skin = "green",
    dashboardHeader(title = "Referencing Box"),
    dashboardSidebar(
        width = 250,
        sidebarMenu(
            id = "tabs",
            menuItem("First Tab", tabName =  "tab1",
                menuSubItem("Box", tabName = "box"))
        )
    ),
    dashboardBody(
        tabItem(
            tabName = "box",
            tabBox(
                id = "tb",
                title = "Two Panels",
                tabPanel(
                    value = "p1",
                    id = "p1",
                    title = "Panel1"
                ),
                tabPanel(
                    id = "p2",
                    title = "Panel2"
                )
            ),
            conditionalPanel(
                condition = "input.tb == 'p1'",
                box(
                    id = "p3",
                    title = "Conditional Panel"
                )
            )
        )
    )
)

server <- function(input, output) { }
shinyApp(ui, server)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM