In Shiny app, how to refresh data from MySQL db every 10 minutes if any change occured

≯℡__Kan透↙ 提交于 2020-01-01 19:54:10

问题


I've made dashboard using shiny, shinydashboard and RMySQL package. Following is what I wrote in order to refresh data every 10 minutes if any change occured.

In global.R

con = dbConnect(MySQL(), host, user, pass, db)
check_func <- function() {dbGetQuery(con, check_query}
get_func <- function() {dbGetQuery(con, get_query}

In server.R

function(input, output, session) {
    # check every 10 minutes for any change
    data <- reactivePoll(10*60*1000, session, checkFunc = check_func, valueFunc = get_func) 
    session$onSessionEnded(function() {dbDisconnect(con)})

However, above code infrequently generates corrupt connection handle error from check_func.

Warning: Error in .local: internal error in RS_DBI_getConnection: corrupt connection handle

Should I put dbConnect code inside server function? Any better ideas?

link: using session$onsessionend to disconnect rshiny app from the mysql server


回答1:


"pool" package is the answer: http://shiny.rstudio.com/articles/pool-basics.html

This adds a new level of abstraction when connecting to a database: instead of directly fetching a connection from the database, you will create an object (called a pool) with a reference to that database. The pool holds a number of connections to the database. ... Each time you make a query, you are querying the pool, rather than the database. ... You never have to create or close connections directly: the pool knows when it should grow, shrink or keep steady.

I've got answer from here. -> https://stackoverflow.com/a/39661853/4672289



来源:https://stackoverflow.com/questions/41424045/in-shiny-app-how-to-refresh-data-from-mysql-db-every-10-minutes-if-any-change-o

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!