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

Eck*_*ard 7 r candlestick-chart shiny plotly

我正在尝试使用交互式shiny/plotly烛台图表中的更新/重新设计按钮在两组开盘/高/低/收盘 (OHCL) 值之间切换。我观察到一些奇怪的行为,可能与NA数据中的 s 或 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), 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()
Run Code Online (Sandbox Code Playgroud)

导致

   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
Run Code Online (Sandbox Code Playgroud)

任何一组 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'
                 , x = dd$Date, 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)
Run Code Online (Sandbox Code Playgroud)

这使 在此处输入图片说明

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

col.0 <- list(line = list(color = 'red'))
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.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'
          , method = 'restyle'
          , args = list(
            list(
              increasing.line.color = col.b$line$color
              , x = list(dd$Date)
              , open = list(dd$o.b)
              , high = list(dd$h.b)
              , low = list(dd$l.b)
              , close = list(dd$c.b)
              )
            )
          )
        )
      )
    )
    ) %>% f
Run Code Online (Sandbox Code Playgroud)

但是,执行此代码,并选择在这个命令后,ab,并a从菜单中,我获得以下,百思不得其解,地块的序列:

在此处输入图片说明 在此处输入图片说明 在此处输入图片说明 在此处输入图片说明

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

在此处输入图片说明 在此处输入图片说明 在此处输入图片说明 在此处输入图片说明

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

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

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

更新 2:可以通过包含第三个按钮来解决此问题,以在从a到之间完全删除烛台数据b

list(
      label = 'Reset'
      , method = 'restyle'
      , 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())
        )
      )
    )
Run Code Online (Sandbox Code Playgroud)

当然,直接切换会更可取。