Post

1 follower Follow
0
Avatar

Graph does not update until loop completion

I'm trying to monitor the status of a loop-until-convergence, and I can't seem to get it to update the graph each time it iterates.

Consider the following sample code:
print(plot(c(0,1)~c(0,100)))
for(i in seq(100)) {
Sys.sleep(.1)
print(points( runif(1)~i ))
}

The problem is that the graph doesn't update until after the loop finishes, no matter how long the loop takes. This is a huge problem for simulation-heavy model estimation, where graphs are often used to monitor the convergence loop.

Is there any way to force instant updates to the plots, rather than waiting? Note this is an RStudio-specific problem, as it works fine in base R.

Thanks!
Ari

Ian Pylvainen

5 comments

0
Avatar

Ari,

You can force an update to the plots pane by putting a Sys.sleep(0) into your loop.

Josh

Ian Pylvainen 0 votes
0
Avatar

Thanks Josh. That works, but only for the first few loop iterations. Tried it both on RStudio Server 0.96.331 (64-bit Ubuntu) and a local version of RStudio (64-bit Linux Mint).

Ian Pylvainen 0 votes
0
Avatar

Ari,

Not sure why this would be the case and I cannot reproduce this myself. This is really the only way to automatically refresh the graphics pane. You could try introducing a minor delay such as Sys.sleep(0.25) instead. Do you have a simple example which exhibits this problem. It'd be great if you could post is and I'll take a look.

Josh

Ian Pylvainen 0 votes
0
Avatar

I can't get the plot-a-few-then-stop behavior to repeat itself now, but the following code plots in real-time on my desktop and not at all on Server.

plot(c(0,1)~c(0,100))
for(i in seq(100)) {
Sys.sleep(.1)
points( runif(1)~i )
Sys.sleep(0)
}

I am apparently not the only one having this problem: http://stackoverflow.com/a/8813323/636656 . Although it sounds like his does eventually update, whereas mine does not.

I've tried adding many more Sys.sleep(0)'s in but to no avail. The situation is also not improved with 1000 points at 0.1 second delay per iteration vs. 100. If I lower it to ten points but with a 1 second delay per iteration, it graphs each point as the loop proceeds. Same is true for .5 second delay. At .25 second delay, it starts appearing blocky (a few will plot, then a few more, etc.).

The fact that plotting a lot of points quickly doesn't work entirely smoothly is not a problem for me. The problem is that when the loop runs fast, it never updates until the loop is complete. This is the 1000 points at 0.1 second example.

The only solution I've found is to not plot every loop iteration. Then it will update properly:
plot(c(0,1)~c(0,1000))
for(i in seq(1000)) {
Sys.sleep(.1)
if(i%%100==0) {
points( runif(1)~i )
Sys.sleep(0)
}
}

So in the end, I guess this is how I will do it and that solves my problem. Hopefully this thread helps someone having the same problem. Thanks for your time Josh.

Best,
Ari

Ian Pylvainen 0 votes
0
Avatar

Ari,

Okay thanks for the update and I was able to reproduce it as well (server only). Not sure what the lag is, but perhaps something we can look into. Glad you have a workaround for now.

Josh

Ian Pylvainen 0 votes