Shiny 在中使用plotly函数highlight()时有多个笔刷实例

Shiny 在中使用plotly函数highlight()时有多个笔刷实例,shiny,r-plotly,crosstalk,Shiny,R Plotly,Crosstalk,我有一个闪亮的应用程序,它允许在结果图中使用反应变量和k簇对数据进行聚类,并通过搜索或高亮显示进一步高亮显示图中的某些标记 每当我改变变量或K簇,就会在一个闪亮的应用程序中产生更多的笔刷实例 下面是一个示例以及输出的图像。如何避免在闪亮的环境中出现多个笔刷实例和SharedData标题的这种行为,只需像示例中那样维护一个笔刷和搜索框 库(闪亮) 图书馆(绘本) 嗨,我不知道你有什么问题。请澄清。问题是,当我更改值SelectInput widgets时,我会得到多个笔刷颜色和共享数据,而不是在您

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

每当我改变变量或K簇,就会在一个闪亮的应用程序中产生更多的笔刷实例

下面是一个示例以及输出的图像。如何避免在闪亮的环境中出现多个笔刷实例和SharedData标题的这种行为,只需像示例中那样维护一个笔刷和搜索框

库(闪亮)
图书馆(绘本)

嗨,我不知道你有什么问题。请澄清。问题是,当我更改值SelectInput widgets时,我会得到多个笔刷颜色和共享数据,而不是在您尝试在闪亮的环境中运行代码后显示的这些功能。这似乎与
选择
有关。。我在《我想》中交叉发布了这个问题,问题是每次触发
renderplly
(例如通过
selectInput
)时,都会创建一个新的plotly对象和一个新的
SharedData
对象。为了避免这种情况,您需要停止重新渲染绘图,例如使用修改现有绘图的
plotlyProxy
。@ismirsehregal请您通过一个简单的示例演示此解决方案。您好,我不知道您的问题是什么。请澄清。问题是,当我更改值SelectInput widgets时,我会得到多个笔刷颜色和共享数据,而不是在您尝试在闪亮的环境中运行代码后显示的这些功能。这似乎与
选择
有关。。我在《我想》中交叉发布了这个问题,问题是每次触发
renderplly
(例如通过
selectInput
)时,都会创建一个新的plotly对象和一个新的
SharedData
对象。为了避免这种情况,您需要停止重新渲染绘图,例如使用修改现有绘图的
plotlyProxy
。@ismirsehregal您能用一个简单的例子来说明这种解决方案吗?
  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)