Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
340 views
in Technique[技术] by (71.8m points)

r - Update graph/plot with fixed interval of time

I have a plot in Shiny UI. If I change any input parameter and through reactivity plot will change. But let's consider following situation:- The plot in Shiny UI plotting let say intra-day price move of a stock. And for that you query some live data source. Now If I create a refresh button and then if time passes by I keep on clicking on refresh button. The plot will be updated as new data arrives as time goes into that live data source. Now my question is I don't want to keep clicking on refresh button. But I want to run a loop with timer so that it will check over a fixed interval of time and as soon as new data comes the plot will auto update. Something sort of Google Finance Graphs which keeps updating over time.

So the problem can be simplified as follows :- Let's consider this example from Shiny itself :- ui.R

library(shiny)    

shinyUI(pageWithSidebar(    

  headerPanel("Hello Shiny!"),

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

  mainPanel(
    plotOutput("distPlot")
  )
))

and server.R

library(shiny)

shinyServer(function(input, output) {

  output$distPlot <- renderPlot({

    # generate an rnorm distribution and plot it
    dist <- rnorm(input$obs)
    hist(dist)
  })

})

Now I want to generate a different random sample from normal distribution without any input activity. So basically I want to call

dist <- rnorm(input$obs)
hist(dist)

again without changing sliderInput. Please help me find out how to do that.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...