闪亮的 eventReactive 不更新数据

eventReactive in shiny doesn't update data

在我下面的示例中,一旦在 RStudio 中 运行,通过单击滑块上的 "play" 按钮,移动的行数逐渐增加。但是通过暂停,然后将数据集名称更改为 iris,然后单击按钮 "Show" 并重新单击 "play",不会发生相同的行数动画增加。 。为什么?我该如何调整我的代码来做到这一点......即让动画出现在不同的数据集上?

下面的示例部分改编自 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)
}

发生这种情况的原因是:

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) 就会递增 1。应用程序打开时只有 0。所以, head(cars, ll()) 仅在第一次按下按钮之前运行。之后,input$button 递增,其值为 2, 3, 4, ... 等

ll() 是一个依赖于 input$x(您的滑块)的事件响应。因此,当您的滑块更新或按下播放符号时,ll() 会更新,并且您的 table 会重新显示。

每次第一次按下后,df() 运行。这是一个依赖于 input$button 的事件响应 - 它仅在按下按钮时运行。在按下按钮之前,您的 table 无法更新。

要解决此问题,您可以使用:

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

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