【发布时间】:2018-12-27 15:38:30
【问题描述】:
我有一些数据,我正在尝试同时使用绘图 (plotly) 和表格 (DT) 逐行(每秒一行)可视化每个观察值。让每个对象独立更新我没有问题,但是我还没有找到同时更新这两个对象的正确方法。这是我一直在尝试的:
library(shiny)
library(DT)
library(plotly)
df <- matrix(rnorm(1000), nrow=1000, ncol=1) # Data
ui <- fluidPage(
sidebarLayout(
sidebarPanel(align = "center",
actionButton("button", "Start")
),
mainPanel(
column(width=6,
plotlyOutput("plot")
),
column(width=6,
dataTableOutput("table")
)
)
)
)
server <- function(input, output, session) {
values <- reactiveValues (step = 1) # Row index
# Static Table
output$table <- renderDataTable({
t(as.matrix(df[values$step, ])) # First observation only
})
# Static Plot
p <- plot_ly(
y = as.numeric(df[1, ]), # First observation only
type = "scatter",
mode = 'lines'
)
output$plot <- renderPlotly(p)
# Updating plot and table
observeEvent(input$button, {
time_0 <- Sys.time() # Initial time
while(TRUE){
Sys.sleep(1)
values$step <- difftime(Sys.time(), time_0, units="secs") # Counting seconds since button hit
dataTableProxy("table", session) %>% # Table doesn't update
replaceData(t(as.matrix(df[values$step, ])))
plotlyProxy("plot", session) %>% # Plot does update
plotlyProxyInvoke("extendTraces", list(y=list(list(df[values$step, ]))), list(0))
}
})
}
shinyApp(ui = ui, server = server)
如果我将dataTableProxy 调用放在observeEvent 之外并在它自己的observe 内调用,它将按预期更新(每秒更新值),但是一旦我点击“开始”它就会停止更新“ 按钮。然后只有情节会更新,这就是为什么我认为如果我将所有更新放在同一个observeEvent 中会更好,但现在只有情节正在更新。为什么values$step 每秒都在变化,但表值没有更新?
【问题讨论】: