代码之家  ›  专栏  ›  技术社区  ›  Robert Long

用ggplot和plotly在r中渲染动画情节

  •  0
  • Robert Long  · 技术社区  · 7 年前

    我有一个动画情节,通过一个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 回复  |  直到 7 年前
        1
  •  1
  •   JohnCoene    7 年前

    你需要使用 renderPlotly 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)
    
    推荐文章