From ripley at stats.ox.ac.uk  Thu Aug 31 15:22:21 2006
From: ripley at stats.ox.ac.uk (Prof Brian Ripley)
Date: Thu, 31 Aug 2006 14:22:21 +0100 (BST)
Subject: [Rd] stop R mainloop without calling exit(1)
In-Reply-To: <903fbcd30608310552w2f3d6350p72e7e52c0144cc4b@mail.gmail.com>
References: <903fbcd30608310552w2f3d6350p72e7e52c0144cc4b@mail.gmail.com>
Message-ID: <Pine.LNX.4.64.0608311406130.22477@gannet.stats.ox.ac.uk>

This is already covered in the R-devel version of R: please consult
`Writing R Extensions' in that version.

On Thu, 31 Aug 2006, Thomas Kelder wrote:

> Hello,
> 
> I'm trying to make my Java application work with R, which involves
> sending and retrieving data and to run R functions from within the
> Java application. I also need to have "live interaction" with R, to
> show the R console output (e.g. warnings and print) and to enable the
> user to enter input when a function asks for it.
> 
> Therefore I created a simple R console in Java using JRI
> (http://rosuda.org/JRI/), and it works ok. The only problem is that I
> have to start the R main loop which doesn't return.

But that's not true.  The documented way is

setup_Rmainloop
run a loop (which can return)
end_Rmainloop

The problem was that the only documented way to shut R down cleanly was 
end_Rmainloop (which did call exit), whereas there now is Rf_endEmbeddedR.

> The only way to stop it is to give R the quit command ('q()'), but then 
> 'exit(1)' is called from the R code and the Java Virtual Machine 
> (including my application) also shuts down.

That's because you called end_Rmainloop, possibly by calling 
run_Rmainloop rather than taking control of the loop.

It seems that almost all users of embedded R did not call end_Rmainloop, 
and I met this when creating a common embedding interface for Unix and 
Windows versions of R: the Windows widgets (such as the graphics device) 
do need to be told to shut down.

[...]

-- 
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] stop R mainloop without calling exit(1)
Prof Brian Ripley