如果动态创建侧边栏菜单,则内容不会显示在仪表板正文中

时间:2016-04-23 22:59:40

标签: dynamic menu shiny sidebar dashboard

我注意到如果侧边栏菜单是由服务器代码动态创建的,则在应用加载之前内容不会显示,直到单击选项卡为止。

在示例中,我使用了闪亮仪表板上的示例代码。该应用程序按预期运行。但是,正如您所看到的,我将侧边栏菜单更改为动态创建,应用程序加载时会显示空白页面。我必须点击侧栏的第一个标签来调出页面。有没有办法显示默认页面,即仪表板标签页?请注意我在闪亮的仪表板github页面上输入了一个问题。非常感谢你的帮助。

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

ui <- dashboardPage(
dashboardHeader(title = "Basic dashboard"),
## Sidebar content
# dashboardSidebar(
# sidebarMenu(
# menuItem("Dashboard", tabName = "dashboard", icon = icon("dashboard")),
# menuItem("Widgets", tabName = "widgets", icon = icon("th"))
# )
# ),

dashboardSidebar(width=150, sidebarMenuOutput("sideMenu")
),

dashboardBody(

tabItems(
  # First tab content
  tabItem(tabName = "dashboard",
          fluidRow(
            box(plotOutput("plot1", height = 250)),

            box(
              title = "Controls",
              sliderInput("slider", "Number of observations:", 1, 100, 50)
            )
          )
  ),

  # Second tab content
  tabItem(tabName = "widgets",
          h2("Widgets tab content")
  )
)
)
)

server <- function(input, output, session) {

output$sideMenu <- renderMenu({
sidebarMenu(
menuItem("Dashboard", tabName = "dashboard", icon = icon("dashboard")),
menuItem("Widgets", tabName = "widgets", icon = icon("th"))
)
})

set.seed(122)
histdata <- rnorm(500)

output$plot1 <- renderPlot({
data <- histdata[seq_len(input$slider)]
hist(data)
})
}

shinyApp(ui, server)`

1 个答案:

答案 0 :(得分:2)

您需要将其中一个标签设置为有效。

例如:

# First tab content
  tabItem(tabName = "dashboard", class = "active",
           fluidRow(
            box(plotOutput("plot1", height = 250)),

            box(
              title = "Controls",
              sliderInput("slider", "Number of observations:", 1, 100, 50)
            )
          )
     ),