微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

在Shiny中使用绘图功能高亮时的多个画笔实例

如何解决在Shiny中使用绘图功能高亮时的多个画笔实例

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

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

下面是一个示例以及输出的图像。如何避免在闪亮的环境中出现多个Brush实例和SharedData标题的行为,而仅像示例here中那样维护一个Brush搜索框?

  library(shiny)
  library(plotly)

 ui <- fluidPage(
       selectInput(inputId="Var1",choices=names(iris),label="Variable1"),selectInput(inputId="Var2",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(),paste0(input$Var2),y2()
                                          ))
                      base %>% highlight(on = "plotly_selected",dynamic = TRUE,selectize = TRUE,opacityDim = 0.5,persistent = TRUE)
                                        })
        }

   shinyApp(ui,server)

Undesired Output

编辑:

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

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?

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。