Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Use reactivePoll to accumulate data for output

Tags:

r

shiny

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?

like image 926
sckott Avatar asked Nov 11 '13 02:11

sckott


1 Answers

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
      })
    })
)
like image 84
jdharrison Avatar answered Oct 21 '22 16:10

jdharrison