我正在使用reactPoll更新我的闪亮仪表板。第一次运行该应用程序时,它运行良好。我给了1分钟的时间间隔以刷新数据。 1分钟后,数据将按预期刷新。从第二分钟开始,检查功能每1分钟触发一次,但值功能未触发,我没有获得最新数据。
app.R
library(shiny)
library(shinythemes)
library(shinyWidgets)
library(shinydashboard)
library(shinycssloaders)
library(RPostgreSQL)
library(pool)
library(config)
library(plotly)
library(data.table)
Sys.setenv(R_CONFIG_ACTIVE = "xyz")
config <- config::get()
pool <- dbPool(
drv = dbDriver("PostgreSQL"),
host = config$host,
dbname = config$dbname,
port = config$port,
user = config$user,
password = config$password
)
onStop(function() {
poolClose(pool)
})
get_data <- function(pool) {
abc <- dbGetQuery(pool,"SELECT * FROM tablename") #Query to pull data
return(abc)
}
abc <- get_data(pool = pool)
ui <- dashboardPage(
dashboardHeader(
title = 'Dashboard'
),
dashboardSidebar(
sidebarMenu(
menuItem("pqr", tabName = "pqrs")
)
),
dashboardBody(
tabItems(
tabItem(
tabName = 'pqrs',
hemaTab("pqr",abc = abc)
)
)
)
)
server <- function(input, output, session) {
pollData <- reactivePoll(60000, session,
checkFunc = function() {
print("Entered Check")
Sys.time()
print(Sys.time())
},
valueFunc = function() {
print("Entered value")
get_data(pool)
}
)
order(input, output, session, data = pollData())
}
shinyApp(ui = ui, server = server)
pqrs.R
pqrs <- function(id, label = "pqr",pqrs) {
ns <- NS(id)
tabPanel('pqr',
tabsetPanel(
tabPanel('Downloads',
fluidPage(
fluidRow(
column(12,
DT::dataTableOutput("table")
)
)
)
)
)
)
}
order <- function(input, output, session, data) {
downloaddata <- reactive({
setDT(data)
})
output$table <- DT::renderDataTable( DT::datatable({
downloaddata()
})
)
}
I get the following result after running the app
"Entered Check"
[1] "2018-12-20 09:53:06 EST"
[1] "Entered Check"
[1] "2018-12-20 09:53:07 EST"
[1] "entered value"
After 1 minute the dashboard gets refreshed and I get the following
result
[1] "Entered Check"
[1] "2018-12-20 09:54:07 EST"
从第二分钟起,仪表盘不会刷新,但会触发检查功能并显示时间。
答案 0 :(得分:0)
tl; dr:尝试将调用
order()
的{{1}}函数放在poolData()
函数中
我认为问题是由于observe()
与它的工作方式相反,实际上需要在反应性环境中调用它才能正常运行。
当我运行以下程序时,我会遇到与您相同的问题:
reactivePoll
但是,如果我使用上面的第二种方式(将library(shiny)
ui <- fluidPage(
mainPanel(
verbatimTextOutput('text')
)
)
server <- function(input, output, session) {
pollData <- reactivePoll(600,session,
checkFunc = function() {
print("Entered Check")
Sys.time()
print(Sys.time())
},
valueFunc = function() {
print("entered value")
return('x')
}
)
ord <- function(data) {
print(data)
}
ord(isolate(pollData())) # 1: Only triggers once
# observe(ord(pollData())) # 2: Triggers every time
}
shinyApp(ui = ui, server = server)
[1] "Entered Check"
[1] "2018-12-20 09:39:35 PST"
[1] "entered value"
[1] "x"
[1] "Entered Check"
[1] "2018-12-20 09:39:35 PST"
[1] "Entered Check"
[1] "2018-12-20 09:39:36 PST"
...
调用包装在ord
函数中),那么它将按预期工作:
observe
我的猜测是,正在发生的事情是[1] "Entered Check"
[1] "2018-12-20 09:41:50 PST"
[1] "Entered Check"
[1] "2018-12-20 09:41:50 PST"
[1] "entered value"
[1] "x"
[1] "Entered Check"
[1] "2018-12-20 09:41:50 PST"
[1] "entered value"
[1] "x"
的工作方式与任何其他reactivePoll
表达式相同:调用该表达式时,它会检查其是否无效。如果不是,它将返回保存的值;否则,返回0。如果是,那么它将再次运行并返回更新的值。
我认为正在发生的事情是,当reactive*
检测到更改时,它不会告诉checkFunc
直接运行,只会使valueFunc
无效。无效后,reactive*
将在调用时运行。如果您从不调用它(因为您仅对副作用感兴趣),那么valueFunc
就不会运行。
在您的情况下,我认为(无论出于何种原因)由valueFunc
创建的反应性环境的作用类似于第一个选项:就像反应性环境一样,它可以访问shinydashboard
的值功能,但不会触发reactivePoll
。通过将valueFunc
函数包含在order
函数中,您将继续检查并重新调用该函数。
答案 1 :(得分:0)
当postgres数据库中的基础数据发生更改时,这对我自动更新非常有用:
library(shiny)
# Define UI for application that draws a histogram
ui <- fluidPage(
# Application title
titlePanel("Auto Update DB Table Viewer"),
# Table Viewer
DT::dataTableOutput("my_drugs_dt")
)
# Define server logic
server <- function(input, output) {
library(magrittr)
library(dplyr)
# Get DB auth token
rdshost <- "db.xxxxx.us-xxxx-x.rds.amazonaws.com"
username <- "my_user_name"
region <- "us-xxxx-x"
token <- reactiveValues(rds_token = system(paste0("aws rds generate-db-auth-token --hostname ", rdshost, " --port 5432 --username ", username, " --region ", region), intern = TRUE))
# Establish DB connection
myPool <- pool::dbPool(drv = RPostgres::Postgres(),
dbname="sengine-data",
host=rdshost,
user= username,
password = isolate(token$rds_token),
bigint = "numeric")
onStop(function() { pool::poolClose(myPool) })
# Pull the data from DB
# Note: using the changelog timestamp from the database would be the best way to do checkFunc.
#helpful: https://www.postgresql.org/docs/11/functions-info.html
#or this one: SELECT * FROM pg_last_committed_xact() https://www.tutorialdba.com/2017/11/postgresql-commit-timestamp-tracking.html
#This is how to modify the parameter in rds: https://aws.amazon.com/premiumsupport/knowledge-center/rds-postgresql-query-logging/
mysource_drugs <- reactivePoll(intervalMillis = 1000,
session = NULL,
checkFunc = function(){
conn <- pool::poolCheckout(myPool)
mod_stamp <- RPostgres::dbGetQuery(conn, "SELECT timestamp FROM pg_last_committed_xact()")
pool::poolReturn(conn)
return(mod_stamp)
},
valueFunc = function(){
myPool %>%
dplyr::tbl("drugs") %>%
dplyr::collect()
}
)
output$my_drugs_dt <- DT::renderDataTable({
mysource_drugs()
})
}
# Run the application
shinyApp(ui = ui, server = server)