首页 > 解决方案 > 在 Shiny 中使用 plotly 函数 highlight () 时的多个画笔实例

问题描述

我有一个 Shiny 应用程序,它允许在结果图中使用反应变量和 k 个集群对数据进行聚类,并通过搜索或突出显示进一步突出显示图中的某些标记。

每当我更改变量或 K 簇时,都会在闪亮的应用程序中产生更多的 Brush 实例。

下面是一个示例以及输出的图像。如何避免在闪亮环境中出现多个 Brush 实例和 SharedData 标题的这种行为,并且只维护一个 Brush 和搜索框,如此处的示例所示

  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)

不需要的输出

编辑:

在对该问题进行进一步研究后,我遇到了这些链接,但没有一个提供维护一个 Brush 实例的解决方案

以闪亮呈现的多个选择/动态颜色画笔

R Shiny响应式选择绘图中的突出显示输入

为什么闪亮的应用程序使用突出显示功能和 selectize=TRUE 将虚假小部件添加到 Plotly 图中?

标签: rshinyr-plotlycrosstalk

解决方案


推荐阅读