2016-11-08 79 views
2

請運行示例代碼。R閃亮:使用ggplot2刷機功能的奇怪行爲

當我在散點圖中選擇點時,這些所選點將從圖表中刪除。它大部分工作正常,除了當我選擇靠近圖表角點的點時,這些點將在快速雙重自我更新後返回。

對於居住在圖表中間部分的點,它只能正常工作。

enter image description here

如何解釋這種奇怪的行爲?

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) 

看來,brush_1事件被觸發兩次,當選擇了那些奇怪的點積復位。

回答

3

當你在情節的限制取消了點,因爲它是那麼重繪以適應整個空間,而這會取消刷的問題發生...

您可以在情節設置固定的限制以防止它:

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))) 
+0

謝謝。這是完美的。 – John