簡體   English   中英

使用ggplot和plotly在R Shiny中渲染動畫圖

[英]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)

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM