title: What's the best way to use R scripts on the command line (terminal)?
body: <p>It's very convenient to have R scripts for doing simple plots from the command line.  However, running R from bash scripts is not convenient at all.  The ideal might be something like</p>
  
  <pre><code>#!/path/to/R
  ...
  </code></pre>
  
  <p>or</p>
  
  <pre><code>#!/usr/bin/env R
  ...
  </code></pre>
  
  <p>but I haven't been able to make either of those work.</p>
  
  <p>Another option is keeping the scripts purely in R, e.g. <code>script.R</code>, and invoking it with <code>R --file=script.R</code> or similar.  However, occasionally a script will rely on obscure command line switches at which point part of the code exists outside the script.  Example: sneaking things into R from bash via a local .Rprofile, the desired switches are then everything <code>--vanilla</code> implies except <code>--no-init-file</code>.</p>
  
  <p>Another option is a bash script to store the R flags and be painlessly executable, which then calls the R script.  The problem is that this means a single program just got split into two files which now have to be keep in sync, transferred to new machines together, etc.</p>
  
  <p>The option I currently despise least is embedding the R in a bash script:</p>
  
  <pre><code>#!/bin/bash
  ... # usage message to catch bad input without invoking R
  ... # any bash pre-processing of input
  ... # etc
  R --random-flags &lt;&lt;RSCRIPT
  # R code goes here
  RSCRIPT
  </code></pre>
  
  <p>Everything's in a single file.  It's executable and easily handles arguments.  The problem is that combining bash and R like this pretty much eliminates the possibility of any IDE not failing on one or the other, and makes my heart hurt real bad.</p>
  
  <p>Is there some better way I'm missing?</p>