1

How do I add a loader page while my UI populates with computations on my dataset inside the server function? My UI populates with values in about 30 secs. So I want this loader page to show for 30 secs and then hide it to show my actual UI which would have filled up by then.

Any help would be appreciated. Here's the sample code below:

ui <- fluidPage(
  useShinyjs(),
  div(
    id = "loading_page",
    h1("Loading...")
  ),

  titlePanel("XYZ"),
  sidebarLayout(
    sidebarPanel(
      
      p("Lorem Ipsum"),
      
      selectInput(inputId = "ab", label = "SelectSomething", choices = c("A","B","C","D")),
      p("Please Wait for 30 secs for data to load.."),
      sliderInput(inputId = "Age", label = "Age Range", min=16, max=45, value=c(16,45)), 
        actionButton(inputId = "update", label = "Go!")
    ),
    mainPanel(
      h3("ABC:"),
      uiOutput("table"),
      br(),
      uiOutput("OP1"),
      br(),
      uiOutput("OP2"),
      uiOutput("OP3"),
      br(),
      uiOutput("OP4") 
    )
  )
)


dataset<-readRDS(file = "my_data.rds")  

server <- function(input, output, session) {

})

Aakash
  • 43
  • 3

1 Answers1

0

It appears that you are using code from https://stackoverflow.com/a/35665217/3358272, which makes this a dupe of sorts, but perhaps not in the vein of: how to do that with more UI components.

Just wrap all of your title and sidepanel stuff in hidden(div(...)) with an id.

From there, you can allow other things to do some work by using a reactive observe block that fires twice: the first time it sets a wake-up alarm (3000 milliseconds here), the second time it removes the #loading_page div.

ui <- fluidPage(
  useShinyjs(),
  div(
    id = "loading_page",
    h1("Loading...")
  ),
  hidden(
    div(
      id = "main_content",
      titlePanel("XYZ"),
      sidebarLayout(
        sidebarPanel(
          p("Lorem Ipsum"),
          selectInput(inputId = "ab", label = "SelectSomething", choices = c("A","B","C","D")),
          p("Please Wait for 30 secs for data to load.."),
          sliderInput(inputId = "Age", label = "Age Range", min=16, max=45, value=c(16,45)), 
          actionButton(inputId = "update", label = "Go!"),
          ),
        mainPanel(
          h3("ABC:"),
          uiOutput("table"),
          br(),
          uiOutput("OP1"),
          br(),
          uiOutput("OP2"),
          uiOutput("OP3"),
          br(),
          uiOutput("OP4")
        )
      )
    )
  )
)
server <- function(input, output, session) {
  already_started <- FALSE
  observe({
    if (already_started) {
      removeUI("#loading_page")
    } else {
      invalidateLater(3000, session)
      already_started <<- TRUE
    }
  })
}

It would also be "right" to use a reactiveVal for already_started. The reason I didn't was that we don't need reactivity from it, just a two-shot memory.

r2evans
  • 141,215
  • 6
  • 77
  • 149