# [Rd] Ctrl-C with R.exe and Rterm.exe in R v1.9.0

Duncan Murdoch dmurdoch at pair.com
Thu May 6 16:16:03 CEST 2004

On Wed, 5 May 2004 12:57:29 +0200, "Henrik Bengtsson"
<hb at maths.lth.se> wrote :

>Ctrl-C works as expected. Exiting R and you're back at a "clean"
>prompt. Now, try the same thing with R.exe (from a restarted prompt):
>
>C:\Program Files\R>rw1090\bin\R.exe --vanilla --quiet
>> ^C
>> ^C
>> 1+1
>[1] 2
>> Sys.sleep(100)
>^C
>C:\Program Files\R>
>>
>
>Strange things happen. R seems still to be running but has at the same
>time returned(?) the control back to the Command prompt.

Yes, that is indeed strange behaviour.

Ctrl-C definitely presents problems.  For instance, on my WinXP system
running R from a Cygwin prompt, a Ctrl-C during the Sys.sleep kills
Rterm, whereas under the Windows cmd.exe prompt it works properly as
it did for you.

I can guess at an explanation for what you saw, but I don't know how
to fix it:

- R (as opposed to Rterm) is a small .exe program that does a little
argument parsing, then runs Rterm.  It looks as though the Ctrl-C is
going to R, and killing that process, but leaving the child process
Rterm running.  But since both Rterm and cmd.exe are reading and
writing to standard file handles, you get the messed up prompts.

I've just tried doing what the Windows documentation says should work
(adding this line before Rterm is called:

SetConsoleCtrlHandler(NULL, TRUE);  /* Ignore Ctrl-C; Rterm will
handle them */

but this didn't fix the problems, it just made them different (i.e.
Ctrl-C stopped working completely).  Rterm *does* set a Ctrl-C
handler, but it just isn't being called, as far as I can tell.

A simple workaround is to run Rterm or Rgui rather than R.  If anyone
knows a real fix, please let me know.

Duncan Murdoch