【问题标题】:shinydashboard menuItem with widgets not showing results带有未显示结果的小部件的闪亮仪表板菜单项
【发布时间】:2020-01-03 02:23:36
【问题描述】:

我正在使用 shinydashboard 构建一个应用程序,该应用程序包含一个带有 menuItem / tabItems 的侧栏菜单,但我无法获得第二个选项卡(包括小部件)来显示结果。正文仅显示第一个选项卡。这一定很简单,但我看不出我做错了什么......

这是一个可重现的例子:

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

为什么我在第二个标签项上看不到直方图?

【问题讨论】:

    标签: shiny shinydashboard


    【解决方案1】:

    shinydashboard 的侧边栏区分“childless”和“childfull”menuItems。通过将sliderInput 放在menuItem“直方图”中,它变成“childfull”,这意味着一个或多个子类别可用于导航,因此不需要在正文中显示内容,因为用户希望导航到其中一个孩子。请阅读this 了解更多信息。

    因此,如果您想在“直方图”选项卡的正文中显示内容,则它需要“无子”。

    这是一种将滑块放置在“直方图”选项卡之外的解决方案,但仅在选择“直方图”时才显示:

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

    【讨论】:

    • 实际上有没有办法让这个 menuItem 与 conditionalPanel 可折叠?一旦它被选中,我想有一种方法再次隐藏输入列表,以便放置在下面的其他菜单项也可见。这可能吗?
    • Childfull menuItems 是可折叠的,childless 不是。因此,这是不可能的。但是,您可以创建另一个 menuItem 并将 histogrm menuItem 放在其中以具有类似的行为。
    猜你喜欢
    • 2019-08-19
    • 1970-01-01
    • 1970-01-01
    • 2019-02-23
    • 2018-05-18
    • 2016-12-09
    • 2019-05-11
    • 1970-01-01
    • 2020-06-10
    相关资源
    最近更新 更多