[R] [Rd] R as shell script

François Pinard pinard at iro.umontreal.ca
Fri Jul 14 19:17:08 CEST 2006


[Juha Vierinen]
>Hi,

Hello, Juha.  Your request, quoted below, is likely more appropriate for
R help than for R devel, so I'm redirecting this reply there.

>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?

I'm often using something like:

   #!/bin/sh
   R --slave --vanilla <<EOF

   # Your R source code goes here!

   EOF

Within your script, shell substitution for $1, etc., will occur.  So 
with a bit of imagination, you can do about anything :-).  Simple 
enough!  Make sure you `cat' or `print' explicitly whatever has to be 
written on standard output: for one, I usually prefer full control in 
scripts over automatic printing of given expressions.

-- 
François Pinard   http://pinard.progiciels-bpi.ca



More information about the R-help mailing list