✊🏿 Black Lives Matter. Please consider donating to Black Girls Code today.
⚡️ Concerned about the grid? Kyle Baranko teaches how to predicting peak loads using XGBoost. Register for the August webinar!

In shiny app, highlight() function with selectize create multiple SharedData boxes

This is a cross-posting from stockoverflow Q.

How can one use highlight(..., selectize=TRUE) in shiny context without duplicating “SharedData” boxes as renderPlotly({}) is iterated?

Here is an example:

library(shiny)
library(plotly)
library(rlang)

ui <- fluidPage(
  selectInput("Var1","Var1",choices=names(iris)[-5], selected=names(iris)[1]),  
  selectInput("Var2","Var2",choices=names(iris)[-5], selected=names(iris)[2]),  
  plotlyOutput(outputId = "ClustGraph")
)


server <- function(input, output, session) { 
  
  output$ClustGraph <-renderPlotly({
    Var1 <- quo(!!input$Var1)
    Var1 <- ensym(Var1)
    Var2 <- quo(!!input$Var2)
    Var2 <- ensym(Var2)
    
    key <- highlight_key(iris, ~Species, group ="SharedData") 

    qplot(data=key, x=!!Var1, y=!!Var2, color=Species) %>%
      ggplotly() %>% 
      highlight(on = "plotly_selected", dynamic = TRUE,
                selectize = TRUE, selected = attrs_selected(showlegend = FALSE))

  })
}

shinyApp(ui, server)