我正在从两个网址读取实时数据到Shiny,并显示最近更新的8条记录。然而,这些数据并不是最新的,
即显示前几天的记录,与来自url的实时记录不一致,
除非我再次粘贴并在浏览器中打开/刷新url。我想知道这是不是缓存的问题,我应该如何修改我的代码?
library(shiny)
shinyApp(
ui <- fluidPage(
column(3,
selectInput("station", "Select station",
c("a", "b")),
tableOutput("table")
)
),
server <- function(input, output) {
df <- eventReactive(input$station, {
if (input$station == "a") {
tail(read.csv("https://datagarrison.com/users/300234062103550/300234062107550/temp/Dawson_Creek__008.txt",
sep = "\t", skip = 2)[, c("Date_Time", "Rain_2440445_mm")], 8)
} else {
tail(read.csv("http://datagarrison.com/users/300234062103550/300234064336030/temp/10839071_004.txt",
sep = "\t", skip = 2)[, c("Date_Time", "Rain_2007476_mm")], 8)
}})
output$table <- renderTable(
df()
)
})
更新:结果是服务器本身遇到了更新问题,而不是代码。然而,答案显示了一种有用的方法。
发布于 2018-06-08 02:29:06
查看http://shiny.rstudio.com/gallery/timer.html,使用它,我们可以继续每秒刷新闪亮,因此可以跟上任何更新。请注意,我将清除您的工作会话,以确保您没有读取任何隐藏变量。
server <- function(input, output,session) {
....
output$table <- renderTable({
invalidateLater(1000, session)
df()
})
}
请注意,output$table<- renderTable
需要一个({
,而不仅仅是一个(
,才能在shiny中被认为是响应式的,您当前的代码可能会显示您以前创建的表。
下面是使用操作按钮强制刷新的代码(注意:替换URL)
library(shiny)
shinyApp(
ui <- fluidPage(
column(3,
selectInput("station", "Select station",
c("a", "b")),
tableOutput("table"),
#add action button
actionButton("button","Refresh")
)
),
server <- function(input, output) {
#trigger rest of code based on button being pressed.
observeEvent(input$button,{
if (input$station == "a") {
df<-tail(read.csv("URL",sep = "\t", skip = 2)[, c("Date_Time",
"Rain_2440445_mm")], 8)
} else {
df<-tail(read.csv("URL",sep = "\t", skip = 2)[, c("Date_Time",
"Rain_2007476_mm")], 8)
}#close if/else()
output$table <- renderTable({
df
}) # close renderTable()
})#close observeEvent()
} #close server
) #close shiny app
https://stackoverflow.com/questions/50747052
复制相似问题