如何使用新数据自动更新闪亮的应用程序?
How to autoupadate a shiny app with new data?
我想在闪亮的应用程序中强制绘制高图表,以绘制来自 Mysql 数据库的新数据。我尝试了不同的方法,但 none 似乎有效。
起初我通过 globalR
中的 library(RMySQL) & library(pool) 连接到 sql
#--------Connect to SQL----------#
pool <- dbPool(
drv = RMySQL::MySQL(),
dbname = "xxx",
host = "x.x.x.x",
username = 'xxxx',
password = 'xxxx'
)
sql1 <- 'SELECT * FROM `results` WHERE `datetime` >= NOW() - INTERVAL 1 DAY'
query1 <- sqlInterpolate(pool, sql1)
notifier1 <- dbGetQuery(pool, sqlInterpolate(pool,'SELECT * FROM `results` WHERE `datetime` >= NOW() - INTERVAL 1 DAY' ))
lapply(dbListConnections( dbDriver( drv = "MySQL")), dbDisconnect)
然后我创建 2 个数据帧
all1 <- as.data.frame(notifier1%>% group_by(datetime, customer) %>% count(ping) %>% filter(datetime > Sys.time()-14400) %>%
mutate(percent = (round(100 * n/sum(n), 1))) %>% filter(ping=='1') %>% arrange(desc(datetime)))
all1_new <- transform(all1,datetime=as.numeric(as.POSIXct(datetime))*1000)
在我试过的服务器中:
server <- function(input, output, session) {
observeEvent(invalidateLater(60000, session),{
notifier1 <- dbGetQuery(pool, sqlInterpolate(pool,'SELECT * FROM `results` WHERE `datetime` >= NOW() - INTERVAL 1 DAY' ))
all1 <- as.data.frame(notifier1%>% group_by(datetime, customer) %>% count(ping) %>% filter(datetime > Sys.time()-14400) %>%
mutate(percent = (round(100 * n/sum(n), 1))) %>% filter(ping=='1') %>% arrange(desc(datetime)))
all1_new <- transform(all1,datetime= as.numeric(as.POSIXct(datetime))*1000)
})
以及我需要每隔 1 分钟显示新数据的高图代码:
output$Plot1 <- renderHighchart ({
# invalidateLater(60000,session)
highchart() %>%
hc_chart(type = "container",
zoomType= "x"
) %>%
#axis
hc_xAxis(type='datetime',
# categories=c(min2$datetime),
labels = list(rotation = 90,
format = '{value:%e-%b %H:%M}'),
showLastLabel = TRUE
) %>%
hc_yAxis(opposite = FALSE,
title = list(text = "Call Success"),
labels = list(format = "{value}%", style=list(fontSize='13px')), max = 100) %>%
hc_add_series(all1_new, "spline", hcaes(x=datetime, y=percent, group=customer)
)%>%
})
它可能看起来像:
check_db_update <- function() {
dbGetQuery(pool, sqlInterpolate(pool,
'SELECT COUNT(*) FROM `results`'
))
}
get_data_from_db <- function() {
dbGetQuery(pool, sqlInterpolate(pool,
'SELECT * FROM `results` WHERE `datetime` >= NOW() - INTERVAL 1 DAY'
))
}
server <- function(input, output, session) {
data_rx <-
reactivePoll(intervalMillis = 6e4, session = session,
checkFunc = check_db_update, valueFunc = get_data_from_db)
data_wrangled_rx <- reactive({
data_rx() %>%
group_by(datetime, customer) %>%
count(ping)
# ...
})
output$Plot1 <- renderHighchart ({
highchart() %>%
hc_add_series(data = data_wrangled_rx())
# ...
})
# ...
}
我想在闪亮的应用程序中强制绘制高图表,以绘制来自 Mysql 数据库的新数据。我尝试了不同的方法,但 none 似乎有效。 起初我通过 globalR
中的 library(RMySQL) & library(pool) 连接到 sql#--------Connect to SQL----------#
pool <- dbPool(
drv = RMySQL::MySQL(),
dbname = "xxx",
host = "x.x.x.x",
username = 'xxxx',
password = 'xxxx'
)
sql1 <- 'SELECT * FROM `results` WHERE `datetime` >= NOW() - INTERVAL 1 DAY'
query1 <- sqlInterpolate(pool, sql1)
notifier1 <- dbGetQuery(pool, sqlInterpolate(pool,'SELECT * FROM `results` WHERE `datetime` >= NOW() - INTERVAL 1 DAY' ))
lapply(dbListConnections( dbDriver( drv = "MySQL")), dbDisconnect)
然后我创建 2 个数据帧
all1 <- as.data.frame(notifier1%>% group_by(datetime, customer) %>% count(ping) %>% filter(datetime > Sys.time()-14400) %>%
mutate(percent = (round(100 * n/sum(n), 1))) %>% filter(ping=='1') %>% arrange(desc(datetime)))
all1_new <- transform(all1,datetime=as.numeric(as.POSIXct(datetime))*1000)
在我试过的服务器中:
server <- function(input, output, session) {
observeEvent(invalidateLater(60000, session),{
notifier1 <- dbGetQuery(pool, sqlInterpolate(pool,'SELECT * FROM `results` WHERE `datetime` >= NOW() - INTERVAL 1 DAY' ))
all1 <- as.data.frame(notifier1%>% group_by(datetime, customer) %>% count(ping) %>% filter(datetime > Sys.time()-14400) %>%
mutate(percent = (round(100 * n/sum(n), 1))) %>% filter(ping=='1') %>% arrange(desc(datetime)))
all1_new <- transform(all1,datetime= as.numeric(as.POSIXct(datetime))*1000)
})
以及我需要每隔 1 分钟显示新数据的高图代码:
output$Plot1 <- renderHighchart ({
# invalidateLater(60000,session)
highchart() %>%
hc_chart(type = "container",
zoomType= "x"
) %>%
#axis
hc_xAxis(type='datetime',
# categories=c(min2$datetime),
labels = list(rotation = 90,
format = '{value:%e-%b %H:%M}'),
showLastLabel = TRUE
) %>%
hc_yAxis(opposite = FALSE,
title = list(text = "Call Success"),
labels = list(format = "{value}%", style=list(fontSize='13px')), max = 100) %>%
hc_add_series(all1_new, "spline", hcaes(x=datetime, y=percent, group=customer)
)%>%
})
它可能看起来像:
check_db_update <- function() {
dbGetQuery(pool, sqlInterpolate(pool,
'SELECT COUNT(*) FROM `results`'
))
}
get_data_from_db <- function() {
dbGetQuery(pool, sqlInterpolate(pool,
'SELECT * FROM `results` WHERE `datetime` >= NOW() - INTERVAL 1 DAY'
))
}
server <- function(input, output, session) {
data_rx <-
reactivePoll(intervalMillis = 6e4, session = session,
checkFunc = check_db_update, valueFunc = get_data_from_db)
data_wrangled_rx <- reactive({
data_rx() %>%
group_by(datetime, customer) %>%
count(ping)
# ...
})
output$Plot1 <- renderHighchart ({
highchart() %>%
hc_add_series(data = data_wrangled_rx())
# ...
})
# ...
}