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

时间:2018-11-15 11:57:28

标签: r ggplot2 shiny plotly r-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")
    })
  })
)

我有什么想念的地方吗?

1 个答案:

答案 0 :(得分:1)

您需要使用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)