[R] Calling R in BATCH mode from C programm
alkatz@post.tau.ac.il
alkatz at post.tau.ac.il
Wed May 21 11:15:36 CEST 2003
Dear, Prof. Repley,
thank you for the help,
of course, you are right.
Redirection of stdin, stdout, stderr does the job.
If R script saves and reloads apprpriate workspaces,
then this method may be used instead of calling R functions from
C/C++ directly, wich is currently a problem, as I understand.
Here is C code that calls R in BATCH mode with arguments.
Then R runs script args.R wich reads the input arguments with Sys.getenv()
function and prints output to args.out and err.out files.
May be someone will find it usefull :
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
char emsg[] = "can't redirect %s to %s\n";
void main ()
{
int result, i;
char buf[5120];
if (!freopen( "args.R", "r", stdin )) {
fprintf( stderr, emsg, "stdin", "args.R" );
exit( 1 );
}
if (!freopen( "args.out", "w", stdout )) {
fprintf( stderr, emsg, "stdout", "args.out" );
exit( 1 );
}
if (!freopen( "err.out", "a", stderr )) {
fprintf( stderr, emsg, "stderr", "err.out" );
exit( 1 );
}
strcpy( buf, "Rterm --slave --no-save --no-restore ARG1=1 ARG2=2" );
if (result = system( buf ))
fprintf( stderr, "exit code = %d, errno = %d\n", result,
errno );
}
Quoting Prof Brian Ripley <ripley at stats.ox.ac.uk>:
> >From C, that is not how you redirect stdin, stdout (and you have
> forgotten
> to redirect stderr). I'll leave you to fathom that out (but as you are
> on
> Windows, the R sources should give you many useful clues). (Note that
> you
> do actually need a stdin and stdout to redirect, and a Windows
> application
> does not necessarily have them set up.)
>
>
> On Tue, 20 May 2003 alkatz at post.tau.ac.il wrote:
>
> > Hello R-people,
> >
> > I have the following problem :
> >
> > In order to run R script from DOS prompt in BATCH mode and pass it
> some
> > parameters I do the following :
> >
> >
> > Rterm --slave --no-save --no-restore <args.R> args.out ARG1=1 ARG2=2
>
> That is actually <args.R >args.out: whether your form works depends on
> the
> OS and shell. The effect is to redirect stdin/stdout and is done either
>
> by the shell or by the C initialization code.
>
>
> > It works fine :
> > the result is that the script args.R is isexecuted. Sys.getenv() sees
> the
> > arguments ARG1 and ARG2, and the R creates output file args.out
> >
> >
> > Now I want to be able to call the same command from C application :
> >
> >
> > #include <conio.h>
> > #include <process.h>
> > #include <string.h>
> >
> >
> > void main()
> > {
> > char *args[10], prog[80];
> > int ch;
> >
> > strcpy(prog, "C:\\Program Files\\R\\RBase\\bin\\Rterm.exe ");
> >
> > /* Arguments to Rterm.exe */
> > args[0] = "--slave";
> > args[1] = "--no-save";
> > args[2] = "--no-restore";
> > args[3] = "<args.R>";
> > args[4] = "args.out";
> > args[5] = "ARG1=1";
> > args[6] = "ARG2=2";
> > args[7] = NULL;
> >
> >
> > _execl( prog, args[0], args[1], args[2], args[3], args[4], args[5],
> args[6],
> > NULL);
> > }
> >
> >
> > Rterm starts, but writes to output :
> >
> > ARGUMENT '<args.R>' __ignored__
> > ARGUMENT 'args.out' __ignored__
> >
> >
> > Command Sys.getenv("ARG1") returns correct result => R sees the rest
> of the
> > arguments.
> >
> > I tryed to type args[3] = "args.R" - without <> it does not help.
> >
> > Does enyone know what might be my problem ??
> >
> > Thanks,
> > Alex.
> >
> > ______________________________________________
> > R-help at stat.math.ethz.ch mailing list
> > https://www.stat.math.ethz.ch/mailman/listinfo/r-help
> >
>
> --
> 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
>
More information about the R-help
mailing list