Is it possible to relayout plotly shapes in shiny with plotly proxy?

Hi everyone, I want to manipulate a vertical shape with plotly proxies but my code doesn't seem to respond. I was able to do that when I included the trigger inside renderPlotly but I want to restyle my bars at the same time and it doesn't seem to go well.

I made a reprex:

library(plotly)
library(magrittr)
library(shiny)

set.seed(42)
dat <- data.frame(x = 1:10 - 0.5, y = sample(1:10))

ui <- fluidPage(
  plotlyOutput("bars"),
  sliderInput("slider", "Pick a Number:", min = 1, max = 10, value = 30)
)
server <- function(input, output, session) {
  output$bars <- renderPlotly({
    g <- ggplotly(
      ggplot(dat, aes(
        x = x, y = y 
      )) +
        geom_col() +
        theme_classic()
    )

    g[["x"]][["layout"]][["shapes"]] <- c()
    names(g[["x"]][["attrs"]])


    g %>%
      layout(
        shapes = list(
          type = "line",
          line = list(color = "gray", dash = "dot"),
          x0 = 5,
          x1 = 5,
          y0 = 0,
          y1 = 3,
          yref = "paper"
        )
      ) %>%
      config(edits = list(shapePosition = TRUE))
  })
  
  
  observeEvent(input$slider, {
    
    col_vec <- rep("green", 10)
    if (input$slider <= 10) {
      
      col_vec[input$slider:10] <- "red"
      
    }

    plotlyProxy("bars", session) %>%
      plotlyProxyInvoke("restyle", "marker.color", list(col_vec))  %>%
    plotlyProxyInvoke("relayout", list(shapes = list(
      type = "line",
      line = list(color = "gray", dash = "dot"),
      x0 = input$slider,
      x1 = input$slider,
      y0 = 0,
      y1 = 3,
      yref = "paper"
    )))    
    
    print(input$slider)

  })

}

shinyApp(ui = ui, server = server)

Any ideas?

This topic was automatically closed 54 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.