Use reactivePoll to accumulate data for output

↘锁芯ラ 提交于 2021-02-07 06:13:28

问题


I just noticed reactivePoll() recently - but need a bit of help figuring it out for my use case.

I want to accumulate data into a vector, list, or data.frame (doesn't matter), then plot the data, with the UI showing a graph with data accumulating as new data comes in. The problem is I don't see how to add new data to old data without replacing the old data. In this example (https://gist.github.com/sckott/7388855) I only get the initial row in the data.frame, and the newest one, not an accumulation of all data. For this example, how can I get the data.frame to grow, adding new data at the bottom?


回答1:


This can be done using the reactiveValues function:

runApp(
  list(
    ui = 
      mainPanel(
        tableOutput(outputId="dataTable")
      ),
    server = function(input, output, session) {
      myReact <- reactiveValues(df =data.frame(time=Sys.time()))
      readTimestamp <- function() Sys.time()
      readValue <- function(){
        data.frame(time=readTimestamp())
      }
      data <- reactivePoll(1000, session, readTimestamp, readValue)
      observe({
        myReact$df <- rbind(isolate(myReact$df), data())
      })
      output$dataTable <- renderTable({
        myReact$df
      })
    })
)


来源:https://stackoverflow.com/questions/19898113/use-reactivepoll-to-accumulate-data-for-output

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!