2

This is a question following up on this answer by @klopetx. I have searched for how to update several data attributes in a single arg in Plotly in the official docs. It is possible in Python and thus also Javascript. For reproducibility, I have copied the code example 1.

For the example, I would like to make the args = list('y', list(df[, var_name])) also include the x. I imagined to this via the following:

args = list('y', list(df[, var_name]),'x', list(df[, var_name]))

Added to the code example, this would be:

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

1 Answer 1

2

The solution comes from this answer: just wrap the attributes in an additional list(). Of course this is straight forward but it was not intuitive for me.

Now the example works with the new arg line which allows for multiple attributes to now be added.

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
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.