[R] Making R script to run in a console
Alexandre Aguiar
asaguiar at spsconsultoria.com
Tue Aug 15 00:14:52 CEST 2006
Em Seg 14 Ago 2006 17:58, Marc Schwartz (via MN) escreveu:
> http://kavaro.fi/mediawiki/index.php/Using_R_from_the_shell
I made a small change to the wrapper example by implementing dynamic
allocation of memory and sizing the command line buffer to 32768 that is the
real maximum size (at least under bash/sh :-).
#include <stdio.h> /* file i/o functions */
#include <string.h> /* string functions */
#include <stdlib.h> /* malloc and exit codes */
/*
* R_HOME has to be defined. Usually it is /usr/lib/R
* todo: copy all command line params to R
*/
int main(int argc, char **argv)
{
/* should be enough for all */
char *cmd, l;
FILE *out;
/* without this "if" a segmentation fault will happen
if no parameters are passed */
if(argc == 1)
return(EXIT_FAILURE);
/* dynamically allocates memory for command line buffer */
cmd=(char *)malloc(32768);
/* assemble command line */
strcpy(cmd,"/usr/lib/R/bin/exec/R --vanilla --slave < ");
strcat(cmd,argv[1]);
/* do the real job */
out=popen(cmd,"r");
l = fgetc(out);
printf("%c",l);
while(l != EOF)
{
l = fgetc(out);
if(l !=EOF)
printf("%c",l);
}
/* cleanup: close files and free memory */
pclose(out);
free((void *)cmd);
return(EXIT_SUCCESS);
}
--
Alexandre Aguiar
Independent consultant for medical research
SPS Consultoria
Voice: +55-11-9320-2046
Fax: +55-11-5549-8760
More information about the R-help
mailing list