以固定的时间间隔更新图/图 [英] Update graph/plot with fixed interval of time
问题描述
我在Shiny UI中有一个图.如果我更改任何输入参数,并且通过反应性图将发生变化.但是,让我们考虑以下情况: Shiny UI绘图中的绘图可以说股票的日内价格变动.为此,您查询一些实时数据源.现在,如果我创建一个刷新按钮,然后如果时间流逝,我继续单击刷新按钮.随着时间的流逝,该实时数据源中的新数据将随着新数据的到来而更新. 现在我的问题是我不想一直点击刷新按钮.但是我想使用计时器运行一个循环,以便它将在固定的时间间隔内进行检查,并且一旦有新数据出现,绘图将自动更新.某种Google Finance Graphs,会随着时间的推移而不断更新.
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.
因此,可以将问题简化如下:- 让我们从Shiny本身来看这个例子: ui.R
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")
)
))
和服务器.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)
再次
而不更改sliderInput. 请帮助我找出方法.
again without changing sliderInput. Please help me find out how to do that.
推荐答案
作为示例,您可以在本地运行以下命令:
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)
})
}
))
每5秒钟将生成一个不同的正常样本
A different normal sample will be generated every 5 seconds
这篇关于以固定的时间间隔更新图/图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!