Change values with buttons not working


#1

I’m not really sure why this isn’t working. I am working on a larger example and the graph is not updating, here is a small example that is similar.

a <- data.frame (x = c(1,2,3,4,5,6,7,8,9,10),
y = c(1,2,3,4,5,6,7,8,9,10))

b <- data.frame(x = c(6,7,8,9,10,11,12,13,14,15),
y = c(6,7,8,9,10,11,12,13,14,15))

updatemenus <- list(
list(
#active = -1,
type = “buttons”,
buttons = list(
list(
label = “A”,
method = “update”,
args = list(list(“visible”, c(TRUE, FALSE)),
list(title = “A”))
),
list(
label = “B”,
method = “update”,
args = list(list(“visible”, c(FALSE, TRUE)),
list(title = “B”))
)
)
)
)

plot_ly() %>%
add_trace(data = a,
x = ~x,
y = ~y,
type = “scatter”,
mode = “markers”,
name = “A”,
visible = TRUE) %>%
add_trace(data = b,
x = ~x,
y = ~y,
type = “scatter”,
mode = “markers”,
name = “B”,
visible = FALSE) %>%
layout(updatemenus = updatemenus)


#2

Hey @randalleg

There could be a few things here but perhaps consider (1) updating plotly:

devtools::install_github("ropensci/plotly")

(2) checking that the examples here work for you https://plot.ly/r/custom-buttons/

(3) Concerning the above, an immediate thing that jumps out is the args value. Consider using args = list(list(visible = c(T, F))


#3

Thanks for the reply. point number 3 did make the example function as I expected. Unfortunately the larger example does not but in a different manner. In the mwe above the button actually changes the axes so that when B is showing the x axis starts at 6, for A it starts at 1. In my larger code my first button is combining the traces for the x axis. So in this example A would show 1-15 instead of 1-10. Any ideas why the visible trace would combine the axis values?


#4

OK it seems to me that the difference is that the larger plot I’m working on has a categorical x axis. Is there a way to ONLY show the values for the visible traces? What I dont get though is that when I select the second button, THAT graph updates to not show all the categorical values but ONLY the ones for those traces.


#5

I realize that response might not be very clear. Here is an example that shows what I mean. By clicking on B you see x ticks with no values.

a <- data.frame (y = c(1,2,3,4,5),
x = c(“Tiger”, “Cat”, “Fish”, “Lion”, “Bird”))

b <- data.frame(y = c(6,7,8,9,10),
x = c(“Tiger”, “Cat”, “Rhino”, “Zebra”, “Pangolin”))

c <- data.frame (y = c(1,2,3,4,5),
x = c(“Tiger”, “Cat”, “Fish”, “Lion”, “Bird”))

d <- data.frame(y = c(6,7,8,9,10),
x = c(“Tiger”, “Cat”, “Rhino”, “Zebra”, “Pangolin”))

updatemenus <- list(
list(
active = -1,
type = “buttons”,
buttons = list(
list(
label = “A”,
method = “update”,
args = list(list(visible = c(TRUE, TRUE, FALSE, FALSE)),
list(title = “A”))
),
list(
label = “B”,
method = “update”,
args = list(list(visible = c(FALSE, FALSE, TRUE, TRUE)),
list(title = “B”))
)
)
)
)

plot_ly() %>%
add_trace(data = a,
x = ~x,
y = ~y,
type = “scatter”,
mode = “markers”,
name = “A”,
visible = TRUE) %>%
add_trace(data = c,
x = ~x,
y = ~y,
type = “scatter”,
mode = “markers”,
name = “B”,
visible = TRUE) %>%
add_trace(data = b,
x = ~x,
y = ~y,
type = “scatter”,
mode = “markers”,
name = “A”,
visible = FALSE) %>%
add_trace(data = d,
x = ~x,
y = ~y,
type = “scatter”,
mode = “markers”,
name = “B”,
visible = FALSE) %>%
layout(updatemenus = updatemenus)