【发布时间】:2017-08-04 13:08:54
【问题描述】:
简而言之:如何在 Shiny Web 应用程序的 UI.R 中完全加载时运行 Plotly 动画?
我正在尝试使用Plot.ly's cumulative animations 向我的 R Shiny Web 应用程序添加动画。我想在 UI 中加载时执行动画情节,但找不到自动运行情节的方法。
下面的 Shiny Web 应用程序的工作示例,其中包括一个 Plot.ly 累积动画,它在单击“播放”按钮时运行并且应该自动运行。
非常感谢您的帮助!
UI.R
pageWithSidebar(
sidebarPanel(
'some controls'
),
mainPanel(
plotlyOutput("frontPage", width = "100%")
)
)
服务器.R
library(shiny)
library(dplyr)
function(input, output, session) {
accumulate_by <- function(dat, var) {
var <- lazyeval::f_eval(var, dat)
lvls <- plotly:::getLevels(var)
dats <- lapply(seq_along(lvls), function(x) {
cbind(dat[var %in% lvls[seq(1, x)], ], frame = lvls[[x]])
})
dplyr::bind_rows(dats)
}
d <- txhousing %>%
filter(year > 2005, city %in% c("Abilene", "Bay Area")) %>%
accumulate_by(~date)
observe({
output$frontPage <- renderPlotly({
p <- d %>%
plot_ly(
x = ~date,
y = ~median,
split = ~city,
frame = ~frame,
type = 'scatter',
mode = 'lines',
line = list(simplyfy = F)
) %>%
layout(
xaxis = list(
title = "Date",
zeroline = F
),
yaxis = list(
title = "Median",
zeroline = F
)
) %>%
animation_opts(
frame = 10,
transition = 5,
redraw = FALSE
) %>%
animation_slider(
hide = T
) %>%
animation_button(
x = 1, xanchor = "right", y = 0, yanchor = "bottom"
)
})
})
}
【问题讨论】: