2015-09-21 116 views
1

在我的下例中,一旦在RStudio中运行,通过单击滑块上的“play”按钮,移位的行数逐渐增加。但是通过暂停,然后将数据集名称更改为iris,然后单击“显示”按钮并重新单击“播放”,同样动态增加行数不会发生......为什么?我该如何调整我的代码才能这样做......即。让动画与不同的数据集一起出现?eventReactive in shiny不会更新数据

下面的例子部分地从eventReactive()函数适于

require(shiny) 
if (interactive()) { 
    ui <- fluidPage(
    column(4, 
      sliderInput('x',label='Num Rows',min=2,max=30,step=1,value=3,animate = TRUE), 
      textInput('tbl_nm',label='Data Set',value='cars'), 
      br(), 
      actionButton("button", "Show") 
    ), 
    column(8, tableOutput("table")) 
    ) 
    server <- function(input, output) { 

    # reactively adjust the number of rows 
    ll <- eventReactive(input$x,{ 
     input$x 
    }) 


    # change the data sets after clicking the button 
    dat <- eventReactive(input$button,{ 
     if(input$tbl_nm=='cars'){ 
     dat <- cars 
     } else { 
     dat <- get(input$tbl_nm) 
     } 
     return(dat) 
    }) 

    # Take a reactive dependency on input$button, but 
    # not on any of the stuff inside the function 
    df <- eventReactive(input$button, { 
     yy <- ll() 
     # choose only the relevant data... 
     head(dat(),yy) 
    }) 

    # show the final table 
    output$table <- renderTable({ 

     if(input$button==0){ 
     # show the first few lines of cars at the begining 
     head(cars, ll()) 
     } else { 
     # show the selected data 
     df() 
     } 

    }) 
    } 


    shinyApp(ui=ui, server=server) 
} 

回答

3

发生这种情况的原因是:

output$table <- renderTable({ 

    if(input$button==0){ 
    # show the first few lines of cars at the begining 
    head(cars, ll()) 
    } else { 
    # show the selected data 
    df() 
    } 

}) 

每一个按钮被一个按压时,其值(input$button)的增量时间。应用程序打开时仅为0。因此, head(cars, ll())只在第一次按下按钮之前运行。之后,input$button递增,其值为2,3,4,...等。

ll()是一个事件反应式,取决于input$x(您的滑块)。因此,当您的滑块更新或按下播放标志时,更新ll(),您的表格将重新显示。

对于每次第一次印刷后,运行df()。这是一个事件反应,取决于input$button - 它只在按下按钮时运行。您的表格被阻止更新,直到按下按钮。

为了解决这个问题,你可以使用:

df <- eventReactive(input$button | input$x, { 
    yy <- ll() 
    # choose only the relevant data... 
    head(dat(),yy) 
}) 

为您df()代替。如果按下按钮或者滑块更新,它现在将更新