'Keep libraries in memory even when all browsers are closed
I have a shiny app on RStudio's free shiny server that uses a fair number of libraries which results in a slow calculation time. The time for the UI to load is acceptable. I placed the libraries in a global.R file so that they can be shared across users.
# All libraries are in global.R for faster start times
source("<path to global.R>", local = T)
ui <- fluidPage(
)
server <- function(input, output, session) {
}
shinyApp(ui, server)
With this setup, the first person to access the app from a browser has a longish wait (~10s) for their first calculation. Subsequent calculations by this user and visits by different users are fast. If everyone closes their browser, the next user to come along will wait ~10 long seconds for the app to calculate.
Is there a way to configure things so that even the first user has a short wait time because the libraries are already in memory?
I think the result I'm looking for would be as if I had a browser tab open all the time pointing to my public shiny app and I hit reload and calculate once (to add the libraries that are not added when the ui starts up). Whenever the app times out and turns grey I reload and calculate again.
Solution 1:[1]
One potential option could be to use the app_idle_timeout shiny server configuration option and set it to 0. This would mean after the first time it is run, the process will never be killed so won't have to run again.
From the administrators guide:
Each Shiny Application has two timeouts associated with it:
app_init_timeout -- Describes the amount of time (in seconds) to wait for an application to start. After the specified number of seconds has elapsed, if the R process still has not become responsive, it will be deemed an unsuccessful startup and the connection will be closed. The default value for app_init_timeout is 60 seconds.
app_idle_timeout -- Defines the amount of time (in seconds) an R process with no active connections should remain open. After the last connection disconnects from an R process, this timer will start and, after the specified number of seconds, if no new connections have been created, the R process will be killed. The default value for app_idle_timeout is 5 seconds. Setting app_idle_timeout to 0 will disable the app idle time out.
Typically, these two parameters will be correlated. Shiny Applications that involve little processing to start (therefore have a small app_init_timeout) can often be closed with minimal concern (and thus would have a small app_idle_timeout). Conversely, applications that require a substantial amount of data to be loaded on startup may merit a longer app_init_timeout to give the data time to load, and a longer app_idle_timeout as the task of spawning a new process is more expensive and should be minimized.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|---|
Solution 1 | cnbrownlie |