Sometimes the data used in a document takes a long time to extract (e.g., when extracted by a SQL query, an API, or an integration). It's also possible that some calculations may be slow (e.g., hierarchical Bayesian calculations). Where these slow operations occur while users are using a Displayr document, the result is that the document slows down.
A solution to this problem is to schedule these slow operations to occur when the users are not using the document.
Worked example
The code below obtains the last five years of stock market prices for Microsoft. While this example is fast to compute, it's used as a surrogate for a more expensive calculation (e.g., a slow SQL query).
library(quantmod)
library(reshape2)
data <- new.env()
suppressWarnings(getSymbols("MSFT",
env = data,
from = seq(as.Date(Sys.Date()), length = 2, by = "-5 years")[2],
to = Sys.Date(),
auto.assign = TRUE))
all.data <- data[[ls(data)[1]]]
stock.prices <- data.frame(all.data)
The code below is the same as the code above, but with the UpdateAt code added to the end, causing the code to run at 1 AM New York time.
library(quantmod)
library(reshape2)
data <- new.env()
suppressWarnings(getSymbols("MSFT",
env = data,
from = seq(as.Date(Sys.Date()), length = 2, by = "-5 years")[2],
to = Sys.Date(),
auto.assign = TRUE))
all.data <- data[[ls(data)[1]]]
stock.prices <- data.frame(all.data)
library(flipAPI)
UpdateAt("07-01-2022 1:00:00",
us.format = TRUE,
time.zone = "America/New_York",
units = "days",
frequency = 1,
options = "wakeup")
"Exporting data to cloud drive every day"