首页 > 解决方案 > 根据绘图框值更新数据框表值

问题描述

time我有一个显示数据框的列和amount的情节图df

东风:

id   time                 amount    category
1    2018-03-22 15:22:00  78        g1        
2    2018-03-22 15:30:00  77        g1
...   
34   2018-03-23 01:00:00  82        k3 
...

该图是一个动画,因此该图逐步出现,如本例 ( https://plot.ly/r/cumulative-animations/#cumulative-lines-animation ) 中的Cumulative Lines Animation部分。我无法发布我的编码,但它看起来与此类似。

来自网站的编码:

library(plotly)

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)

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 = 100, 
    transition = 0, 
    redraw = FALSE
  ) %>%
  animation_slider(
    hide = T
    ) %>%
  animation_button(
    x = 1, xanchor = "right", y = 0, yanchor = "bottom"
  )

我的情节: 剧情动画

现在我想在绘图旁边显示一个动态表,该表显示我的数据框与当前绘制时间有关的值。

所需的输出(如上图所示):

id           34
amount       82
category     k3

此表输出应与绘图同时更新,始终显示当前时间的值(滑块的值),这是函数的frame属性plotly()

所以关键问题是我需要访问动画滑块的值,但函数没有 id,所以我不能调用它。

知道如何获得滑块值吗?或者是否有另一种方法可以将动画连接到表格以实现我的目标?

标签: rplotshinyplotly

解决方案


推荐阅读