dashboardBody 在使用条件面板时不显示

问题描述 投票:0回答:1

我正在构建一个闪亮的应用程序。当我在 sideBar 菜单中包含一个 conditionalPanel 时,我不再在 Dashboard 正文中看到任何内容。 这是我的代码:

library(shiny)
library(shinydashboard)

ui <- dashboardPage(
  dashboardHeader(title="TEST"),
  dashboardSidebar(
    sidebarMenu(
      menuItem("tab1", tabName="tab_number1", icon=icon("arrow-right"),
               conditionalPanel("input.sidebar==='tab_number1",
                                selectInput(inputId="selection",label = "Select country:",choices ="", selected=NULL))
               
      )
    )
  ),
  
  ## Body content
  dashboardBody(
    tabItems(
      #intro tab
        tabItem(tabName="tab_number1",
              fluidPage(
                fluidRow(
                  h2("This text should appear here!!"),
                  uiOutput("up"),
                  plotOutput(outputId="plotnumber1"),br(),
                  ))
      )
    )
  )
)

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

当我在 tab1 中单击时,我看不到 dashboardBody 中的文本。 如果我删除侧边栏菜单中的条件面板,我就能看到文本。

我做错了什么?

shiny shinydashboard sidebar
1个回答
0
投票

当 menuItem 中有子项时,您需要定义 menuSubItem 来显示页面/输出。此外,您不需要默认情况下的条件面板。试试这个

choices <- names(mtcars[-1])

ui <- dashboardPage(
  dashboardHeader(title="TEST"),
  dashboardSidebar(
    sidebarMenu(id="sidebar",
      menuItem("Home", tabName = "home", icon = icon("home")),
      menuItem("tab1", tabName="tab_1", icon=icon("arrow-right"),
               selectInput(inputId="selection",label = "Select country:",choices = choices, selected=NULL),
               menuSubItem("My Plot", tabName="tab_1plot",
                           icon = icon("line-chart"))
      ),
      menuItem("tab2", tabName="tab_2", icon = icon("arrow-right")),
      menuItem("tab3", tabName="tab_3", icon = icon("arrow-right"))
    )
  ),
  
  ## Body content
  dashboardBody(
    tabItems(
      #intro tab
      tabItem(tabName="tab_1plot",
              #fluidPage(
                fluidRow(
                  h2("This text should appear here!!"),
                  #uiOutput("up"),
                  plotOutput(outputId="plotnumber1")
                )#)
      ),
      tabItem(tabName="tab_2", fluidRow(plotOutput("p2"))
      ),
      tabItem(tabName="tab_3", fluidRow(plotOutput("p3"))
      )
    )
  )
)

server <- function(input, output,session) { 
  observe({print(input$sidebar)})
  output$plotnumber1 <- renderPlot(plot(mtcars[,c("mpg",input$selection)]))
  output$p2 <- renderPlot(plot(cars))
  output$p3 <- renderPlot(plot(pressure))
}
shinyApp(ui, server=server)
© www.soinside.com 2019 - 2024. All rights reserved.