From ripley at stats.ox.ac.uk  Fri Jan 16 08:07:25 2009
From: ripley at stats.ox.ac.uk (Prof Brian Ripley)
Date: Fri, 16 Jan 2009 07:07:25 +0000 (GMT)
Subject: [Rd] interrupting R from a GUI
In-Reply-To: <94730b8a0901152207n4499c4e3n4352840334f025eb@mail.gmail.com>
References: <94730b8a0901152207n4499c4e3n4352840334f025eb@mail.gmail.com>
Message-ID: <alpine.OSX.1.00.0901160612480.65436@tystie.local>

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).

> ?...but it is beyond me.

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.

> Alternatively, might there be a way to tell the cairoDevice widget to
> stop drawing, somehow?

Why not ask its author?

> Any help would be appreciated.
>
> -Felix
>
> --
> Felix Andrews / ???
> http://www.neurofractal.org/felix/

[Those seeking technical advice will increase their chances of help by 
giving their affiliation(s) or at least explaining the purpose of the 
request so that potential helpers know it is non-commercial.  You may 
not get the benefit of the doubt next time.]

-- 
Brian D. Ripley,                  ripley at stats.ox.ac.uk
Professor of Applied Statistics,  http://www.stats.ox.ac.uk/?ripley/
University of Oxford,             Tel:  +44 1865 272861 (self)
1 South Parks Road,                     +44 1865 272866 (PA)
Oxford OX1 3TG, UK                Fax:  +44 1865 272595


[Rd] interrupting R from a GUI
Prof Brian Ripley