5

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?

marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
NRJ
  • 148
  • 2
  • 8

1 Answers1

11

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)

My Workflow

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!

dyrland
  • 608
  • 1
  • 7
  • 17