将conditionalPanel添加到闪亮的多个navbarMenus中

时间:2017-09-27 17:06:16

标签: r shiny

我创建了一个包含多个navbarMenustabPanel的闪亮应用程序。每个navBarMenu都有单独的conditionalPanel个。当我在下面的代码段中添加第二个navBarMenu时,conditionalPanel似乎不适用于tabPanel,因为所有输入都显示在所有tabPanel上。不确定我做错了什么。

  ui <- shinyUI(tagList(
  shinythemes::themeSelector(),
  tags$head(
    tags$style(HTML("
                    .shiny-output-error-validation {
                    color: green;
                    }
                    "))
    ),
  navbarPage(title = "Test App",
             navbarMenu("nav bar 1", icon = icon("line-chart"),
                        "menu 1",
                        "--------",
                        tabPanel("Tab1", 
                                 tabPanel("panel 1", icon = icon("line-chart"),
                                          pageWithSidebar(
                                            headerPanel(""),
                                            sidebarPanel(tags$head(
                                              tags$style(type="text/css", "select { max-width: 400px; }"),
                                              tags$style(type="text/css", ".span4 { max-width: 400px; }"),
                                              tags$style(type="text/css", ".well { max-width: 400px; }"),
                                              tags$style(type="text/css", '#leftPanel { width:200px; float:left;}')
                                            ),
                                            conditionalPanel(condition="input.conditionedPanels==1",
                                                             dateRangeInput("daterange1", "Select Date Range:", start = "2017-01-01", end = Sys.Date(), width = "100%")
                                            ),
                                            conditionalPanel(condition="input.conditionedPanels==2",
                                                               dateRangeInput("daterange2", "Select Date Range:", start = "2017-01-01", end = Sys.Date(), width = "100%"),
                                                               selectInput("inputw1", "Select cols:", choices = c("col1","col2","col3"), selected = "col1", multiple = FALSE, width = "100%")
                                            ),
                                            conditionalPanel(condition="input.conditionedPanels==3"
                                            )
                                            ),
                                            mainPanel(
                                              tabsetPanel(id = "conditionedPanels",
                                                          tabPanel("tab 1", value = 1, icon = icon("table"), 
                                                                   br(),
                                                                   h3("Data Table")
                                                          ),
                                                          tabPanel("tab 2", value=2, icon = icon("line-chart"),
                                                                   column(width = 5, leafletOutput("map1")
                                                                   )
                                                          ),
                                                          tabPanel("tab 3", value=3 , icon = icon("table"),
                                                                   tinyMCE('editor1', 'Click to edit text')
                                                          )
                                              )
                                            )
                                          )
                                 )
                        )

             ),

             navbarMenu("nav bar 2", icon = icon("line-chart"),
                        "menu 1",
                        "--------",
                        tabPanel("Tab1", 
                                 tabPanel("panel 2", icon = icon("line-chart"),
                                          pageWithSidebar(
                                            headerPanel(""),
                                            sidebarPanel(tags$head(
                                              tags$style(type="text/css", "select { max-width: 400px; }"),
                                              tags$style(type="text/css", ".span4 { max-width: 400px; }"),
                                              tags$style(type="text/css", ".well { max-width: 400px; }"),
                                              tags$style(type="text/css", '#leftPanel { width:200px; float:left;}')
                                            ),
                                            conditionalPanel(condition="input.conditionedPanels==4",
                                                             dateRangeInput("daterange3", "Select Date Range:", start = "2014-01-01", end = Sys.Date(), width = "100%")
                                            ),
                                            conditionalPanel(condition="input.conditionedPanels==5",
                                                             dateRangeInput("daterange4", "Select Date Range:", start = "2014-01-01", end = Sys.Date(), width = "100%"),
                                                             selectInput("inputw2", "Select cols:", choices = c("col4","col5","col6"), selected = "col1", multiple = FALSE, width = "100%")
                                            ),
                                            conditionalPanel(condition="input.conditionedPanels==6"
                                            )
                                            ),
                                            mainPanel(
                                              tabsetPanel(id = "conditionedPanels",
                                                          tabPanel("tab 1", value = 4, icon = icon("table"), 
                                                                   br(),
                                                                   h3("Data Table")
                                                          ),
                                                          tabPanel("tab 2", value=5, icon = icon("line-chart"),
                                                                   column(width = 5, leafletOutput("map1")
                                                                   )
                                                          ),
                                                          tabPanel("tab 3", value=6 , icon = icon("table"),
                                                                   tinyMCE('editor2', 'Click to edit text')
                                                          )
                                              )
                                            )
                                          )
                                 )
                        )

             )



  )

    )
)

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

我感谢您修复我的代码所能获得的任何帮助。

1 个答案:

答案 0 :(得分:0)

此问题不是因为第二个navBarMenu,因为您在两个标签中有两个具有相同名称的输出元素,即leafletOutput("map1")不允许您为两个输出元素指定相同名称。

将第leafletOutput("map1")leafletOutput("map2")更改为tabsetPanel会修复您报告的问题。

修复此问题后您将遇到的第二个问题是,tabsetPanel具有相同的ID,即conditionedPanels,这只会导致您的第一个tabsetPanel成为{{1} }} 好好工作。要解决此问题,您必须更改conditionalPanel的ID。

我已更改代码中tabsetPanelleafletOutput ID的值,以获得所需的输出。

tabsetPanel

希望它有所帮助!