Robert Long
Robert Long

Reputation: 6897

Rendering an animated plot in R shiny with ggplot and plotly

I have an animated plot that displays properly via an R (not a shiny app) script:

load("HF.RData") # this contains the dataframe dt1.HF

p <- ggplot(dt1.HF, aes(x = tm, y = HF, colour = team)) +
  geom_point(aes(frame = sn, ids = tm), size = 5)

p <- ggplotly(p)

animation_opts(p, frame = 1000, transition = 500, easing = "linear",
           redraw = TRUE, mode = "immediate")

However I have been unable to display this in a shiny app, running locally (not on a shiny server), with the same dataframe. Currently, my (simplified) shiny app is:

library(shiny)
library(ggplot2)
library(plotly)

shinyApp(
  shinyUI(
    fluidPage(
      plotOutput("HF.plot")
    )
  ),
  shinyServer(function(input, output, session) {

    load("HF.RData")

    output$HF.plot <- renderPlotly({
      p <- ggplot(dt1.HF, aes(x = tm, y = HF, colour = team)) +
        geom_point(aes(frame = sn, ids = tm), size = 5)
      p <- ggplotly(p)
      animation_opts(p, frame = 1000, transition = 500, easing = "linear",
                     redraw = TRUE, mode = "immediate")
    })
  })
)

Is there something I am missing or doing wrong ?

Upvotes: 0

Views: 2840

Answers (1)

JohnCoene
JohnCoene

Reputation: 2261

You need to use renderPlotly and plotlyOutput.

library(shiny)
library(plotly)

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

server <- function(input, output, session) {
    output$plot <- renderPlotly({
        ggiris <- qplot(Petal.Width, Sepal.Length, data = iris, color = Species)
        ggplotly(ggiris)
    })
}

shinyApp(ui, server)

Upvotes: 2

Related Questions