Skip to content

Event handling in R

2 messages · beetonn, Henrik Bengtsson

#
Dear R-helpers, 

I've just started playing with getGraphicsEvent() in R, and was wondering if there is a simple way to stop this function waiting for input after a pre-defined time, instead of relying either on a non-NULL value from one of the event handlers or for a user-interrupt for it to halt (as per the R manual).

The only way that I've thought of to make this work is using setTimeLimit to kill the function after a set time - here's a (messy) example:

setTimeLimit(cpu = 1)
plot(0, 0)
eventEnv <- getGraphicsEventEnv()
setGraphicsEventHandlers(prompt = 'Test')
while(TRUE) 
{
   err <- try(q <- getGraphicsEvent(), silent=TRUE)
   print(err)
}
# Not run
setTimeLimit(cpu = Inf)

but setTimeLimit doesn't seem to kill getGraphicsEvent() properly, as running this code just ends up with the repeated returned error of "Error in getGraphicsEvent(): recursive use of getGraphicsEvent not supported" instead of the intended "Error: reached CPU time limit" every second.

Is there a way to get around this problem, or a different method to make this work?  Or should I quit dabbling in things beyond my ken?


Thanks,
Nick
#
...because you're catching the timeout error with try() so it has not
effect, and hence the while(TRUE) {} loop keeps running.

Without knowing anything about graphics event handlers per se, here's
an alternative to catch the timeout event:

library("R.utils");

plot(0, 0);
eventEnv <- getGraphicsEventEnv();
setGraphicsEventHandlers(prompt='Test');

q <- NULL;
evalWithTimeout({
  q <- getGraphicsEvent();
}, timeout=1);
print(q);

does it.  See print(evalWithTimeout) to see what you are doing differently.

My $.02

/Henrik
On Thu, Dec 15, 2011 at 9:49 PM, beetonn <Nicholas.Beeton at utas.edu.au> wrote: