首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >闪亮的实时url数据不会更新

闪亮的实时url数据不会更新
EN

Stack Overflow用户
提问于 2018-06-08 01:29:12
回答 1查看 904关注 0票数 3

我正在从两个网址读取实时数据到Shiny,并显示最近更新的8条记录。然而,这些数据并不是最新的,

即显示前几天的记录,与来自url的实时记录不一致,

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

代码语言:javascript
复制
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()
  )
})

更新:结果是服务器本身遇到了更新问题,而不是代码。然而,答案显示了一种有用的方法。

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2018-06-08 02:29:06

查看http://shiny.rstudio.com/gallery/timer.html,使用它,我们可以继续每秒刷新闪亮,因此可以跟上任何更新。请注意,我将清除您的工作会话,以确保您没有读取任何隐藏变量。

代码语言:javascript
复制
  server <- function(input, output,session) {

  ....

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

 }

请注意,output$table<- renderTable需要一个({,而不仅仅是一个(,才能在shiny中被认为是响应式的,您当前的代码可能会显示您以前创建的表。

下面是使用操作按钮强制刷新的代码(注意:替换URL)

代码语言:javascript
复制
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
票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/50747052

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档