鑑於您使用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()
function's值第一個參數是毫秒。 15分鐘是900000毫秒。
編輯:每內部命令,這的確是可以重複的腳本在一個特定的時間間隔,它可以以更多的方式來完成(link1或link2或其他含invalidateLater()
閃亮的情況下)。這裏遵循其中之一的適應在一個無限循環的形式地塊GGPLOT2每6秒柱狀圖:
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)
}
對不起 - 我沒有用閃亮的 - 只是GGPLOT2 –
如果你不介意我問用什麼方式您是否僅使用ggplot2創建儀表板?通常當人們在R中談到儀表板時,使用閃亮的概念就是同義詞。至少對於我來說。對於內部R時間間隔重複,您也可以使用'重複'功能 –
對不起,如果它是TMI,但只是爲了完整性。我連接到MySQL數據庫並提取信息。我使用R來對數字進行一些算術運算。然後,我使用GGPlot創建一些圖,然後將其排列在一個網格中。我有文字和徽標,這些徽標被轉換爲grobs進行排列。 –