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

______________________________________________
[email protected] mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.

Reply via email to