简体   繁体   English

Shinydashboard:在多个 menuSubItems 的情况下,menuSubItem 不会在开始时呈现

[英]shinydashboard: menuSubItem not rendering at start in case of several menuSubItems

I found that menuSubItem content is not rendering in case of several (more than one) tabItems.我发现在多个(不止一个)tabItems 的情况下,menuSubItem 内容不会呈现。

Minimal example demonstrating this behavior is below.演示此行为的最小示例如下。

The desired behavior is to show content of the tabItem marked as selected = TRUE on startup.所需的行为是在启动时显示标记为selected = TRUE的 tabItem 的内容。 Now, the content shows up only after switching between menuSubItems in the sidebar.现在,只有在侧栏中的 menuSubItems 之间切换后才会显示内容。

How can I make it work?我怎样才能让它工作?

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)

Indeed, putting ui elements directly in UI solves it.确实,将 ui 元素直接放在 UI 中可以解决这个问题。 But the approach of putting everything inside ui is limited to situations that do not involve using reactive values.但是将所有内容放入 ui 的方法仅限于不涉及使用反应值的情况。 As I understand passing reactive value from server to ui is not possible in general (or limited to special cases).据我了解,一般不可能将反应值从服务器传递到 ui(或仅限于特殊情况)。 Please correct if I am wrong... Thanks如果我错了请纠正...谢谢

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)

Renaming your output to something other than "body" helps - please see this .将您的输出重命名为“body”以外的其他内容会有所帮助 - 请参阅

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)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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