On Fri, 16 Jan 2009, Felix Andrews wrote:
list(...),
I am looking for a way to interrupt R from a callback: specifically,
to interrupt plotting (typically on a cairoDevice, but would be good
if it worked with other devices too). Of course, one can interrupt R
nicely from the console with Ctrl-C (or Esc in Rgui), but I need to do
it from a GUI. Callbacks run in a new thread, so obviously stop() etc
will not work. I tried to look into how Rgui does it...
https://svn.r-project.org/R/trunk/src/gnuwin32/psignal.c
Actually no, Rgui does not use that code (but Rterm does).
In detail this is OS-specific, but what the front-ends do is to set a flag
or send a signal to R: look e.g. at rterm.c:
static void my_onintr(int nSig)
{
UserBreak = 1;
PostThreadMessage(mainThreadId,0,0,0);
}
or onintr in errors.c (which is called when UserBreak = 1, from the ain
thread). In due course R_interrupts_pending gets set and then at an
appropriate spot R stops and unwinds the currrent evaluations.
The relevant Windows code is even in 'Writing R Extensions'.
So depending on your OS you can raise a signal or set a flag. You can raise
a signal on Windows too, so 'raise' is the most portable solution -- but
beware that threading models differ.