From thomaskelderml at gmail.com  Thu Aug 31 17:33:26 2006
From: thomaskelderml at gmail.com (Thomas Kelder)
Date: Thu, 31 Aug 2006 17:33:26 +0200
Subject: [Rd] stop R mainloop without calling exit(1)
In-Reply-To: <903fbcd30608310831s4b323e4fya224e99961b97db9@mail.gmail.com>
References: <903fbcd30608310552w2f3d6350p72e7e52c0144cc4b@mail.gmail.com>
	<Pine.LNX.4.64.0608311406130.22477@gannet.stats.ox.ac.uk>
	<Pine.LNX.4.64.0608311429420.22954@gannet.stats.ox.ac.uk>
	<903fbcd30608310831s4b323e4fya224e99961b97db9@mail.gmail.com>
Message-ID: <903fbcd30608310833h7a06b4d8p682912f0041d1baa@mail.gmail.com>

Dear Professor Ripley,

Thank you for your reply. I have a few questions left:

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

In the documentation  "run_Rmainloop" is called to start the loop:

         Rf_initEmbeddedR(argc, argv);
         /* submit some code to R, which is done interactively via
             run_Rmainloop();
         */
         Rf_endEmbeddedR(0);

But that method doesn't return normally, right? It only ends if
ReplIteration returns -1.
Or do you mean I need to re-implement the read-and-evaluate loop in a
way that I can interrupt it (at "run a loop (which can return)" in
your example)? So instead of calling run_Rmainloop, I can copy the
loop in R_ReplConsole, add an interrupted flag and check that every
iteration.

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

> Another idea on Unix-alikes is to replace R_CleanUp by your own callback:
> it is part of the documented interface for writing a front-end.

So for the Unix alikes I could use a customized R_CleanUp for the
non-devel versions of R. For windows I should still call
end_Rmainloop, but if I understand you well, this is only needed if I
use the windows widgets in R. I don't want to, so it shouldn't be a
problem then?

Thank you for your help, I'm going to try to modify the JRI interface
by writing a custom loop instead of calling run_Rmainloop.

By the way, I use R version 2.2.1 in linux, because I got the "Error:
C stack usage is too close to the limit R" error in higher versions (I
saw there was a topic about this already:
https://stat.ethz.ch/pipermail/r-devel/2006-April/037219.html). I
tried to use the R-devel version to make use of the Rf_endEmbeddedR
method, but the error also occurs in this version. Could this be a
bug?

Thomas

2006/8/31, Prof Brian Ripley <ripley at stats.ox.ac.uk>:
> Another idea on Unix-alikes is to replace R_CleanUp by your own callback:
> it is part of the documented interface for writing a front-end.
>
> On Thu, 31 Aug 2006, Prof Brian Ripley wrote:
>
> > 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)
Thomas Kelder