使用R

时间:2017-08-23 12:29:24

标签: r plotly

我想有几个下拉菜单,用于指定显示哪些数据。

使用闪亮我可以分别传递所有选定的条件值,然后根据它们的相互作用过滤数据集。

在以下示例中是否可以获得相同的功能而没有闪亮?

library(plotly)
means = c(0,1,10)
scales = c(1,5)

sample.size = 100

t.visible = rep(F,2*length(means)*length(scales))
t.buttons = list()

pl = plot_ly()

for(i in 1:length(means)){
  for(j in 1:length(scales)){

    tt.visible = (i==1)&(j==1)

    pl = pl %>% 
          add_trace(x=0:sample.size,y=c(0,cumsum(means[i]+scales[j]*rnorm(sample.size))),type='scatter',mode='lines',color='one', visible = tt.visible) %>%
          add_trace(x=0:sample.size,y=c(0,cumsum(means[i]+scales[j]*rt(sample.size,df=5))),type='scatter',mode='lines',color='two',visible = tt.visible)

    tt.visible = t.visible
    tt.visible[(i-1)*length(scales)*2+(j-1)*2+(1:2)] = T

    t.buttons[[(i-1)*length(scales)+j]] = list(
      method = 'update',
      args = list(list(visible = tt.visible),
                  list(title = paste0('mean = ',means[i],'; scale = ',scales[j]))),
      label = paste0('mean = ',means[i],'; scale = ',scales[j])
    )
  }
}

pl = pl %>% layout(
  title = paste0('mean = ',means[1],'; scale = ',scales[1]),
  xaxis = list(title='time'),
  yaxis = list(title='value'),
  updatemenus = list(list(active = 0,
                          buttons = t.buttons))
)

特别是,是否有一种方法可以有两个独立的(交互)按钮,一个用于表示向量,另一个用于比例向量?

1 个答案:

答案 0 :(得分:1)

您可以创建自己的下拉菜单,并使用一些JavaScript动态显示和隐藏跟踪。

  • 根据您的输入数组动态创建溺水菜单
  • 在两个菜单中添加eventlistener
  • 根据选择
  • 设置Plotly数据的visible

enter image description here

使用htmlwidgets时,包含Plotly图的div将作为参数传递(在此示例中为el)。数据可以在data属性中找到。

library(plotly)
library(htmlwidgets)

means = c(0,1,10)
scales = c(1,5)
sample.size = 100

pl = plot_ly()

for(i in 1:length(means)){
  for(j in 1:length(scales)){
    trace_name <- paste('means:', means[i], '; scale:', scales[j])
    pl = pl %>% 
      add_trace(x=0:sample.size,
                y=c(0,cumsum(means[i]+scales[j]*rnorm(sample.size))),
                type='scatter',
                mode='lines',
                color='one',
                mode='line',
                visible = (i==1)&(j==1),
                name = trace_name) %>%
      add_trace(x=0:sample.size,
                y=c(0,cumsum(means[i]+scales[j]*rt(sample.size,df=5))),
                type='scatter',
                mode='lines',
                color='two',
                visible = (i==1)&(j==1),
                name = trace_name)
  }
}

javascript <- "
var select_mean = document.createElement('select');
select_mean.id = 'mean';
var select_scale = document.createElement('select');
select_scale.id = 'scale';
el.append(document.createTextNode('Means'));
el.append(select_mean);
el.append(document.createTextNode('Scale'));
el.append(select_scale);
function showTraces() {
  var select_scale = document.getElementById('scale');
  var select_mean = document.getElementById('mean');
  var scale = select_scale.options[select_scale.selectedIndex].text;
  var mean = select_mean.options[select_mean.selectedIndex].text;
  var traceName = 'means: ' + mean + ' ; ' + 'scale: ' + scale;
  for (var i = 0; i < el.data.length; i += 1) {
    el.data[i].visible = el.data[i].name.indexOf(traceName) > -1
  }
  Plotly.redraw(el)
}
select_scale.addEventListener('change', function() {
    showTraces();
});
select_mean.addEventListener('change', function() {
    showTraces();
});
"
for(i in 1:length(means)){
  javascript <- paste(javascript, "
var option = document.createElement('option');
option.text = '", means[i], "';
select_mean.append(option);", sep='')
}

for(j in 1:length(scales)) {
  javascript <- paste(javascript, "
var option = document.createElement('option');
option.text = '", scales[j], "';
select_scale.append(option);", sep='')
}

w <- as_widget(pl)
w <- htmlwidgets::onRender(w, paste("function(el, x, data) {", javascript, "}"), data=list('a', 'b'))
htmlwidgets::saveWidget(w, "buttons.html")
w