鉴于您使用 Shiny 构建仪表板,您可以使用 invalidateLater()。查看它的默认脚本:
if (interactive()) {
ui <- fluidPage(
sliderInput("n", "Number of observations", 2, 1000, 500),
plotOutput("plot")
)
server <- function(input, output, session) {
observe({
# Re-execute this reactive expression after 1000 milliseconds
invalidateLater(1000, session)
# Do something each time this is invalidated.
# The isolate() makes this observer _not_ get invalidated and re-executed
# when input$n changes.
print(paste("The value of input$n is", isolate(input$n)))
})
# Generate a new histogram at timed intervals, but not when
# input$n changes.
output$plot <- renderPlot({
# Re-execute this reactive expression after 2000 milliseconds
invalidateLater(2000)
hist(rnorm(isolate(input$n)))
})
}
shinyApp(ui, server)
}
来源:https://shiny.rstudio.com/reference/shiny/latest/invalidateLater.html
invalidateLater() 函数的第一个参数的值以毫秒为单位。 15 分钟是 900000 毫秒。
编辑:根据内部命令,确实可以在特定时间间隔内重复脚本,它可以通过更多方式完成(link1 或 link2 或其他包括invalidateLater() 在闪亮的上下文中)。以下是对其中一个的改编,以无限循环的形式每 6 秒绘制一次 ggplot2 直方图:
library(ggplot2)
plot_it <- function(){
a <- rnorm(1000, mean = 50, sd = 10)
print(summary(a))
p <- ggplot()+geom_histogram(aes(x=a), bins=40)
print(p)
}
repeat {
startTime <- Sys.time()
plot_it()
sleepTime <- startTime - Sys.time()+ 6
if (sleepTime > 0)
Sys.sleep(sleepTime)
}