1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60
|
library(shiny)
library(plotly)
ui <- fluidPage(
actionButton("stream", "Turn stream on/off"),
plotlyOutput("plot")
)
server <- function(input, output, session) {
# initial values
yint <- c(0, 1)
# initiate graph with initial values
output$plot <- renderPlotly({
plot_ly(y = yint, x = seq_along(yint)) %>%
add_lines()
})
# reactiveValues() act very much like input values, but may be used to
# maintain state (e.g., are we currently streaming?)
rv <- reactiveValues(
stream = FALSE,
yend = sum(yint),
n = length(yint)
)
# turn streaming on/off when the button is pressed
observeEvent(input$stream, {
rv$stream <- if (rv$stream) FALSE else TRUE
})
observe({
# if we're not streaming, don't do anything
if (!rv$stream) return()
# re-execute this code block to every 100 milliseconds
invalidateLater(100, session)
# changing a reactive value "invalidates" it, so isolate() is needed
# to avoid recursion
isolate({
rv$n <- rv$n + 1
rv$yend <- rv$yend + sample(c(-1, 1), 1)
})
# add the new value to the plot
plotlyProxy("plot", session) %>%
plotlyProxyInvoke(
"extendTraces",
list(
y = list(list(rv$yend)),
x = list(list(rv$n))
),
list(0)
)
})
}
shinyApp(ui, server)
|