在 RStudio 中更新涉及 NA 的烛台图表数据

问题描述

我正在尝试使用交互式 shiny / plotly 烛台图表中的更新/重新设计按钮在两组开盘/高/低/收盘 (OHCL) 值之间切换。我观察到一些奇怪的行为,可能与数据中的 NA 或 RStudio 更新交互式绘图的方式有关,我希望有人能够向我解释。

这些是模拟数据

set.seed(11357)
library(tidyverse)
library(shiny)
library(plotly)

n<-10
dd <- data_frame(Date = 1:n,o.a=sample(1:n,n),h.a=o.a+1,l.a=o.a-1,c.a=o.a+.5,o.b=sample((n+1):(2*n),h.b=o.b+1,l.b=o.b-1,c.b=o.b+.5)
dd <- apply(dd,2,function(x) {x[sample( c(1:n),floor(n/5))] <- NaN; x} ) %>% as.data.frame()

导致

   Date o.a h.a l.a  c.a o.b h.b l.b  c.b
1     1 NaN   6 NaN  NaN  16  17  15 16.5
2     2   3 NaN   2  3.5  15  16  14  NaN
3     3   8   9 NaN  8.5  14  15  13 14.5
4     4   4   5   3  4.5  11  12  10 11.5
5     5   2   3   1  2.5  12  13  11  NaN
6     6   7   8   6  7.5  20  21 NaN 20.5
7   NaN NaN  11   9 10.5 NaN NaN NaN 13.5
8   NaN   9  10   8  9.5 NaN NaN  18 19.5
9     9   6   7   5  6.5  18  19  17 18.5
10   10   1 NaN   0  NaN  17  18  16 17.5

任何一组 OHLC 值(由后缀 .a.b 表示)都可以使用

绘制
col.a <- list(line = list(color = 'blue'))
col.b <- list(line = list(color = 'green'))
f <- function(x)layout(x,xaxis = list(rangeslider=list(visible=FALSE)))

fig.a <- plot_ly(type = 'candlestick',x = dd$Date,open = dd$o.a,high = dd$h.a,low = dd$l.a,close = dd$c.a,increasing = col.a) %>% f
fig.b <- plot_ly(type = 'candlestick',open = dd$o.b,high = dd$h.b,low = dd$l.b,close = dd$c.b,increasing = col.b) %>% f

subplot(fig.a,fig.b,shareY = TRUE)

这给

enter image description here

我使用以下代码实现了一个下拉菜单,以便在单个图中在 OHLC.a 和 OHLC.b 之间切换。

col.0 <- list(line = list(color = 'red'))
plot_ly(type = 'candlestick',x=dd$Date,increasing = col.0,opacity = .2) %>% 
  layout(updatemenus = list(
    list(
      buttons=list(
        list(
          label = 'a',active = 0,method = 'restyle',args = list(
            list(
              increasing.line.color = col.a$line$color,x = list(dd$Date),open = list(dd$o.a),high = list(dd$h.a),low = list(dd$l.a),close = list(dd$c.a)
              )
            )
          ),list(
          label = 'b',args = list(
            list(
              increasing.line.color = col.b$line$color,open = list(dd$o.b),high = list(dd$h.b),low = list(dd$l.b),close = list(dd$c.b)
              )
            )
          )
        )
      )
    )
    ) %>% f

但是,在执行此代码并按此顺序从菜单中选择 aba 后,我获得了以下令人费解的图序列:

enter image description here

enter image description here

enter image description here

enter image description here

如果我改为使用 plotly Download plot as a PNG 按钮保存图像,则结果如预期(相同顺序):

enter image description here

enter image description here

enter image description here

enter image description here

如何让 RStudio 显示正确的图,并在 Viewer 面板中显示“后图像”?

我已经尝试使用替代语法 dd %>% plot_ly(x=~Date,...)...,args = list(list(x = list(~Date) ...,它们产生了相同的结果。

更新:奇怪的是,当我在 RStudio 查看器中平移其中一个有问题的图时,不需要的额外蜡烛似乎在屏幕上保持固定位置,但相对于其他图表元素移动,表明它们确实不是情节的一部分,而是仍然开始显示的某种幻影残像......

Update2:可以通过添加第三个按钮来解决此问题,以在从 a 切换到b 之间完全删除烛台数据。

list(
      label = 'Reset',args = list(
        list(
          increasing.line.color = col.0$line$color,x = list(list()),open = list(list()),high = list(list()),low = list(list()),close = list(list())
        )
      )
    )

当然,最好直接切换。

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)