Update graph/plot with fixed interval of time

As an example you can run the following locally:

library(shiny)

runApp(list(
  ui = pageWithSidebar(    

  headerPanel("Hello Shiny!"),

  sidebarPanel(
    sliderInput("obs", 
                "Number of observations:", 
                min = 1,
                max = 1000, 
                value = 500)
  ),

  mainPanel(
    plotOutput("distPlot")
  )
),
  server =function(input, output, session) {
    autoInvalidate <- reactiveTimer(5000, session)
    output$distPlot <- renderPlot({
      autoInvalidate()
      # generate an rnorm distribution and plot it
      dist <- rnorm(input$obs)
      hist(dist)
    })

  }
))

A different normal sample will be generated every 5 seconds


This can also be solved using reactivePoll. The code is a lot simpler. it has also the advantage that you can use a check-function that does not necessarily invalidate the reactive expression because time has passed. You are able to write less resource-demanding code that way.

The sample, however, uses only Sys.tim() as check function. Since Sys.time() will be different every time it is called, the check function ALWAYS indicates that an update is necessary.

library(shiny)

runApp(list(
  ui = pageWithSidebar(

    headerPanel("Hello Shiny!"),

    sidebarPanel(
      sliderInput("obs",
                  "Number of observations:",
                  min = 1,
                  max = 1000,
                  value = 500)
    ),

    mainPanel(
      plotOutput("distPlot")
    )
  ),
  server = function(input, output, session) {
    dist <- reactivePoll(5000, session, function () Sys.time(), function () {
      rnorm(input$obs)
    })

    output$distPlot <- renderPlot({
      hist( req(dist()) )
    })
  }
))