1

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?

Ronak Shah
  • 377,200
  • 20
  • 156
  • 213
R noob
  • 495
  • 3
  • 20
  • Hi I don't know what your problem is. Please clarify. – KM_83 Oct 08 '20 at 23:01
  • the problem is that when I change values SelectInput widgets I get multiple Brush Color and SharedData instead of these features being displayed once Have you tried to run the code on shiny environment ? – R noob Oct 09 '20 at 05:04
  • It seems that it has to do with `selectize`.. I cross-posted this issue in https://community.plotly.com/t/in-shiny-app-highlight-function-with-selectize-create-multiple-shareddata-boxes/45870 – KM_83 Oct 09 '20 at 08:17
  • I guess, the problem is that each time you trigger `renderPlotly` (e.g. via your `selectInput`) a new plotly object and a new `SharedData` object is created. To avoid this you need to stop re-rendering your plot by e.g. using `plotlyProxy` which modifies the existing plot. – ismirsehregal Oct 09 '20 at 09:51
  • @ismirsehregal would you kindly show this solution by a simple example? – R noob Oct 10 '20 at 05:29
  • I don't have a specific answer you are looking for but I found this [talk](https://talks.cpsievert.me/20191115/#25) helpful in solving a similar brush issue I was having. I needed to add dragmode to the plot. – alittleloopy Aug 12 '21 at 23:24

0 Answers0