【问题标题】:Plotly: Update Several Data Attributes in an Argument with RPlotly:使用 R 更新参数中的多个数据属性
【发布时间】: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

【问题讨论】:

    标签: r r-plotly


    【解决方案1】:

    解决方案来自这个answer:只需将属性包装在一个额外的list() 中。当然这是直截了当的,但对我来说并不直观。

    现在该示例使用新的arg 行,现在允许添加多个属性。

    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 didn't work
            args = list(list(y = list(df[, var_name]),x = list(df[, var_name]))), # working code! 
            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
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2020-11-07
      • 2021-03-13
      • 1970-01-01
      • 2016-03-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多