我正在尝试在 Shiny 应用程序中创建一个定时计数器。当它每秒递增时,它应该用一些取决于计数器的新特性刷新一个图。这是一个基于“Old Faithful”应用程序的示例。它不起作用,但它得到了这个想法。我试图reactiveTimer()刷新情节,并用reactiveValues().
服务器
library(shiny)
shinyServer(function(input, output) {
refreshPlot <- reactiveTimer(intervalMs = 1000)
vals <- reactiveValues(counter = 0)
output$distPlot <- renderPlot({
refreshPlot()
vals$counter <- isolate(vals$counter) + 1
# generate bins based on input$bins from ui.R
x <- faithful[, 2]
n_bins <- input$bins + vals$counter
bins <- seq(min(x), max(x), length.out = n_bins)
# draw the histogram with the specified number of bins
hist(x, breaks = bins, col = 'darkgray', border = 'white')
})
})
Run Code Online (Sandbox Code Playgroud)
用户界面
library(shiny)
shinyUI(fluidPage(
# Application title
titlePanel("Old Faithful Geyser Data"),
# Sidebar with a slider input for number of bins
sidebarLayout(
sidebarPanel(
sliderInput("bins",
"Number of bins:",
min = 1,
max = 50,
value = 30)
),
# Show a plot of the generated distribution
mainPanel(
plotOutput("distPlot")
)
)
Run Code Online (Sandbox Code Playgroud)
invalidateLater是我要找的。谢谢,迪特。下面是有效的 server.R。
library(shiny)
shinyServer(function(input, output, session) {
vals <- reactiveValues(counter = 0)
output$distPlot <- renderPlot({
invalidateLater(millis = 1000, session)
vals$counter <- isolate(vals$counter) + 1
# generate bins based on input$bins from ui.R
x <- faithful[, 2]
n_bins <- input$bins + vals$counter
bins <- seq(min(x), max(x), length.out = n_bins)
# draw the histogram with the specified number of bins
hist(x, breaks = bins, col = 'darkgray', border = 'white')
})
})
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
555 次 |
| 最近记录: |