使用 ggplot 和 plotly 在 R 中渲染动画图

Rendering an animated plot in R shiny with ggplot and plotly

我有一个动画图,可以通过 R(不是闪亮的应用程序)脚本正确显示:

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")

但是我一直无法在闪亮的应用程序中显示它,运行 在本地(不是在闪亮的服务器上),具有相同的数据帧。目前,我的(简化的)闪亮应用是:

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")
    })
  })
)

我有什么遗漏或做错了什么吗?

您需要使用 renderPlotlyplotlyOutput

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)