简体   繁体   中英

shinydashboard : getting specific default page while using uiOutput

I was making reactive shiny web page and stucked at the code below.

library(shiny)
library(shinydashboard)

ui = dashboardPage(
  dashboardHeader(title = "header"),
  dashboardSidebar(
    sidebarMenuOutput("sideBar")),
  dashboardBody(
    uiOutput("test")
  )
)

server = shinyServer(function(input, output, session) { 
  output$sideBar <- renderMenu({
    sidebarMenu(id = "menu",
                menuItem("Dashboard", tabName ="dashboard", icon = icon('dashboard')),
                menuItem("DBcentor", tabName ="dbcenter", icon = icon('database'))
    )
  }) 
  output$test <- renderUI ({
    tabItems(
      tabItem(tabName = "dashboard", uiOutput("dashboardbody")),
      tabItem(tabName = "dbcenter", uiOutput("dbcenterbody"))
    )
  })
  output$dashboardbody <- renderUI ({
    box("Dashboard Body")
  })
  output$dbcenterbody <- renderUI ({
    box("Dbcenter Body")
  })
  
})

shinyApp(ui, server)

I used uiOutput to build body but after shiny app is loaded, nothing comes out at the dashboard page.

I changed my code like this,

library(shiny)
library(shinydashboard)

ui = dashboardPage(
  dashboardHeader(title = "header"),
  dashboardSidebar(
    sidebarMenuOutput("sideBar")),
  dashboardBody(
    tabItems(
      tabItem(tabName = "dashboard", uiOutput("dashboardbody")),
      tabItem(tabName = "dbcenter", uiOutput("dbcenterbody"))
    )
  )
)

server = shinyServer(function(input, output, session) { 
  output$sideBar <- renderMenu({
    sidebarMenu(id = "menu",
                menuItem("Dashboard", tabName ="dashboard", icon = icon('dashboard')),
                menuItem("DBcentor", tabName ="dbcenter", icon = icon('database'))
    )
  }) 
  output$dashboardbody <- renderUI ({
    box("Dashboard Body")
  })
  output$dbcenterbody <- renderUI ({
    box("Dbcenter Body")
  })
  
})

shinyApp(ui, server)

Of course it works and box("dashboadbody") comes out at the first page.

I want to use uiOutput because i should use input data to build menus and items. Any ways to use uiOutput and get box("dashboard") on the default page at the same time?

Try this

ui = dashboardPage(
  dashboardHeader(title = "header"),
  dashboardSidebar(
    sidebarMenuOutput("sideBar")),
  dashboardBody(
    uiOutput("test")
  )
)

server = shinyServer(function(input, output, session) { 
  output$sideBar <- renderMenu({
    sidebarMenu(id = "menu",
                menuItem("Dashboard", tabName ="dashboard", icon = icon('dashboard')),
                menuItem("DBcentor", tabName ="dbcenter", icon = icon('database'))
    )
  }) 
  
  output$dashboardbody <- renderUI ({
    box("Dashboard Body")
  })
  output$dbcenterbody <- renderUI ({
    box("Dbcenter Body")
  })
  updateTabItems(session,"menu","dbcenter")
  updateTabItems(session,"menu","dashboard")
  
  output$test <- renderUI ({
    tagList(
      tabItems(
        tabItem(tabName = "dashboard", uiOutput("dashboardbody")),
        tabItem(tabName = "dbcenter", uiOutput("dbcenterbody"))
      ))
  })
})

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