Reputation: 646
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?
Upvotes: 0
Views: 462
Reputation: 33417
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)
Upvotes: 1