I've looked at this and this thread and a few others but haven't been able to figure out my solution.
I have built a dashboard using R and Shiny, and said dashboard pulls in data from a Postgres db using the RPostgreSQL
package. Right now, the code for all the data pulling and analysis is done outside of the shinyServer
function, and only the displaying part (the output
and render
functions) is in the shinyServer
portion. I'd like to set it up so that the dashboard data is periodically refreshed and the charts are updated. I've looked into reactivePoll
and invalidateLater
and understand them, but can't quite figure out how to implement it in my code.
Here's a simplified example server.R
code:
library(RPostgreSQL)
drv <- dbDriver("PostgreSQL")
con <- dbConnect(drv, host='host', port='12345', dbname='mydb',
user='me', password='mypass')
myQuery <- "select * from table"
myTable <- dbGetQuery(con, myQuery)
foo <- nrow(myTable)
bar <- foo * 2
shinyServer(
function(input, output, session) {
output$foo <- renderText(foo)
output$bar <- renderText(bar)
session$onSessionEnded(function(){
dbDisconnect(con)
dbDisconnect(con2)
dbUnloadDriver(drv)
})
}
)
Now if I want foo
to update periodically, that requires me to refresh the dbGetQuery
command I have too, and I can't figure out how to make them all work together. Do I need to reformat things and put everything inside the shinyServer
function? I have about 250 lines of code and it feels wrong to throw them all in there, and just putting the data pull part in there could mess with the order of things. Any help is appreciated.