【问题标题】:Connect tabItems and sub-Items with the main body in shinydashboard将 tabItems 和 sub-Items 与 shinydashboard 中的主体连接起来
【发布时间】:2019-10-01 16:26:27
【问题描述】:

我有一个带有 tabItems 和子项的闪亮仪表板。选择它们时,它们都应在主体中显示相应的项,但这不起作用。

#ui.r
library(shiny)
library(shinydashboard)
library(shinydashboardPlus)
ui <- dashboardPagePlus(
  dashboardHeaderPlus(title = "AA Tester"),
  dashboardSidebar(
    dashboardSidebar(
      sidebarMenu(id = 'sidebarmenu',
                  menuItem('Introduction', tabName = 'intro', icon = icon('dashboard')),
                  menuItem('Explore Funds', tabName = 'expf',
                           icon = icon('th'),
                           menuItem('Choose Strategy',
                                    tabName = 'retAA',
                                    icon = icon('line-chart'),
                                    selectInput("str", "Strategies:", choices=c("Strategy 1",
                                                                                     "Strategy 2",
                                                                                     "Strategy 3",
                                                                                     "Strategy 4",
                                                                                     "Strategy 5",
                                                                                     "Strategy 6",
                                                                                     "Strategy 7",
                                                                                     "Strategy 8"),multiple = T,selected = "Strategy 1"))
                  )))


  ),
  dashboardBody(
    tabItems(
      # First tab content
      tabItem(tabName = "intro",
              fluidRow(
                h2("Intro tab content")
              )
      ),

      # Second tab content
      tabItem(tabName = "retAA",
              h2("Exp tab content")
      )
    )
  )
)
#server.r
server <- function(input, output) { }

【问题讨论】:

    标签: r shiny shinydashboard


    【解决方案1】:

    您需要进行一些更改:

    1. 使用menuSubItem 而不是menuItem 作为标签retAA
    2. retAAselectInput 应该是兄弟姐妹
    3. 添加用于显示策略的 UI
    4. 您需要在selectInput 中添加监听变化的服务器代码
    
    #ui.r
    library(shiny)
    library(shinydashboard)
    library(shinydashboardPlus)
    ui <- dashboardPagePlus(
        dashboardHeaderPlus(title = "AA Tester"),
        dashboardSidebar(
            dashboardSidebar(
                sidebarMenu(id = 'sidebarmenu',
                            menuItem('Introduction', tabName = 'intro', icon = icon('dashboard')),
                            menuItem('Explore Funds', tabName = 'expf',
                                     icon = icon('th'),
                                     menuSubItem('Choose Strategy',
                                              tabName = 'retAA',
                                              icon = icon('line-chart')), # point 1
                                     selectInput("str", "Strategies:", choices=c("Strategy 1",
                                                                                 "Strategy 2",
                                                                                 "Strategy 3",
                                                                                 "Strategy 4",
                                                                                 "Strategy 5",
                                                                                 "Strategy 6",
                                                                                 "Strategy 7",
                                                                                 "Strategy 8"),multiple = T,selected = "Strategy 1") # point 2
                            )))
    
    
        ),
        dashboardBody(
            tabItems(
                # First tab content
                tabItem(tabName = "intro",
                        fluidRow(
                            h2("Intro tab content")
                        )
                ),
    
                # Second tab content
                tabItem(tabName = "retAA",
                        h2("Exp tab content"),
                        textOutput("userStr") # point 3
                )
            )
        )
    )
    #server.r
    server <- function(input, output) {
        output$userStr <- renderText(input$str) # point 4
    }
    
    shiny::shinyApp(ui,server)
    
    

    【讨论】:

    • 这是一个好方法。一个小问题是,我想在按下新标签(探索基金)后立即显示相应的面板,而不必按下“选择策略”。
    猜你喜欢
    • 1970-01-01
    • 2014-12-05
    • 2014-01-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-01-12
    • 2015-08-17
    相关资源
    最近更新 更多