🚀 Gen 5 of the leading AI app deployment platform launches October 6. Click for the livestream.

Update both shapes and data not working in R

I am trying to update both shapes and data with plotly in R. The visibility of data and shapes should change with buttons. Unfortunately, it does not work.

Also, if restyle and relayout functions can be done by update, why do we even have these two methods?

Please help.

Here is a code I tried, which shows it does not work as expected:

data1 <- runif(100,0,1000)
data2 <- runif(5,0,1000)
data3 <- runif(100,0,1000)
data4 <- runif(500,0,1000)

p <- plot_ly() %>%
  add_trace(type = 'scatter', mode = 'markers',
            y = data1, visible=T, marker = list(color = 'blue'))  %>%
  add_trace(type = 'scatter', mode = 'markers',
            y = data2, visible=F, marker = list(color = 'red'))%>%
  add_trace(type = 'scatter', mode = 'markers',
            y = data3, visible=T, marker = list(color = 'gray'))  %>%
  add_trace(type = 'scatter', mode = 'markers',
            y = data4, visible=F, marker = list(color = 'black')) %>%
  layout(title ="Data12",
    updatemenus = list(
      list(type='buttons',

        buttons = list(
          list(method = "update",
               args = list(list(visible=c(T, F,T,F)),list(title 
          ="Data12"),list(shapes=c(type="rect",x0=0,x1=100, xref="x",
                                y0=0,y1=100, yref="y",fillcolor = "green", 
         line = list(color = "green")))),

               label = 'data12'),

          list(method = "update",
               args = list(list(visible=c(F,T,F,T)),list(title 
               ="Data34"),list(shapes=c(type="rect",x0=0,x1=100, xref="x",
                                           y0=0,y1=100, yref="y",fillcolor = 
               "blue", line = list(color = "blue")))),
               label = 'data34')
        ))))
p