# Change values with buttons not working

**URL:** <https://community.plotly.com/t/change-values-with-buttons-not-working/10848>\
**Category:** Plotly R\
**Created:** [June 11, 2018, 7:12pm UTC](https://community.plotly.com/t/change-values-with-buttons-not-working/10848 "2018-06-11T19:12:08Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![randalleg](https://avatars.discourse-cdn.com/v4/letter/r/c6cbf5/32.png) [@randalleg](https://community.plotly.com/u/randalleg)\
**Post date:** [June 11, 2018, 7:12pm UTC](https://community.plotly.com/t/change-values-with-buttons-not-working/10848/1 "2018-06-11T19:12:08Z")

</div>

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)

---

<div class="post-metadata">

**Author:** ![bcd](https://avatars.discourse-cdn.com/v4/letter/b/ec9cab/32.png) [@bcd](https://community.plotly.com/u/bcd)\
**Post date:** [June 12, 2018, 7:42am UTC](https://community.plotly.com/t/change-values-with-buttons-not-working/10848/2 "2018-06-12T07:42:01Z")

</div>

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/](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))`

---

<div class="post-metadata">

**Author:** ![randalleg](https://avatars.discourse-cdn.com/v4/letter/r/c6cbf5/32.png) [@randalleg](https://community.plotly.com/u/randalleg)\
**Post date:** [June 12, 2018, 1:14pm UTC](https://community.plotly.com/t/change-values-with-buttons-not-working/10848/3 "2018-06-12T13:14:29Z")

</div>

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?

---

<div class="post-metadata">

**Author:** ![randalleg](https://avatars.discourse-cdn.com/v4/letter/r/c6cbf5/32.png) [@randalleg](https://community.plotly.com/u/randalleg)\
**Post date:** [June 12, 2018, 1:30pm UTC](https://community.plotly.com/t/change-values-with-buttons-not-working/10848/4 "2018-06-12T13:30:05Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![randalleg](https://avatars.discourse-cdn.com/v4/letter/r/c6cbf5/32.png) [@randalleg](https://community.plotly.com/u/randalleg)\
**Post date:** [June 12, 2018, 2:33pm UTC](https://community.plotly.com/t/change-values-with-buttons-not-working/10848/5 "2018-06-12T14:33:44Z")

</div>

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)
