将tabItem和子Item与shinydashboard中的主体连接

时间:2019-10-01 16:26:27

标签: r shiny shinydashboard

我有一个带有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) { }

1 个答案:

答案 0 :(得分:1)

您需要进行一些更改:

  1. 在标签menuSubItem中使用menuItem代替retAA
  2. retAAselectInput应该是兄弟姐妹
  3. 添加用于显示策略的UI
  4. 您需要添加服务器代码,以监听selectInput
  5. 中的更改

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