Search code examples
rshinyshinydashboard

Shinydashboard error passing functions to variables


I am planning to build a complex app and ideally I want to split parts into simplier parts in order to be easier for debugging and reading of my code. However, i get an error trying to implement that approach. Do I have to use source() function for this instead of passing by variables? Finally, what kind of approach do you recommend when trying to build a complex app?

library(shiny)
library(shinydashboard)

ui <- dashboardPage(header = Header_app, sidebar = Sidebar_app, body = Body_app)

Header_app <- dashboardHeader(title = "Hello")
Sidebar_app <- dashboardSidebar()
Body_app <- dashboardBody()

server <- function(input, output) {}

shinyApp(ui, server)

The error i get is:

Error in tagAssert(header, type = "header", class = "main-header") : object 'Header_app' not found


Solution

  • Nothing is wrong, you just need to declare the objects before you access them, i.e just declare it before the ui:

    library(shiny)
    library(shinydashboard)
    
    Header_app <- dashboardHeader(title = "Hello")
    Sidebar_app <- dashboardSidebar()
    Body_app <- dashboardBody()
    
    ui <- dashboardPage(header = Header_app, sidebar = Sidebar_app, body = Body_app)
    
    server <- function(input, output) {}
    
    shinyApp(ui, server)