Grid: flickering graphics

I am designing an interactive plot using the grid package in R. As part of the interactivity, I repeatedly delete and recreate various parts of the plot. However, the total number of grid elements (obtained using the grid.ls() command) remains constant; everything I create has been previously deleted.

The problem is this: after I go through several creation and deletion cycles, each deletion that I do on the chart, however small, causes all the interactive parts of the plot (the ones that I deleted and the creation more than once) to flicker.

Here is the simplest example I could come up with - run this code first to set up the grid graphics, and re-delete and recreate certain elements

 library(grid) pushViewport(viewport()) for (x in seq(0, 1, length=5)) { for (y in seq(0, 1, length=5)) { pushViewport(viewport(x = x, y = y, width=1/5, height=1/5, name=paste("foo", x, y, sep=""))) grid.rect() pushViewport(viewport(x = 0, 0, width=1/4, height=1/4, name="bar1")) grid.circle(name="testing") grid.text("123") upViewport() pushViewport(viewport(x = 1, 0, width=1/4, height=1/4, name="bar2")) grid.circle(name="testing") grid.text("123") upViewport() pushViewport(viewport(x = 0, 1, width=1/4, height=1/4, name="bar3")) grid.circle(name="testing") grid.text("123") upViewport() pushViewport(viewport(x = 1, 1, width=1/4, height=1/4, name="bar4")) grid.circle(name="testing") grid.text("123") upViewport() upViewport() } } for (i in 1:10) { grid.gremove("testing") for (x in seq(0, 1, length=5)) { for (y in seq(0, 1, length=5)) { downViewport(paste("foo", x, y, sep="")) downViewport("bar1"); grid.circle(name="testing"); upViewport() downViewport("bar2"); grid.circle(name="testing"); upViewport() downViewport("bar3"); grid.circle(name="testing"); upViewport() downViewport("bar4"); grid.circle(name="testing"); upViewport() upViewport() } } } 

Once everything is set up, create a new arbitrary square on the device

 grid.rect(height=0.5, width=0.5, gp=gpar(lty = 2), name = "lastShape") 

Now try to remove it

 grid.gremove("lastShape") 

Please note that when you run this last delete command, all the small circles that I created and delete flicker slightly, although I did not touch them. This makes the whole drawing very distracting.

Any ideas how to prevent this?

Thank you, million!

+4
source share
1 answer

@hadley - you boss! Your first comment gave the correct answer; I am copying it and expanding it here for future reference ...

All you have to do is use

 dev.hold() # .... do scary modifications ... dev.flush() 

It seems to need to be processed. I will send a message again if it breaks again.

Note. It is available only in R v3 onwards ...

+4
source

Source: https://habr.com/ru/post/1493099/


All Articles