【问题标题】:Rendering an animated plot in R shiny with ggplot and plotly使用 ggplot 和 plotly 在 R 中渲染动画情节
【发布时间】:2018-11-15 11:57:28
【问题描述】:

我有一个通过 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")
    })
  })
)

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

【问题讨论】:

  • renderPlotly 末尾返回p 是否有帮助?目前您只返回 animation_opts 的返回值
  • @thothal 谢谢,我试过了,但同样的问题仍然存在
  • 你能创建一个reprex吗?没有手头的数据很难说

标签: r ggplot2 shiny plotly r-plotly


【解决方案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)

【讨论】:

  • 太棒了。谢谢!
猜你喜欢
  • 2015-11-03
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-11-29
  • 2021-01-23
  • 2021-05-08
  • 2018-01-20
  • 2018-02-21
相关资源
最近更新 更多