在Shiny应用程序中,如果发生任何更改,如何每10分钟从MySQL db刷新数据 [英] In Shiny app, how to refresh data from MySQL db every 10 minutes if any change occured
问题描述
我已经使用闪亮的Shinydashboard和RMySQL软件包制作了仪表板. 以下是我写的内容,以便每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.
在global.R
con = dbConnect(MySQL(), host, user, pass, db)
check_func <- function() {dbGetQuery(con, check_query}
get_func <- function() {dbGetQuery(con, get_query}
在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)})
但是,以上代码很少会从check_func生成损坏的连接句柄错误.
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
我应该将dbConnect代码放入服务器功能中吗? 还有更好的主意吗?
Should I put dbConnect code inside server function? Any better ideas?
链接:使用session $ onsessionend断开rshiny mysql服务器上的应用程序
推荐答案
"pool"包是答案: http://shiny.rstudio.com/articles/pool-basics.html
"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.
我从这里得到了答案. -> https://stackoverflow.com/a/39661853/4672289
I've got answer from here. -> https://stackoverflow.com/a/39661853/4672289
这篇关于在Shiny应用程序中,如果发生任何更改,如何每10分钟从MySQL db刷新数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!