R Flexdashboard双轨迹动画滑块因子顺序及共享数据问题
原Stack Overflow已有方案可解决Plotly中x轴与动画帧为同一因子时的滑块顺序混乱问题,但该方案在flexdashboard环境中结合SharedData使用时完全不适用。以下是两个典型异常场景:
- 注释
fixer修复函数后,带颜色编码的动画轨迹直接失效,无法正常渲染 - 无颜色编码的情况下,每个选中分组理应仅显示1个动画点,但实际会出现2个重复点
示例Rmd代码
--- title: "Flexdashboard + Plotly SharedData Animation Issue" output: flexdashboard::flex_dashboard --- ```{r setup, include=FALSE} library(flexdashboard) library(plotly) library(crosstalk)
Animation Problem
# 构造测试数据 df <- data.frame( group = rep(c("A", "B"), each = 3), frame = rep(c("1", "2", "3"), 2), x = rep(c("1", "2", "3"), 2), y = c(1, 2, 3, 2, 3, 4), color = rep(c("red", "blue"), each = 3) ) # 创建SharedData对象 sd <- SharedData$new(df, key = ~group, group = "group_select") # 原修复函数(注释后触发异常) # fixer <- function(p) { # p$x$layout$sliders[[1]]$steps <- lapply(p$x$layout$sliders[[1]]$steps, function(s) { # s$args[[1]]$frame <- df$frame[match(s$label, df$frame)] # s # }) # p # } # 绘制动画图 p <- plot_ly(sd) %>% add_markers( x = ~x, y = ~y, color = ~color, frame = ~frame, ids = ~group ) %>% animation_slider( currentvalue = list(prefix = "Frame: ") ) %>% animation_button( x = 1, xanchor = "right", y = 0, yanchor = "bottom" ) # 应用修复函数(注释后触发异常) # p <- fixer(p) p
内容的提问来源于stack exchange,提问作者user2602640
相关产品推荐
相关产品推荐

