Multiple Brush Instances when using plotly function highlight () in Shiny

623 Views Asked by At

I have a Shiny app that allows clustering of data using reactive variables and k clusters in the resulting plot and further highlight certain markers in the plot by search or highlight.

Whenever I change the variables or K clusters it results in more instances of Brush in a shiny app.

An example is as below as well as an image of the output . How to avoid this behavior of multiple Brush instances and the SharedData titles in the shiny environment and just maintain one Brush and search box as in the examples here?

  library(shiny)
  library(plotly)

 ui <- fluidPage(
       selectInput(inputId="Var1",choices=names(iris),label="Variable1"),  
       selectInput(inputId="Var2",choices=names(iris),label="Variable2"),  
       sliderInput(inputId = "sliderClust",  label = "Number of Clusters:",
                    1, min = 1, max = 3, step = 1),  
        plotlyOutput(outputId = "ClustGraph")
      )




  server <- function(input, output, session) { 

             K_Clust <- reactive({
                        selectedData <- iris[, c( input$Var1, input$Var2)]
                     kmeans(na.omit(selectedData), centers = input$sliderClust, iter.max = 10)
                   })


            x2 <- reactive({
                      selectedData <- iris 
                      selectedData[,input$Var1]
                     })

            y2 <- reactive({
                     selectedData <- iris 
                     selectedData [,input$Var2]
                     })

            output$ClustGraph <-renderPlotly({
                             req(input$Var1)
                             req(input$Var2)  

                           selectedData <- iris   
                           selectedData$Group <- as.factor(K_Clust()$cluster)

                           key <- highlight_key(selectedData, ~Species) 

               base <- plot_ly(key, x = x2(), y = y2(), 
                          type = 'scatter', mode = 'markers', color = selectedData$Group,
                           hoverinfo = 'text',
                                 text = ~ paste(
                                                 Species,
                                               '<br>',paste0(input$Var1),':',
                                                 x2(),
                                               '<br>',paste0(input$Var2),':',
                                                 y2()
                                          ))
                      base %>% highlight(on = "plotly_selected", dynamic = TRUE, selectize = TRUE,
                               opacityDim = 0.5, persistent = TRUE)
                                        })
        }

   shinyApp(ui, server)

Undesired Output

EDIT:

After further research on the issue I have come across these links but none offers a solution of maintaining one Brush instance

multiple selectize/dynamic color brushes rendered in shiny

R Shiny reactive selectize highlight input in a plotly plot

Why does shiny App add a spurious widget to a Plotly graph using highlight function and selectize=TRUE?

0

There are 0 best solutions below