r - Is there a way to display plots as they build in Shiny instead of waiting for all of them? -
i have shiny dashboard many plots, of take multiple seconds build. once last 1 builds, display. instead each plot display completes. understand r single-threaded, seems though there must way "return execution display code" or that.
this code demonstrates issue:
library(shiny) ui <- fluidpage( title = "page loading test" , h1("page loading test") , plotoutput("plot1") , plotoutput("plot2") , plotoutput("plot3") , plotoutput("plot4") ) server <- function(input, output) { output$plot1 <- renderplot({ sys.sleep(10) plot(rnorm(50)) }) output$plot2 <- renderplot({ sys.sleep(10) plot(rnorm(50)) }) output$plot3 <- renderplot({ sys.sleep(10) plot(rnorm(50)) }) output$plot4 <- renderplot({ sys.sleep(10) plot(rnorm(50)) }) } shinyapp(ui = ui, server = server)
the sleeps merely emulate slow execution.
it takes 40s page display. page take 10s display plot1, additional 10s display plot2, etc. there call updatepage() can called @ bottom of each plot function?
on page, have loading animations running user aware activity occurring, makes more obvious when load @ once.
i could, of course, have simpler page, wouldn't dashboard. :)
you can use reactivetimer()
refresh page regularly.
and can save plots in list
of plots print them @ each refresh.
i had reorder renderplot
functions step
iterator renders 1 plot @ time
also chose not start first render right away plot "loading" plots.
library(shiny) ui <- fluidpage( title = "page loading test" , h1("page loading test") , plotoutput("plot1") , plotoutput("plot2") , plotoutput("plot3") , plotoutput("plot4") ) # loading plot plot(-1:1, -1:1, type = "n", xlab = "", ylab = "") text(0,0, "loading",cex = 5) loading <- recordplot() plotlist <- vector("list",4) step <- 0 # plot should rendered next server <- function(input, output, session) { autoinvalidate <- reactivetimer(10, session) output$plot4 <- renderplot({autoinvalidate(); if(step>4){plotlist[[4]]} else if(step==4){step <<- step+1 print("rendering step 4") sys.sleep(10) plotlist[[4]] <<- {plot(rnorm(50));recordplot()}} else loading }) output$plot3 <- renderplot({autoinvalidate(); if(step>3){plotlist[[3]]} else if(step==3){step <<- step+1 print("rendering step 3") sys.sleep(10) plotlist[[3]] <<- {plot(rnorm(50));recordplot()}} else loading }) output$plot2 <- renderplot({autoinvalidate(); if(step>2){plotlist[[2]]} else if(step==2){step <<- step+1 print("rendering step 2") sys.sleep(10) plotlist[[2]] <<- {plot(rnorm(50));recordplot()}} else loading }) output$plot1 <- renderplot({autoinvalidate(); if(step>1){plotlist[[1]]} else if(step==1){step <<- step+1 print("rendering step 1") sys.sleep(10) plotlist[[1]] <<- {plot(rnorm(50));recordplot()}} else {step <<-1;loading} }) } shinyapp(ui = ui, server = server)
Comments
Post a Comment