我尝试使用ReactivePoll
来更新数据框。当a)将新行添加到数据库表并且b)input$type
更改时,check函数应返回不同的值。
# checks to see if input$type has changed or the table corresponding to input$type has grown
check.data <- function() {
row.count <- 0
if (data.connection$source == "pg") {
pg.connection$connection <- dbConnect(pg.connection$driver, dbname = pg.settings.database(), host = pg.settings.host(), port = as.integer(pg.settings.port()), user = pg.settings.user(), password = pg.settings.password())
row.count <- as.integer(dbGetQuery(pg.connection$connection, paste0("SELECT count(*) FROM ", input$type ,"datum")))
dbDisconnect(pg.connection$connection)
}
print(row.count) # make sure return value is changing when it should
row.count
}
# refreshes data
refresh.data <- function() {
print("check") # check if function is executing
frame <- data.frame()
if (data.connection$source == "pg") {
pg.connection$connection <- dbConnect(pg.connection$driver, dbname = pg.settings.database(), host = pg.settings.host(), port = as.integer(pg.settings.port()), user = pg.settings.user(), password = pg.settings.password())
frame <- dbReadTable(pg.connection$connection, paste0(input$type, "datum"))
dbDisconnect(pg.connection$connection)
}
frame
}
data <- reactivePoll(15000, NULL, check.data, refresh.data)
根据R Shiny的文档,&#34;检查功能通过返回与上次呼叫时不同的值来指示更改。&#34;我已经确认row.count
正在发生变化,但是&#34;检查&#34;从不打印,暗示值函数永远不会执行。我知道行计数不是检查更改的可靠方法(表可能具有相同的行数),但row.count
正在更改。