R Shinny:ggplot2笔刷功能的奇怪行为

R Shinny:ggplot2笔刷功能的奇怪行为,r,ggplot2,shiny,brush,R,Ggplot2,Shiny,Brush,请运行示例代码 在散点图中选择点时,这些选定点将从图表中删除。它基本上工作正常,只是当我选择一些靠近图表角落的点时,这些点会在快速的双重自我更新后返回 对于图表中间部分的点,它工作正常。 如何解释这种奇怪的行为 library(ggplot2) library(shiny) server <- function(input, output) { vals = reactiveValues(keeprows = TRUE) observeEvent(input$brush_1

请运行示例代码

在散点图中选择点时,这些选定点将从图表中删除。它基本上工作正常,只是当我选择一些靠近图表角落的点时,这些点会在快速的双重自我更新后返回

对于图表中间部分的点,它工作正常。

如何解释这种奇怪的行为

library(ggplot2)
library(shiny)

server <- function(input, output) {

  vals = reactiveValues(keeprows = TRUE)

  observeEvent(input$brush_1,{
    cat("---------------\n")
    print("brush_1")
    Res = brushedPoints(mtcars,brush = input$brush_1,allRows = TRUE)
    vals$keeprows = !Res$selected_    
  })

  observeEvent(input$brush_2,{
    cat("---------------\n")
    print("brush_2")
    Res = brushedPoints(mtcars,brush = input$brush_2,allRows = TRUE)
    vals$keeprows = !Res$selected_    
  })

  D = reactive({
    print("D")
    mtcars[vals$keeprows,]
  })

  output$p1 = renderPlot({
    print("plot_1")
    X = D()
    ggplot(X,aes(x=mpg,y=cyl))+geom_point()
  })
  output$p2 = renderPlot({
    print("plot_2")

    ggplot(D(),aes(x=mpg,y=wt))+geom_point()
  })

  output$L = renderPrint({
    Res = brushedPoints(mtcars,brush = input$brush_1,allRows = TRUE)
    Res
  })
}


ui <- fluidPage(
  splitLayout(plotOutput("p1",brush = "brush_1"),plotOutput("p2",brush = "brush_2"))
              ,
  verbatimTextOutput("L")
)


shinyApp(ui = ui, server = server)
库(ggplot2)
图书馆(闪亮)

服务器取消选择绘图边界处的点时会出现问题,因为随后会重新绘制以适应整个空间,这会取消笔刷的设置

您可以在绘图上设置修复限制以防止:

ggplot(X,aes(x=mpg,y=cyl))+
geom_point()+
scale_x_continuous(limits=c(min(mtcars$mpg),max(mtcars$mpg)))+
scale_y_continuous(limits=c(min(mtcars$cyl),max(mtcars$cyl)))

谢谢太完美了。