Skip to content

[R-gui] Small annoyance while interacting with R

7 messages · Duncan Temple Lang, Thomas Friedrichsmeier, Philippe GROSJEAN +3 more

#
Hello,

I'm trying to write some program in Ruby to interact with the R console but it
doesn't work : in short, I can connect R standard input, output and error and
retrieve correct calculations for correct inputs, but as soon as R writes
something to the standard error, the program exits.

For example

for

stdin : "3 + 3;"
stdout : ">3+3\n" "6\n"

but for
stdin : "a;"
stdout : "Error : object not found\n" "Execution finished"

It doesn't say that when used with the normal shell.
The same program works perfectly with the simple calculator bc. Does anyone
knows of this particularity ?

Pablo
#
-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1


You don't tell us anything about your platform or how
you are creating the R session.
But yes, R is designed when running in non-interactive mode
to quit when there is an error.
There are different things you can do to get the desired
behaviour.  I suggest you send mail to r-devel as this
is not just an R-sig-gui question.

 D.
Pablo Winant wrote:
- --
Duncan Temple Lang                duncan at wald.ucdavis.edu
Department of Statistics          work:  (530) 752-4782
371 Kerr Hall                     fax:   (530) 752-7099
One Shields Ave.
University of California at Davis
Davis, CA 95616, USA
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.2 (Darwin)
Comment: Using GnuPG with Thunderbird - http://enigmail.mozdev.org

iD8DBQFDXP5U9p/Jzwa2QP4RAjqxAJ9zAXiOikVtyBc5ocqVD5lSpeUflgCeKB/M
LOGF46ZxUWCJdUYjDEelpdw=
=qEEu
-----END PGP SIGNATURE-----
#
Hi,
this is from memory, and may not be entirely correct, but roughly it's this:
If R notices it is not connected to a PTY, it goes into scripting/batch mode 
(i.e. interactive () == FALSE). This means, it will exit on any error. As far 
as I recall, this behavior cannot be changed, or at least not easily so.
A way to work around this is to embed R:
http://stat.ethz.ch/R-manual/R-devel/doc/manual/R-exts.html#Linking-GUIs-and-other-front_002dends-to-R
then set R_ConsoleFile = NULL, and override ptr_R_WriteConsole (Linux) or 
Rd->WriteConsole to point to your own function to handle output.
Several slightly different solutions are possible, but the key is to avoid 
having R go into batch mode.

Regards
Thomas Friedrichsmeier
#
Thomas Friedrichsmeier wrote:
Another way, if you want to simply redirect input and output is to run R 
in ESS mode (with --ess flag).
Best,

Philippe Grosjean
#
On 10/24/2005 11:36 AM, Thomas Friedrichsmeier wrote:
It can be easily changed (at least on some platforms), but the method is 
not obvious.  In Windows if Rterm.exe is started with command line 
option --ess it acts pretty much the way Pablo wants.  This is 
documented as a Windows-only option; I'm not sure what the method would 
be to achieve the same on other platforms.

Duncan Murdoch
#
Thank you all,

You understand my problem. I'm running a Linux terminal and unfortunately, the
option --ess does not exist on this plateform. (did you so Philippe for Tinn-R
?).
I have not fully understood if it's feasible to make R believe it is in
interactive mode. From the document about R extensions, I believe I could as a
workaround, talk to a C program embedding R. I must admit it is quite a
difficult task for me (I'm an economist) but I will give it a try.

Pablo

PS : my "program" in ruby under Linux was,

require 'open3'
rin,rout,rerr = Open3.popen3('R --vanilla --quiet')
rin.puts("2+4;")
rerr.gets # returns "2+4;\n" "6\n"

Quoting Duncan Murdoch <murdoch at stats.uwo.ca>:
#
Hi Pablo,
Yes, interfacing multiple programming languages can be difficult.  So have
you considered just writing all your code in one language?  Maybe you have
more expertise in Ruby than R, but maybe the time it would take to learn
to write all your code in R would be less than the time it would take to
learn how to interface R with other languages?

Just a thought,
James