2016-06-13 3 views
1

Shinyアプリケーション内でタイムドカウンタを作成しようとしています。 1秒ごとにインクリメントすると、カウンタに依存する新しい特性を持つプロットが更新されます。ここには、「Old Faithful」アプリに基づく例があります。それは動作しませんが、それはアイデアを得る。私はreactiveTimer()プロットを更新しようとしましたが、カウンタはreactiveValues()と記録されています。1秒ごとにインクリメントしてプロットを更新するカウンタR Shiny

server.R

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') 

    }) 

}) 

ui.R

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") 
    ) 
) 
+1

これをテストするためのコンピュータは手元にありませんが、 'invalidateLater'を見てください –

答えて

2

invalidateLater私が探しているものです。ありがとう、Dieter。以下は動作する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') 

    }) 

}) 
関連する問題