Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to update data in shiny app periodically?

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?

like image 814
NRJ Avatar asked Jul 31 '18 09:07

NRJ


People also ask

Why is the Shiny app so slow?

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.

Why does Shiny app disconnect from server?

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.

Do Shiny apps work on mobile?

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.


1 Answers

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!

like image 197
dyrland Avatar answered Oct 24 '22 11:10

dyrland