I have a Shiny app with plotly graphs that are modified via plotlyProxy() in response to user input. Right now the graph modification is instantaneous and abrupt, so I'm trying to use plotly's animation frames to code for smooth changes.
For example, some reproducible code:
# reproducible code for stack overflow
library(plotly)
library(tidyverse)
lvls <- c("lv1", "lv2", "lv3", "lv4")
dat <- data.frame(var1 = sample(lvls, 300, replace = T))
ui <- fluidPage(
plotlyOutput("plot")
)
server <- function(input, output, session) {
output$info <- renderPrint(event_data("plotly_click"))
output$plot <- renderPlotly({
p <- plot_ly(dat, x = ~var1) %>%
add_histogram()
p
})
observeEvent(event_data("plotly_click"),
{
click <- event_data("plotly_click")
level <- click$x
opacity <- lvls %>%
as_tibble() %>%
mutate(opacity = ifelse(value == level, 1, .15)) %>%
.$opacity
plotlyProxy("plot", session) %>%
plotlyProxyInvoke("restyle",
list(marker.opacity = list(opacity)))
})
}
shinyApp(ui = ui, server = server)
When you run this app and click on each of the bars, the selected bar is highlighted without re-rendering the plot thanks to plotlyProxy(). How can I make the highlighting transition smooth with plotly's animation frames?