Skip to content

executable R scripts

5 messages · Jonathan Baron, Dirk Eddelbuettel, John Zedlewski +1 more

#
Hi, I'm a newbie trying to make an R program executable on UNIX, just like one 
would write an executable perl script by putting "#!/usr/bin/perl" in the 
first line, and so on.

It seems, though, that this would only work if I use the "BATCH" command to 
tell R to execute the program in its first argument. This would have the 
unfortunately side-effect of dumping all output to a file rather than stdout.

Additionally, I'd want to see only the results of "print" statements on 
stdout, not all off R's output, just as when you source a script with 
echo=FALSE.

This seems like it would be a pretty common problem, but I haven't found any 
explanations in the docs. Does somebody have a sample script that I could 
look at for advice? Or should I just bite the bullet and write a wrapper 
shell script?

Thanks!
--JRZ
#
On 06/08/03 17:35, John Zedlewski wrote:
See
man R
for how to do it, although I'm not sure where it says the
following:

To get just the print output and nothing else, it helps to have
print()'s in the script itself.  Then you can use

R --slave < myfile.R > printoutput.txt

I also use R --vanilla < myfile.R
for a R file that has write.table()'s in it.  For this you do not
need to pipe the output anywhere.
#
On Sun, Jun 08, 2003 at 05:35:14PM -0700, John Zedlewski wrote:
This is not currently supported, but with some luck may be supported in a
later version of R.
My personal favourite currently is to arrange everything (loading of
package, code, ...) in a file which I can read with source() from within R.
Then
    $ echo "source(\"foo.R\") | R --slave
works quite well, you can redirect etc. Works on windows/cygwin too using
Rterm.exe.
I think the above fits that bill.
That's where the above leads to as well.

Dirk
#
Dirk and Jonathan--
  Thanks a lot for the fast and helpful comments, guys. I ended up writing a 
wrapper script that uses the trick of echoing "source(\"filename\")" into R 
--slave, and it works well.
  Thanks again!
--JRZ
#
On 20030608 (Sun) at 1735:14 -0700, John Zedlewski wrote:
I've seen the other replies, but thought this might be of interest too:
I hacked a hashbang wrapper so you can start an R script with
  #! /bin/sh /usr/bin/setR
and then invoke it with command-line arguments, which get passed to the
script in a character vector called argv.

See http://www.bgl.nu/~glouis/setR.html if you're interested.  I think
the version displayed has a couple of ampersand-lt; that need to be
changed to < if you use 'save as' rather than cutting and pasting.