【问题标题】:Updating plot and table data simultaneously within a Shiny app在 Shiny 应用程序中同时更新绘图和表格数据
【发布时间】: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 每秒都在变化,但表值没有更新?

【问题讨论】:

    标签: r shiny plotly dt


    【解决方案1】:

    这似乎是一个范围界定问题(将 t(as.matrix(df[1, ])) 更改为 t(as.matrix(df[values$step, ])) 以查看差异)。我通过在自己的observe 中更新values$step 找到了另一种处理方法:

    library(shiny)
    library(DT)
    library(plotly)
    
    df <- matrix(rnorm(1000), nrow=1000, ncol=1)  # Data
    
    ui <- fluidPage(
      sidebarLayout(
    
        sidebarPanel("Empty panel"),
    
        mainPanel(
          column(width=6,
            plotlyOutput("plot")
          ),
          column(width=6,
            dataTableOutput("table")
          )
        )
      )
    )
    
    server <- function(input, output, session) {
    
      values <- reactiveValues (step = 1)  # Row index
    
      observe({
        invalidateLater(1000)
    
        isolate({
        values$step <- values$step + 1  # Updates after 1 second
        })
      })
    
      # Static Table
      output$table  <- renderDataTable({
        t(as.matrix(df[1, ]))  # 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
      observe({
    
        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)
    

    【讨论】:

      猜你喜欢
      • 2017-04-01
      • 2021-11-10
      • 2019-07-02
      • 2020-01-08
      • 2019-03-23
      • 2017-08-22
      • 2021-03-13
      • 2021-10-21
      • 2018-04-03
      相关资源
      最近更新 更多