我正在尝试使用闪亮的动作按钮来触发绘图的动画。 通过使用框架中的动画来完成动画。但是,这会创建一个自动播放按钮来触发动画。我不希望该按钮存在,而是希望使用我创建的闪亮的动作按钮来触发动画。
我尝试将plotlyProxy与plotlyProxyInvoke(“ animate”)函数一起使用。
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)
单击闪亮的动作按钮后,最终的情节动画应该是带有移动标记的静态正弦波。
答案 0 :(得分:1)
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)