'Show a message instead of/within plot while data is loading in R Shiny
Is there a possibility in R Shiny to replace a plot with a "loading" message while additional data is loading? I am using a big dataset in my app and since not all data is always necessary, I split the data in two parts and initially load only a smaller sample.
Only when the full dataset is chosen in a dropdown menu, I load the full sample. Since loading takes some time and freezes the plot, I would like to show a message instead and show the plot only when the loading is done. Example:
library(shiny)
ui <- fluidPage(
selectInput("select_length","Length",choices = c("Short","Long"), multiple= FALSE, selected = "Short"),
plotOutput("hist")
)
server <- function(input, output){
rv <- reactiveValues()
rv$df <- c(1,2)
observeEvent(input$select_length,{
Sys.sleep(5)
df_new <- c(3,4)
rv$df <- c(rv$df, df_new)
},
once = TRUE,
ignoreInit = TRUE
)
output$hist <- renderPlot({
barplot(rv$df)
})
}
shinyApp(ui = ui, server = server)
I would like to show a plot with a simple "loading" message while the additional data is being loaded, e.g.:
plot(c(0, 1), c(0, 1), ann = F, bty = 'n', type = 'n', xaxt = 'n', yaxt = 'n')
text(x = 0.5, y = 0.5, paste("Data is loading..."), cex = 1.6, col = "black")
Solution 1:[1]
You might like the ShinyBS package. I've used it for alerts when loading data before and it works great (it also looks fancy).
Here's an example of my usage....
This was the code used to create the alert, it's fairly simple. The user can exit it or you can call to delete as detailed below.
Server:
createAlert(session, 'upload_complete',title = 'Data Import Complete', content = 'You may continue to the other tabs', alertId = 'alert_delete', append = FALSE)
UI:
mainPanel(
....
bsAlert('upload_complete'),
)
Call to delete (in server)
closeAlert(session,'alert_delete')
Solution 2:[2]
would it be acceptable to show a progress bar while the additional data is loading? Shiny RStudio show an example for displaying a plot - http://shiny.rstudio.com/gallery/progress-bar-example.html
Solution 3:[3]
I suggest to use library(shinycssloaders).
is very simple and professional.
library(shinycssloaders)
- options like eg:
options(spinner.color="#0275D8", spinner.color.background="#ffffff", spinner.size=.5)
- add the loader with
withSpinner(Output method(objectname), type = 1-8)
https://www.listendata.com/2019/07/add-loader-for-shiny-r.html
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|---|
Solution 1 | |
Solution 2 | John Walker |
Solution 3 | Suraj Rao |