您可以使用reactiveTimer()
定期刷新您的页面。
你可以将你的情节保存在list
绘图以在每次刷新时立即打印它们。
我不得不重新订购renderPlot
函数所以step
迭代器一次仅渲染一张图
另外,我选择不立即开始第一次渲染来绘制“加载”图。
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 # which plot should be 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)