【发布时间】:2017-08-29 19:53:14
【问题描述】:
我想了解为什么在observeEvent() 内部使用invalidateLater() 时下表不会自动更新。我准备了以下程序来说明我的问题,“mytable2”使用reactiveTimer() 并且确实产生了所需的输出,但是“mytable”使用invalidateLater() 并且除非我单击“更新”按钮,否则不会自动更新。为什么?
library(shiny)
ui <- fluidPage(
sidebarLayout(
sidebarPanel(
actionButton("update", "Update")
),
mainPanel(
column(6, tableOutput('mytable')),
column(6, tableOutput('mytable2'))
)
)
)
server <- function(input, output) {
values <- reactiveValues(df = RenderMyTable())
observeEvent(invalidateLater(1000), {
values$df <- RenderMyTable() # This does not update after 1 sec
})
observeEvent(input$update, {
values$df <- RenderMyTable() # This does update upon clicking
})
output$mytable <- renderTable(values$df) # Depends on reactiveValues
autoInvalidate <- reactiveTimer(1000)
output$mytable2 <- renderTable({
autoInvalidate()
RenderMyTable() # >This does update after 1 sec
})
}
time1 <- Sys.time() # Start time
df <- data.frame(a = 1:1000) # Some data
RenderMyTable <- function(){
# Seconds since start time
time2 <- as.integer(difftime(Sys.time(), time1, units="secs"))
df.now <- df[1:time2,] # Updates each second
df.now
}
shinyApp(ui = ui, server = server)
跟进问题
另外,让绘图自动更新的适当方法是什么(如“mytable2”)?到目前为止,我获得更新的唯一方法是使用上面“mytable”中的“更新”按钮,但我希望它在 1 秒后自动更新,因为每一秒都有新数据开始添加到绘制数据表。
感谢您分享任何建议或资源。
【问题讨论】:
-
为什么不让
RenderMyTable()成为反应函数?
标签: r plot shiny auto-update