Show Sidebar Hide Sidebar

Streaming in R

Getting Started with Streaming in R

New to Plotly?

Plotly's R library is free and open source!
Get started by downloading the client and reading the primer.
You can set up Plotly to work in online or offline mode.
We also have a quick-reference cheatsheet (new!) to help you get started!

Version Check

Version 4 of Plotly's R package is now available!
Check out this post for more information on breaking changes and new features available in this version.

library(plotly)
packageVersion('plotly')

Streaming in R

The plotlyProxy and plotlyProxyInvoke functions allow a plotly object to be modified by invoking any of the PlotlyJS methods. In particular, the extendTraces function allows you to add data to traces in an exisiting plotly object. See below application for code.

Basic Example

library(shiny)
library(plotly)

rand <- function() {
  runif(1, min=1, max=9)
}

ui <- fluidPage(
  includeCSS("styles.css"),

  headerPanel(h1("Streaming in Plotly", align = "center")),
  br(),
  div(actionButton("button", "Extend Trace"), align = "center"),
  br(),
  div(plotlyOutput("plot"), id='graph')
)

server <- function(input, output, session) {

    p <- plot_ly(
        y = c(rand(),rand(),rand()),
        type = 'scatter',
        mode = 'lines',
        line = list(
          color = '#25FEFD',
          width = 3
        )
      ) %>%
      layout(
        yaxis = list(range = c(0,10))
      )

    output$plot <- renderPlotly(p)

    observeEvent(input$button, {
      while(TRUE){
        Sys.sleep(1)
        plotlyProxy("plot", session) %>%
          plotlyProxyInvoke("extendTraces", list(y=list(list(rand()))), list(0))
      }
    })
}

shinyApp(ui, server)

Multiple Traces

library(shiny)
library(plotly)

rand <- function() {
  runif(1, min=1, max=9)
}

ui <- fluidPage(
  includeCSS("styles.css"),

  headerPanel(h1("Streaming in Plotly: Multiple Traces", align = "center")),
  br(),
  div(actionButton("button", "Extend Traces"), align = "center"),
  br(),
  div(plotlyOutput("plot"), id='graph')
)

server <- function(input, output, session) {

  p <- plot_ly(
      type = 'scatter',
      mode = 'lines'
    ) %>%
    add_trace(
      y = c(rand(),rand(),rand()),
      line = list(
        color = '#25FEFD',
        width = 3
      )
    ) %>%
    add_trace(
      y = c(rand(),rand(),rand()),
      line = list(
        color = '#636EFA',
        width = 3
      )
    ) %>%
    layout(
      yaxis = list(range = c(0,10))
    )

  output$plot <- renderPlotly(p)

  observeEvent(input$button, {
    while(TRUE){
      Sys.sleep(1)
      plotlyProxy("plot", session) %>%
        plotlyProxyInvoke("extendTraces", list(y=list(list(rand()), list(rand()))), list(1,2))
    }
  })

}

shinyApp(ui, server)

Timestamp

library(shiny)
library(plotly)

rand <- function() {
  runif(1, min=1, max=9)
}

t <- format(Sys.time(), "%H:%M:%S")

ui <- fluidPage(
  includeCSS("styles.css"),

  headerPanel(h1("Streaming in Plotly: Timestamp", align = "center")),
  br(),
  div(actionButton("button", "Extend Trace"), align = "center"),
  br(),
  div(plotlyOutput("plot"), id='graph')
)

server <- function(input, output, session) {

  p <- plot_ly(
    x = t,
    y = rand(),
    type = 'scatter',
    mode = 'lines',
    line = list(
      color = '#25FEFD',
      width = 3
    )
  ) %>%
    layout(
      yaxis = list(range = c(0,10))
    )

  output$plot <- renderPlotly(p)

  observeEvent(input$button, {
    while(TRUE){
      Sys.sleep(1)
      t <- format(Sys.time(), "%H:%M:%S")
      plotlyProxy("plot", session) %>%
        plotlyProxyInvoke("extendTraces", list(x=list(list(t)), y=list(list(rand()))), list(0))
    }
  })

}

shinyApp(ui, server)

Subplots

library(shiny)
library(plotly)

rand <- function() {
  runif(1, min=1, max=9)
}

ui <- fluidPage(
  includeCSS("styles.css"),

  headerPanel(h1("Streaming in Plotly: Subplots", align = "center")),
  br(),
  div(actionButton("button", "Extend Traces"), align = "center"),
  br(),
  div(plotlyOutput("plot"), id='graph')
)

server <- function(input, output, session) {

  p <- plot_ly(
    y = c(rand(),rand(),rand()),
    type = 'scatter',
    mode = 'lines',
    line = list(
      color = '#25FEFD',
      width = 3
    )
  ) %>%
    layout(
      yaxis = list(range = c(0,10))
    )

  pp <- plot_ly(
    y = c(rand(),rand(),rand()),
    type = 'scatter',
    mode = 'lines',
    line = list(
      color = '#636EFA',
      width = 3
    )
  ) %>%
    layout(
      yaxis = list(range = c(0,10))
    )

  s <- subplot(p,pp, nrows = 2)

  output$plot <- renderPlotly(s)

  observeEvent(input$button, {
    while(TRUE){
      Sys.sleep(1)
      plotlyProxy("plot", session) %>%
        plotlyProxyInvoke("extendTraces", list(y=list(list(rand()), list(rand()))), list(0,1))
    }
  })

}

shinyApp(ui, server)

Reference

Click here to visit the PlotlyJS functions.

?plotlyProxy
Still need help?
Contact Us

For guaranteed 24 hour response turnarounds, upgrade to a Developer Support Plan.