[R] Newbie help with calling R from C programs

Tom Blackwell tblackw at umich.edu
Sun Feb 8 15:09:35 CET 2004


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:

> If all you want R to do is the graphics, you might as well use gnuplot for
> that, IMHO.
>
> Andy
>
> > -----Original Message-----
> > From: r-help-bounces at stat.math.ethz.ch
> > [mailto:r-help-bounces at stat.math.ethz.ch] On Behalf Of Marko Krco
> > Sent: Saturday, February 07, 2004 11:45 AM
> > To: r-help at stat.math.ethz.ch
> > Subject: [R] Newbie help with calling R from C programs
> >
> >
> > Hi,
> >
> > I'm a new user of R (since yesterday) and I'd like to see if
> > I can use it
> > in my codes. I often run large simulations which can take
> > hours or days to
> > complete and I need a way of viewing how my data is evolving while the
> > simulation is running.
> >
> > I figure I can output my temporary data into a file and then use the
> > system command in C to execute R with some command line
> > parameters. Now is
> > there a way that I can order R to load in the data file, set
> > some plotting
> > parameters, create a plot and exit? I'd like to be able to do
> > it in such a
> > way that the only visible output is the plot display window
> > and no input
> > is required of the user.
> >
> > Any advice would be appreciated, or if you know of someplace
> > this question
> > has been adressed before.
> >
> > Thank you,
> > Marko Krco
> >
> > P.S. I'll be doing this in Linux
> >
> > ______________________________________________
> > R-help at stat.math.ethz.ch mailing list
> > https://www.stat.math.ethz.ch/mailman/listinfo/r-help
> > PLEASE do read the posting guide!
> > http://www.R-project.org/posting-guide.html
> >
> >
>
>
> ------------------------------------------------------------------------------
> Notice:  This e-mail message, together with any attachments,...{{dropped}}
>
> ______________________________________________
> R-help at stat.math.ethz.ch mailing list
> https://www.stat.math.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html
>




More information about the R-help mailing list