如何将闪亮的动作按钮链接到R中的绘图动画? [英] How do I link a shiny action button to a plotly animation in R?

查看:139
本文介绍了如何将闪亮的动作按钮链接到R中的绘图动画?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用闪亮的动作按钮来触发绘图的动画. 动画是通过使用图框内的帧来完成的.但是,这将创建一个自动播放按钮来触发动画.我不希望该按钮存在,而是希望使用我创建的闪亮的动作按钮来触发动画.

I am trying to trigger an animation of a plotly graph with a shiny action button. The animation is done with the use of frames in plotly. However, this creates an automatic play button that triggers the animation. I don't want this button to exist and, instead, I want to trigger the animation with a shiny action button I created.

我尝试将plotlyProxy与plotlyProxyInvoke("animate")函数一起使用.

I have tried, unsuccessfully, using the plotlyProxy with the plotlyProxyInvoke("animate") function.

p <- plot_ly(sinusoid, x = ~time, y = ~sin, type = "scatter", mode = 'line',
colors = colorRampPalette(brewer.pal(5,"Spectral"))(50), hoverinfo = 'none',
name = "Cycle") %>%

add_markers(x = compUn$angleShift, y = compUn$sin, type = "scatter",
name = compUn$Country[i], showlegend = TRUE, marker = list(size = 12), 
frame = compUn$DateStringAdjusted, hoverinfo = 'text', 
text = paste0('D: ', round(compUn$D, 3), 
              '\nA: ', round(compUn$A, 3),
              '\nReturn: ', round(compUn$R, 3))) %>%

animation_opts(frame = 10000, redraw = FALSE)

单击闪亮的动作按钮后,最终的情节动画应为带有移动标记的静态正弦波.

The final plot animation should be a static sine wave with a moving marker, once the shiny action button is clicked.

推荐答案

library(shiny)
library(plotly)
library(htmlwidgets)

ui <- fluidPage(
  actionButton("anim", "Animate"),
  plotlyOutput("plot")
)

server <- function(input, output){
  output[["plot"]] <- renderPlotly({
    df <- data.frame(
      x = c(1,2,1), 
      y = c(1,2,1), 
      f = c(1,2,3)
    )
    df %>%
      plot_ly(
        x = ~x,
        y = ~y,
        frame = ~f,
        type = 'scatter',
        mode = 'markers',
        marker = list(size = 20),
        showlegend = FALSE
      ) %>% 
      animation_button(visible = FALSE) %>%
      onRender("
        function(el,x){
          $('#anim').on('click', function(){Plotly.animate(el);});
        }")

  })
}

shinyApp(ui, server)

这篇关于如何将闪亮的动作按钮链接到R中的绘图动画?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆