Skip to content

Newbie help with calling R from C programs

4 messages · Liaw, Andy, Tom Blackwell

#
If all you want R to do is the graphics, you might as well use gnuplot for
that, IMHO.

Andy
------------------------------------------------------------------------------
Notice:  This e-mail message, together with any attachments,...{{dropped}}
#
Marko  -

Here's how I would do it.  Your simulation code writes (or overwrites)
its summary to a named file.  Then your simulation code does a system
call (somehow) that creates a subshell with a Bourne or csh command
line and sends the command "R --vanilla < script.file &".  (I'm not
quite sure at the moment how I would reach this shell again to
terminate both R and the shell.  You might have to kill it by process
number from outside the whole operation.)

*All* of the R-specific stuff is in 'script.file'.  This is an ascii
file containing exactly the R commands you would type interactively
at the R command line to get the display you want, line by line in the
file, and NOT including 'q()'.  Here's a real simple one:

tmp <- read.table("named.file")

X11()
par(lab=c(24,12,7), las=1, tcl=-0.35, mgp=c(1.5,0.5,0), pch=15)
plot(tmp$X1, tmp$X2, type="l", log="y")

It can have comments, blank lines, etc. in it -- anything you would
type at R's command line.  It has the file name for the named file
wired into it.

So the R process starts, reads data from the named file, and does
whatever you tell it to.  The R process has to stay running as long
as the plot window is displayed.  (How would you reach the window
process to kill it otherwise ?)  As soon as R has read the data
from "named.file", R has the data and "named.file" can disappear
or be overwritten without affecting R.

You're on your own to figure out R's interactions with the X window
manager if you want to try to do something fancy like specify the
geometry of the R command window, or minimize it or whatever.

But that's how I would do it.

Why this way ?  It's modular.  Easy to alter, experiment with and
improve.  Your simulation code needs to know only two file names,
and the format of the summary data it will write to "named.file".
It doesn't know anything about the contents of "script.file", so
you can change those at will without continually re-compiling your
simulation code.  In contrast to Andy, I think it's a *good*
solution.

-  tom blackwell  -  u michigan medical school  -  ann arbor  -
On Sat, 7 Feb 2004, Liaw, Andy wrote:

            
#
Marko  -

I can't resist.

The next more elegant solution would be to have a single R process
poll the directory entry for "named.file" periodically to see whether
the file date has changed since it was last read.  See help("file.info")
for an R tool that will help do this.  Use  'system("sleep 600", F)'
in R to space the polling ten minutes apart.

-  tom blackwell  -  u michigan medical school  -  ann arbor  -
#
Marko  -

  . . . and, using a single R process, rather than calling it
from inside the simulation code, means that the R process doesn't
need to be running on the same machine as the simulation code.
It only needs to run on a machine which has shared file access
with the simulation process.

And now, I REALLY have got to stop thinking about this and get
down to my own work !!

-  best  -  tom blackwell  -  umichigan medical school  -  ann arbor  -