首页 > 解决方案 > 带有未显示结果的小部件的闪亮仪表板菜单项

问题描述

我正在使用shinydashboard 构建一个应用程序,该应用程序包含一个带有menuItem / tabItems 的侧栏菜单,但我无法获得第二个选项卡(包括小部件)来显示结果。正文仅显示第一个选项卡。这一定是非常简单的事情,但我看不出我做错了什么......

这是一个可重现的例子:

## app.R ##
library(shiny)
library(shinydashboard)

ui <- dashboardPage(

  dashboardHeader(),

  dashboardSidebar(

    sidebarMenu(

      menuItem("SomeText", 
               tabName = "sometext"
               ),

      menuItem("Histogram", 
               tabName = "histogram",

            # input1: number of observations:
            sliderInput(
              inputId = "n",
              label = "Number of observations",
              min = 10, max = 100, value = 30
            )

      ) # end menuItem       

    ) # end sidebarMenu

  ), # end dashboardSidebar


  dashboardBody(
     tabItems(

         tabItem(tabName = "sometext",
            h2("blah blah blah")
         ),

        tabItem(tabName = "histogram",
            plotOutput("my_histogram")
        )
    )  
  )   
)

server <- function(input, output) { 
  output$my_histogram <- renderPlot({
    hist(input$n, col = "red" )
  })

  }

shinyApp(ui, server)

为什么我在第二个选项卡项上看不到直方图?

标签: shinyshinydashboard

解决方案


shinydashboard's sidebar distinguishes "childless" and "childfull" menuItems. By placing the sliderInput inside the menuItem "histogram" it becomes "childfull", which means one or more sub categories are available for navigation, accordingly there is no need to display content in the body, as the user is expected to navigate to one of the childs. Please read this for further information.

Therefore, if you want to display content in the body for the "histogram"-tab it needs to be "childless".

Here is a solution placing the slider outside of the "histogram"-tab, but only displaying it when "histogram" is selected:

## app.R ##
library(shiny)
library(shinydashboard)

ui <- dashboardPage(dashboardHeader(),
                    dashboardSidebar(
                      sidebarMenu(
                        id = "mySidebar",
                        menuItem("SomeText", tabName = "sometext"),
                        menuItem("Histogram", tabName = "histogram"),# end menuItem
                        conditionalPanel(condition = "input.mySidebar == 'histogram'", {
                          # input1: number of observations:
                          sliderInput(
                            inputId = "n",
                            label = "Number of observations",
                            min = 10,
                            max = 100,
                            value = 30
                          )
                        })
                      ) # end sidebarMenu
                    ), # end dashboardSidebar
                    dashboardBody(tabItems(
                      tabItem(tabName = "sometext",
                              h2("blah blah blah")),
                      tabItem(tabName = "histogram",
                              plotOutput("my_histogram"))
                    )))

server <- function(input, output) {
  output$my_histogram <- renderPlot({
    hist(round(runif(input$n, 1, 10), digits = 0), col = "red")
  })
}

shinyApp(ui, server)

推荐阅读