'Why doesn't method = "update" work when using "transforms" to filter in R plotly
I'm fairly new to r plotly so I'm still getting accustomed to all the arguments. One topic that I'm exploring is how to create a dropdown menu event by filtering. I found this helpful example using the iris dataset found here https://community.plotly.com/t/need-help-on-using-dropdown-to-filter/6596/2, it's the same code I provided below with some slight changes. I was experimenting with how I can change the annotations and shapes for the "Setosa" and "Versicolor" histograms respectively while still being able to change the graphic in terms of the data. However, when I tried to use method = "update" in layout(), the annotations/shapes changed, but the data remained the same even when I toggled between "Setosa" and "Versicolor." Does anybody know why this happens, and how to fix it?
Here is the reproducible code. I want to be able to filter the histogram graphics that correspond to the flower species (to create a dropdown menu) while changing their annotations/shapes simultaneously.
library(plotly)
datasets::iris
line_shape <- list(type = "line",
x0 = 0,
x1 = 1,
xref = "paper",
y0 = 6,
y1 = 6,
line = list(color = "black"))
line_shape2 <- list(type = "line",
x0 = 0,
x1 = 1,
xref = "paper",
y0 = 6,
y1 = 6,
line = list(color = "red"))
line_annotation <- list( x = 5, y = 6.4, xref = "x", yref = "y",
text = "Sepal", showarrow = FALSE,
font = list(color = 'black'))
line_annotation2 <- list( x = 5, y = 6.4, xref = "x", yref = "y",
text = "Sepal", showarrow = FALSE,
font = list(color = 'red'))
p <- iris %>%
plot_ly(
type = 'histogram',
x = ~Sepal.Length,
transforms = list(
list(
type = 'filter',
target = ~Species,
operation = '=',
value = unique(iris$Species)[1]
)
)) %>% layout(
updatemenus = list(
list(
type = 'dropdown',
active = 0,
buttons = list(
list(method = "update",
args = list(list("transforms[0].value", unique(iris$Species)[1]),
list(shapes = list(line_shape, c()),
annotations = list(line_annotation, c()))),
label = unique(iris$Species)[1]),
list(method = "update",
args = list(list("transforms[0].value", unique(iris$Species)[2]),
list(shapes = list(c(), line_shape2),
annotations = list(c(), line_annotation2))),
label = unique(iris$Species)[2])
)
)
)
)
p
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
