带有小部件的Shinydashboard menuItem不显示结果

时间:2019-08-29 19:04:44

标签: shiny shinydashboard

我正在构建带有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)

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

1 个答案:

答案 0 :(得分:1)

shinydashboard的侧边栏区分“ {childless”和“ childfull” menuItem。通过将sliderInput放置在menuItem“直方图”内,它会变成“子项”,这意味着一个或多个子类别可用于导航,因此无需在体内显示内容,因为用户应导航到其中一个孩子。请阅读this了解更多信息。

因此,如果要在正文中显示“直方图”选项卡,则内容必须是“没有孩子的”。

这是一种将滑块放置在“直方图”选项卡之外的解决方案,但仅在选择“直方图”时才显示:

## 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)