简体   繁体   中英

Connect tabItems and sub-Items with the main body in shinydashboard

I have a shinydashboard with tabItems and sub-items.Every one them when selected should display the the respective Item in the main body but this does not work.

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

There are several changes you need to make:

  1. Use menuSubItem instead of menuItem for tab retAA
  2. retAA and selectInput should be siblings
  3. Add an UI for displaying strategies
  4. You need to add server codes which listen to changes in the 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)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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