[R] break loop with keypress

William Simpson william.a.simpson at gmail.com
Tue Aug 5 14:13:38 CEST 2014


This works, but it is not quite what I need:

par(mar=rep(0,4))

while(1)
  {
  img1<-matrix(runif(2500),50,50)
  dev.hold(); image(img1,useRaster=TRUE); dev.flush()
  img2<-matrix(runif(2500),50,50)
  dev.hold(); image(img2,useRaster=TRUE); dev.flush()
  }

I would like to do this:
while(!kbhit())
  {
  ...

where kbhit() polls the keyboard, returning a non-zero integer if the
keyboard buffer has something in it. The animation loop continues
until a key is pressed.

All the ways of getting user input I have seen (e.g. getGraphicsEvent)
are not suitable because they would wait on each pass through the loop
until the key is pressed and therefore no animation would be
presented.

Any ideas on how to present a continuous animation loop which is
broken upon user input (keypress or mouse button press)? I am using
Windows 7. Thanks very much for any help.

Bill



More information about the R-help mailing list