闪亮的实时网址数据不会更新

时间:2018-06-07 17:29:12

标签: r url caching shiny reactive

我正在将两个网址的实时数据读入Shiny,并显示最新更新的8条记录。但是,数据并不是Shiny中最新的,

即。显示前几天的记录,与网址中的实时记录不一致,

除非我再次粘贴并打开/刷新浏览器中的网址。我想知道这是否是缓存问题,我应该如何更改我的代码?

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()
  )
})

更新 事实证明,服务器本身遇到了更新问题,而不是代码。但是,答案显示了一种有用的方法。

1 个答案:

答案 0 :(得分:1)

请参阅http://shiny.rstudio.com/gallery/timer.html,使用此功能,我们可以每秒继续刷新闪亮,从而跟上所有更新。注意我会清除你的工作会话,以确保你没有阅读任何隐藏的变量。

  server <- function(input, output,session) {

  ....

  output$table <- renderTable({
  invalidateLater(1000, session)
  df()
  })

 }

请注意,output$table<- renderTable需要({而不仅仅(被认为是闪亮的​​反应,您当前的代码可能会显示您之前创建的表格。

以下是使用操作按钮强制刷新的代码(注意:替换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
相关问题