[R] Double buffering plots on Windows

Bickis, Mikelis b|ck|@ @end|ng |rom m@th@u@@@k@c@
Fri Mar 22 18:43:08 CET 2024


Hello:

I want to present a sequence of plots as an animation.   As a toy example consider the code

function(n){for (i in 1:n){
plot(1:100,sin(i*(1:100)),type="l")
title(paste("n=",i))
segments(0,0,100,0,col=2)
}}

This sort-of works on a MacOS platform, but the rendering of the plots is a bit choppy.  Inserting a sleep function allows the plots to evolve smoothly.

function(n){for (i in 1:n){
plot(1:100,sin(i*(1:100)),type="l")
title(paste("n=",i))
segments(0,0,100,0,col=2)
Sys.sleep(.2)
}}

However, on a Windows platform, only the last plot is rendered without the Sys.sleep, so the dynamic element is lost.   Inserting the Sys.sleep does allow all the plots to be rendered, but they seem to be erased before they are drawn again, so there is substantial flicker in the appearance.

Is there some kind of double-buffering available within R, so that plots are rendered only after they are fully drawn, leaving the previous plot visible until it is replaced?   I just used the default graphics driver on Windows — is there perhaps a different driver that will the graphics smoother?

Mik Bickis
Professor Emeritus
Department of Mathematics and Statistics
University of Saskatchewan


More information about the R-help mailing list