【发布时间】:2021-11-20 15:45:59
【问题描述】:
这是@klopetx 对this answer 的后续问题。我在official docs 的 Plotly 中搜索了如何在单个 arg 中更新 几个 数据属性。它在 Python 中是可能的,因此在 Javascript 中也是可能的。为了重现性,我复制了代码示例1。
例如,我想让args = list('y', list(df[, var_name])) 也包含x。我通过以下方式想象到这一点:
args = list('y', list(df[, var_name]),'x', list(df[, var_name]))
添加到代码示例中,这将是:
library(plotly)
df <- data.frame(x = runif(200), y = runif(200), z = runif(200), j = runif(200), k = rep(0.7, 200), i = rnorm(200,0.6,0.05))
create_buttons <- function(df, y_axis_var_names) {
lapply(
y_axis_var_names,
FUN = function(var_name, df) {
button <- list(
method = 'restyle',
# args = list('y', list(df[, var_name])), #previous example for only one attribute
args = list('y', list(df[, var_name]),'x', list(df[, var_name])), #new code which doesn't work
label = sprintf('Show %s', var_name)
)
},
df
)
}
y_axis_var_names <- c('y', 'z', 'j', 'k', 'i')
p <- plot_ly(df, x = ~x, y = ~y, mode = "markers", name = "A", visible = T) %>%
layout(
title = "Drop down menus - Styling",
xaxis = list(domain = c(0.1, 1)),
yaxis = list(title = "y"),
updatemenus = list(
list(
y = 0.7,
buttons = create_buttons(df, y_axis_var_names)
)
))
p
【问题讨论】: