r - 如何将闪亮的动作按钮链接到 R 中的情节动画?
问题描述
我正在尝试使用闪亮的操作按钮触发情节图的动画。动画是通过在情节中使用帧来完成的。但是,这会创建一个触发动画的自动播放按钮。我不希望这个按钮存在,相反,我想用我创建的闪亮动作按钮触发动画。
我尝试使用带有 plotlyProxyInvoke("animate") 函数的 plotlyProxy,但未成功。
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)
单击闪亮的动作按钮后,最终的绘图动画应该是带有移动标记的静态正弦波。
解决方案
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)
推荐阅读
- ruby-on-rails - 检测文件类型并调整图像大小
- javascript - 有没有人成功地使用 react-testing-library 来测试 DraftJS Editor 组件上的更改事件?
- javascript - 在 Canvas HTML getSeconds() 中,有没有办法以 56 秒 56 分 28 小时运行时钟?
- dask - 如何在每个工作人员上延迟 dask 以允许顺序执行进程?
- php - 从 firebase Php 获取上个月的数据
- postgresql - 在将数据插入 Postgresql 表时遇到一些麻烦
- c# - 如何使用 Dev Express Office File API 在现有文档上插入工作表
- javascript - I have a question on the part of jQuery ID Selector
- c - 存档的应用程序无法打开文本文件,从 Xcode 成功打开
- python - or.rename(src,dest) 在 python 中抛出文件不是错误