Skip to content

Making R script to run in a console

4 messages · Ronaldo Reis-Jr., Marc Schwartz (via MN), Romain Francois +1 more

#
Hi,

is possible to make a R script to run under a console without open the R 
environment?

Something like this example.R

#!/usr/bin/R

function(name="Put here your name") {
print(name)
}

In a console I make
./example.R name="Ronaldo Reis J?nior"
then program print my name.

It is possible?

Thanks
Ronaldo
#
On Mon, 2006-08-14 at 14:12 -0300, Ronaldo Reis-Jr. wrote:
Ronaldo,

You might want to review these web pages:

http://wiki.r-project.org/rwiki/doku.php?id=developers:rinterp

http://kavaro.fi/mediawiki/index.php/Using_R_from_the_shell


HTH,

Marc Schwartz
#
Le 14.08.2006 19:12, Ronaldo Reis-Jr. a ?crit :
Hi,

take a look at that wiki page : 
http://wiki.r-project.org/rwiki/doku.php?id=developers:rinterp

Cheers,

Romain
#
Em Seg 14 Ago 2006 17:58, Marc Schwartz (via MN) escreveu:
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);
}