I have deployed shiny server with several shiny apps. Each app is used as online dashboard which should be refreshed periodically (e.g. every 2 hours or at 10pm every day) according to my plan.
Now the server code for each application looks like
## server.R
data<-sqlQuery(...)
shinyServer(function(input, output,session) {
...
renderPlotly()
}
The main goal is to refresh the global data which is loaded using SQL every time the server starts without restarting the server itself. I'm not interested in reactive solutions which use reactivePoll or invalidateLater due to the fact that this approach will lead to a multiple queries each time the user refresh the page in browser.
Actually, I'm a bit confused that Shiny doesn't provide any native implementation of such feature. Are there any great workarounds to do this?
There are many reasons why a shiny app runs slower than expected. The most common reason is the Shiny app code has not been optimized. You can use the profvis package to help you understand how R spends its time. You also might want to make sure your server is large enough to host your apps.
The "Disconnected from Server" error is a generic message that means that the R session has shut down for some reason. This could happen for a multitude of reasons, ranging from missing objects, to data that takes too long to load, to the use of forbidden packages, to hitting the application timeout settings.
You should find that most applications you create will run properly on an iPhone, Android, or iPad without modification, however if you need to tweak things, you have full control to do that by dropping down to the javascript and html layers.
Step one is to get yourself an auto-scheduler. I use taskscheduleR
because I can't for the life of me figure out windows scheduler, and also the later
package for some other tricky things.
Then you need to schedule your SQL download, and save that SQL as an R image.
Finally, just use deployApp("...", launch.browser = F, forceUpdate = T)
library(later)
shiny.auto <- function(interval = 2*60*60){ # 2 hours 60 minutes 60 seconds
source("script source1")
source("shinyappscript")
later::later(shiny.auto, interval)
}
Script source1:
data<-sqlQuery(...)
save.image(...)
shinyappscript:
load(...)
deployApp("...", launch.browser = F, forceUpdate = T)
Hope this helps!
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With