What is the most performant way to update a graph with new data?

It’s been a while but however, here is an R example for replacing data using Plotly.restyle:

library(shiny)
library(plotly)

initial_plot <- plot_ly(
  type = 'scatter',
  mode = 'lines',
  line = list(color = '#1166FF',
              width = 3)
)

ui <- fluidPage(
  plotlyOutput("myPlot")
)

server <- function(input, output, session) {
  
  myData <- reactiveValues(x = 1, y = runif(1, min=1, max=10))
  
  output$myPlot <- renderPlotly({
    initial_plot
  })
  
  myPlotProxy <- plotlyProxy("myPlot", session)

  observe({
    invalidateLater(1000)
    myData$x <- isolate(c(myData$x, length(myData$x)+1))
    myData$y <- isolate(c(myData$y, runif(1, min=1, max=10)))
    print(myData$x)
    print(myData$y)
  })
  
  observe({
    plotlyProxyInvoke(myPlotProxy, "restyle", list(x = list(myData$x), y = list(myData$y)))
  })
  
}

shinyApp(ui, server)

2 Likes