Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

shinydashboard does not work with uiOutput

Tags:

r

shiny

I setup the UI in server.R for more control, but shinyDashboard does not work when defined in server.R. I use this method with navBarPage without problems.

This code works

library(shiny)     
library(shinydashboard)

ui     <- dashboardPage(  dashboardHeader( ), 
                          dashboardSidebar(),
                          dashboardBody()   )

server <- shinyServer(function(input, output) {   })
runApp(list(ui= ui, server = server))

But this one just show an empty page

ui     <-  uiOutput('dash')
server <- shinyServer(function(input, output) { 
  output$dash <- renderUI({   
    dashboardPage(dashboardHeader( ), 
                  dashboardSidebar(),
                  dashboardBody()  )
  }) 
})
runApp(list(ui= ui, server = server))

This is an example using navBarPage, that works fine

ui     <-  uiOutput('nav')
server <- shinyServer(function(input, output) { 
  output$nav <- renderUI({   
    navbarPage("App Title",  
               tabPanel("Tab 1"),    
               tabPanel("Tab 2")  ) 
  })
})  
runApp(list(ui= ui, server = server))
like image 726
Eduardo Bergel Avatar asked Aug 08 '16 10:08

Eduardo Bergel


1 Answers

I don't think that you can use only a uiOutput the create a dashboard. I'm assuming that your goal is to create a dynamic dashboard. For that you need to define the header, body and side bar in your UI and use functions such as renderMenu on SERVER to create it. Here is an example to create a dashboard with all the UI defined in the SERVER.

ui <- dashboardPage(
  dashboardHeader(title = "My Page"),
  dashboardSidebar(sidebarMenuOutput("sideBar_menu_UI")),
  dashboardBody(
    uiOutput("body_UI"),
    uiOutput("test_UI")
  )
)

server <- shinyServer(function(input, output, session) { 
  output$sideBar_menu_UI <- renderMenu({
    sidebarMenu(id = "sideBar_Menu",
      menuItem("Menu 1", tabName="menu1_tab", icon = icon("calendar")),
      menuItem("Menu 2", tabName="menu2_tab", icon = icon("database"))
    )
  }) 
  output$test_UI <- renderUI ({
    tabItems(
      tabItem(tabName = "menu1_tab", uiOutput("menu1_UI")),
      tabItem(tabName = "menu2_tab", uiOutput("menu2_UI"))
    )
  })
  output$body_UI <- renderUI ({
    p("Default content in body outsite any sidebar menus.")
  })
  output$menu1_UI <- renderUI ({
    box("Menu 1 Content")
  })
  output$menu2_UI <- renderUI ({
    box("Menu 2 Content")
  })

})

runApp(list(ui= ui, server = server))

In this example, a menu for the sidebar is not selected by default and the content of body_UI will be visible all the time. If you want that your dashboard starts on a specific menu, put the sidebarMenu in your UI. Also you can delete the body_UI.

like image 56
Geovany Avatar answered Nov 08 '22 18:11

Geovany