Skip to content

Instantly share code, notes, and snippets.

@stevepowell99
Last active August 9, 2018 10:09
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save stevepowell99/0e32008b6d96e2085dbe3c2be21874ba to your computer and use it in GitHub Desktop.
Save stevepowell99/0e32008b6d96e2085dbe3c2be21874ba to your computer and use it in GitHub Desktop.
Hack to animate a series of DiagrammeR graphs.
# Animation not yet possible natively in DiagrammeR: https://github.com/rich-iannone/DiagrammeR/issues/8
# so this simple hack for Shiny is based on https://stackoverflow.com/questions/27194893/reset-animation-in-shiny-r-studio#27224900
library(shiny)
library(DiagrammeRsvg)
library(DiagrammeR)
ui <- (fluidPage(
# Application title
headerPanel("Diagrammer Animation"),
sidebarPanel(actionButton("goButton", "Go!")),
mainPanel(
plotOutput(outputId = "tsplot")
,
grVizOutput('diagram',
width = "100%",
height = "500px")
)
))
server <- (function(input, output, session) {
# initialize reactive values
ts <- reactiveValues(counter = 1)
output$diagram <- renderGrViz({
graph_1 <-
create_graph() %>%
add_path(n = 2)
graph_2 <-
create_graph() %>%
add_cycle(n = 33)
graph_3 <-
create_graph() %>%
add_star(n = 6)
# Create an empty graph series
# and add the graphs
series <-
create_graph_series() %>%
add_graph_to_graph_series(graph = graph_1) %>%
add_graph_to_graph_series(graph = graph_2) %>%
add_graph_to_graph_series(graph = graph_3)
render_graph_from_graph_series(graph_series = series,
graph_no = ts$counter)
})
observe({
isolate({
ts$counter = ts$counter + 1
if (ts$counter > 3)
ts$counter = 1
})
if (((isolate(ts$counter) < 100)) & (input$goButton > 0)) {
invalidateLater(2000, session)
}
})
})
runApp(list(ui = ui, server = server))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment