[R] break loop with keypress

William Simpson william.a.simpson at gmail.com
Wed Aug 6 11:01:40 CEST 2014

Thanks Greg.
I guess another option is to call a C function directly. On Windows I
see there is a function _kbhit() in conio.h. Not sure if it would be
that simple.
Write a .c file

#include <conio.h>

int main(void)
  int ch;
  ch= _kbhit();
  return ch;

Then do the necessary stuff to call that from R.


On 06/08/2014, Greg Snow <538280 at gmail.com> wrote:
> You could create a tcltk window that looks for a button click and/or
> key press and when that happens change the value of a variable.  Then
> in your loop you just look at the value of the same variable and break
> when the value changes.
> On Tue, Aug 5, 2014 at 6:13 AM, William Simpson
> <william.a.simpson at gmail.com> wrote:
>> 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
>> ______________________________________________
>> R-help at r-project.org mailing list
>> https://stat.ethz.ch/mailman/listinfo/r-help
>> PLEASE do read the posting guide
>> http://www.R-project.org/posting-guide.html
>> and provide commented, minimal, self-contained, reproducible code.
> --
> Gregory (Greg) L. Snow Ph.D.
> 538280 at gmail.com

More information about the R-help mailing list