简体   繁体   中英

shinydashboard menuItem with widgets not showing results

I'm building an app with shinydashboard which incorporates a side bar menu with menuItem / tabItems, but i can't get the 2nd tab (which includes widgets) to display the result. The body is only displaying the 1st tab. It must be something very simple but i can't see what i'm doing wrong...

here's a reproducible example:

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

Why can i not see the histogram plot on the 2nd tab item?

shinydashboard's sidebar distinguishes "childless" and "childfull" menuItem s. 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)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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