Shinydashboard:如果有多个menuSubItems,则开始时不会呈现menuSubItem

时间:2018-10-08 16:01:39

标签: r shiny shinydashboard

我发现在有多个(多个)tabItem的情况下,menuSubItem的内容无法呈现。

下面是演示此行为的最小示例。

所需的行为是在启动时显示标记为selected = TRUE的tabItem的内容。现在,只有在侧边栏中的menuSubItems之间切换后,内容才会显示。

我如何使其工作?

library(shiny)
library(shinydashboard)

ui <- dashboardPage(
  dashboardHeader(title = "MINIMAL EXAMPLE"),
  dashboardSidebar(
    sidebarMenuOutput("menu")
  ),
  dashboardBody(
    uiOutput("body")
  )
)

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

  output$menu <- renderMenu(
    sidebarMenu(
        menuItem(text = "TABS", tabName = "TABS", startExpanded = T,
                 menuSubItem(text = "tab1", tabName="tab1",
                             icon = icon("cube"), selected = TRUE),
                 menuSubItem(text = "tab2", tabName="tab2",
                             icon = icon("cube"), selected = FALSE)
        )
   )
  )

  output$body <- renderUI({
    tabItems(
      tabItem(tabName = "tab1", 
              h4("MY TEXT 1")
      ),
      tabItem(tabName = "tab2", 
              h4("MY TEXT 2")
      ))
  })
}

shinyApp(ui = ui, server = server)

2 个答案:

答案 0 :(得分:0)

实际上,将ui元素直接放在UI中即可解决。 但是将所有内容放入ui的方法仅限于不涉及使用反应性值的情况。据我了解,通常无法将无功值从服务器传递到ui(或仅限于特殊情况)。如果我错了,请更正...谢谢

library(shiny)
library(shinydashboard)

ui <- dashboardPage(
  dashboardHeader(title = "MINIMAL EXAMPLE"),
  dashboardSidebar(
    sidebarMenuOutput("menu")
  ),
  dashboardBody(
    tabItems(
      tabItem(tabName = "tab1", 
              h4("MY TEXT 1")
      ),
      tabItem(tabName = "tab2", 
              h4("MY TEXT 2")
      ))
  )
)

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

  output$menu <- renderMenu(
    sidebarMenu(
        menuItem(text = "TABS", tabName = "TABS", startExpanded = T,
                 menuSubItem(text = "tab1", tabName="tab1",
                             icon = icon("cube"), selected = TRUE),
                 menuSubItem(text = "tab2", tabName="tab2",
                             icon = icon("cube"), selected = FALSE)
        )
    )
  )
}

shinyApp(ui = ui, server = server)

答案 1 :(得分:0)

将输出重命名为除“ body”以外的其他内容有帮助-请参阅this

library(shiny)
library(shinydashboard)

ui <- dashboardPage(
  dashboardHeader(title = "MINIMAL EXAMPLE"),
  dashboardSidebar(
    sidebarMenuOutput("menu")
  ),
  dashboardBody(
    uiOutput("myBodyOutput")
  )
)

server <- function(input, output, session) {
  
  output$myBodyOutput <- renderUI({
    tabItems(
      tabItem(tabName = "tab1", 
              h4("MY TEXT 1")
      ),
      tabItem(tabName = "tab2", 
              h4("MY TEXT 2")
      ))
  })
  
  output$menu <- renderMenu(
    sidebarMenu(id = "sidebarID",
                menuItem(text = "TABS", tabName = "TABS", startExpanded = T,
                         menuSubItem(text = "tab1", tabName="tab1",
                                     icon = icon("cube"), selected = TRUE),
                         menuSubItem(text = "tab2", tabName="tab2",
                                     icon = icon("cube"), selected = FALSE)
                )
    )
  )
  
}

shinyApp(ui = ui, server = server)