Re: [Rd] R as shell script

From: Jeffrey Horner <jeff.horner_at_vanderbilt.edu>
Date: Fri 14 Jul 2006 - 17:31:13 GMT

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@r-project.org mailing list
https://stat.ethz.ch/mailman/listinfo/r-devel
Received on Sat Jul 15 03:34:10 2006

Archive maintained by Robert King, hosted by the discipline of statistics at the University of Newcastle, Australia.
Archive generated by hypermail 2.1.8, at Mon 17 Jul 2006 - 22:31:08 GMT.

Mailing list information is available at https://stat.ethz.ch/mailman/listinfo/r-help. Please read the posting guide before posting to the list.