Hi,
I am considering if I should invest in learning R. Based on the
language definition and introductory documents, it seems nice. But now
I am faced with a problem: I want to be able to run R programs easily
from the unix shell, and write scripts that can automatically select R
as the interpreter:
#!/usr/bin/R
cat("Hello world.\n")
This of course doesn't work, because /usr/bin/R is a shell script.
I have been able to create a binary wrapper that calls R with the
correct arguments, which is documented here:
http://kavaro.fi/mediawiki/index.php/Using_R_from_the_shell
This still lacks eg. standard input (but I have no idea how I can
implement it in R) and full command line argument passing (can be
done), but am I on the right track, or is there already something that
does what I need?
juha
R as shell script
3 messages · Juha Vierinen, Jeffrey Horner
Juha Vierinen wrote:
Hi,
I am considering if I should invest in learning R. Based on the
language definition and introductory documents, it seems nice. But now
I am faced with a problem: I want to be able to run R programs easily
from the unix shell, and write scripts that can automatically select R
as the interpreter:
#!/usr/bin/R
cat("Hello world.\n")
This of course doesn't work, because /usr/bin/R is a shell script.
I have been able to create a binary wrapper that calls R with the
correct arguments, which is documented here:
http://kavaro.fi/mediawiki/index.php/Using_R_from_the_shell
This still lacks eg. standard input (but I have no idea how I can
implement it in R) and full command line argument passing (can be
done), but am I on the right track, or is there already something that
does what I need?
If you search the mailing list archives: http://tolstoy.newcastle.edu.au/R/ you can find entries like the following (searching on "bang"): http://tolstoy.newcastle.edu.au/R/devel/05/01/1910.html which implements similar functionality. But I thought this would be a fun little (distracting) project, so I created an R wiki page: http://wiki.r-project.org/rwiki/doku.php?id=developers:rinterp with source code describing how to solve this problem by embedding R within your C program. Here's the main function: /* rinterp: shebang support for R * * Usage: * * From the command line: * $ rinterp filename * * In a file: * -------------------- * #!/path/to/rinterp * cat("hello world\n"); * -------------------- */ int main(int argc, char **argv){ /* R embedded arguments */ char *R_argv[] = {"RINTERP", "--gui=none", "--slave", "--silent", "--vanilla","--no-readline"}; int R_argc = sizeof(R_argv)/sizeof(R_argv[0]); struct stat sbuf; /* Setenv R_HOME: insert or replace into environment. * The RHOME macro is defined during configure */ if (setenv("R_HOME",RHOME,1) != 0){ perror("ERROR: couldn't set/replace R_HOME"); exit(1); } /* Assume last argument is the file we want to source. * Ignore other args for now. */ if (argc > 1) {/* don't try and source rinterp binary */ if (stat(argv[argc-1],&sbuf) != 0){ perror(argv[argc-1]); exit(1); } } else { fprintf(stderr,"Usage: rinterp filename\n"); exit(1); } /* Initialize R interpreter */ Rf_initEmbeddedR(R_argc, R_argv); /* Now call R function source(filename) */ call_fun1str("source",argv[argc-1]); exit(0); } Feel free to logon to the R wiki and contribute to the page. I think it would be interesting to solve this once and for all. Cheers!
Jeffrey Horner Computer Systems Analyst School of Medicine 615-322-8606 Department of Biostatistics Vanderbilt University
3 days later
Hi Jeffrey!
Great work with rinterp! I had barely read into the R embedded API and
somebody implements exactly what I need. Thank you! I played around
with it a bit and I was able to access stdin() without any
modifications, I have added a stdin example to the wiki. I hope this
can be considered for inclusion to the R distribution.
I took a quick stab at adding making a copy of argc and argv to the R
script, but those macros look quite scary, so I might be doing many
more mistakes than I am aware of (hint: malloc). Now command line
arguments are also passed, and the following also works:
--------args.r:
#!/usr/bin/rinterp
cat("n args: ")
cat(argc)
cat("\n")
for (i in 1:argc)
{
cat(sprintf("arg %d = %s\n",i,argv[i]))
}
-------------
./args.r a b c
n args: 4 arg 1 = ./args.r arg 2 = a arg 3 = b arg 4 = c The attachment contains the new rinterp.c juha
On 7/14/06, Jeffrey Horner <jeff.horner at vanderbilt.edu> wrote:
Juha Vierinen wrote:
Hi,
I am considering if I should invest in learning R. Based on the
language definition and introductory documents, it seems nice. But now
I am faced with a problem: I want to be able to run R programs easily
from the unix shell, and write scripts that can automatically select R
as the interpreter:
#!/usr/bin/R
cat("Hello world.\n")
This of course doesn't work, because /usr/bin/R is a shell script.
I have been able to create a binary wrapper that calls R with the
correct arguments, which is documented here:
http://kavaro.fi/mediawiki/index.php/Using_R_from_the_shell
This still lacks eg. standard input (but I have no idea how I can
implement it in R) and full command line argument passing (can be
done), but am I on the right track, or is there already something that
does what I need?
If you search the mailing list archives: http://tolstoy.newcastle.edu.au/R/ you can find entries like the following (searching on "bang"): http://tolstoy.newcastle.edu.au/R/devel/05/01/1910.html which implements similar functionality. But I thought this would be a fun little (distracting) project, so I created an R wiki page: http://wiki.r-project.org/rwiki/doku.php?id=developers:rinterp with source code describing how to solve this problem by embedding R within your C program. Here's the main function: /* rinterp: shebang support for R * * Usage: * * From the command line: * $ rinterp filename * * In a file: * -------------------- * #!/path/to/rinterp * cat("hello world\n"); * -------------------- */ int main(int argc, char **argv){ /* R embedded arguments */ char *R_argv[] = {"RINTERP", "--gui=none", "--slave", "--silent", "--vanilla","--no-readline"}; int R_argc = sizeof(R_argv)/sizeof(R_argv[0]); struct stat sbuf; /* Setenv R_HOME: insert or replace into environment. * The RHOME macro is defined during configure */ if (setenv("R_HOME",RHOME,1) != 0){ perror("ERROR: couldn't set/replace R_HOME"); exit(1); } /* Assume last argument is the file we want to source. * Ignore other args for now. */ if (argc > 1) {/* don't try and source rinterp binary */ if (stat(argv[argc-1],&sbuf) != 0){ perror(argv[argc-1]); exit(1); } } else { fprintf(stderr,"Usage: rinterp filename\n"); exit(1); } /* Initialize R interpreter */ Rf_initEmbeddedR(R_argc, R_argv); /* Now call R function source(filename) */ call_fun1str("source",argv[argc-1]); exit(0); } Feel free to logon to the R wiki and contribute to the page. I think it would be interesting to solve this once and for all. Cheers! -- Jeffrey Horner Computer Systems Analyst School of Medicine 615-322-8606 Department of Biostatistics Vanderbilt University
______________________________________________ R-devel at r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-devel