Shiny Dashboard - display a dedicated “loading..” page until initial loading of the data is done

前端 未结 3 1290
一整个雨季
一整个雨季 2020-12-03 11:09

I have initial loading of data from the DB in the server.R which takes a few seconds. Until this is done, the page displayed is distorted (wrong data in selecti

3条回答
  •  轻奢々
    轻奢々 (楼主)
    2020-12-03 11:45

    Here's a very simple example using shinyjs package

    The idea is to create the loading "page" and the content "page" under different IDs, have the content page initially hidden, and use show() and hide() after the app is ready

    library(shiny)
    library(shinyjs)
    
    load_data <- function() {
      Sys.sleep(2)
      hide("loading_page")
      show("main_content")
    }
    
    ui <- fluidPage(
      useShinyjs(),
      div(
        id = "loading_page",
        h1("Loading...")
      ),
      hidden(
        div(
          id = "main_content",
          "Data loaded, content goes here"
        )
      )
    )
    
    server <- function(input, output, session) {
      load_data()
    }
    
    shinyApp(ui = ui, server = server)
    

提交回复
热议问题