tags:

views:

177

answers:

3

The title says it all. The objective is to have two simple ways to source some code, say func.R, containing a function. Calling R CMD BATCH func.R initializes the function and evaluates is. Within a session, issuing source("func.R") simply initializes the function. Any idea?

+2  A: 

You could pass arguments into R, and if an argument is present run main(). More on arguments here: http://yangfeng.wordpress.com/2009/09/03/including-arguments-in-r-cmd-batch-mode/

Vince
I voted the other answer, but I didn't know you could pass arguments from the command line either. That's helpful.
gappy
Since about the beginning of 2009 it's been possible to run R scripts with a shebang line, e.g. `#!/usr/bin/Rscript --vanilla` directly from the command line: e.g. run `chmod 700 my_script.r` to make it executable, and then you can run the script with `./my_script.r` (within the script you read arguments the same way as in the link above, by `args=(commandArgs(TRUE))`
Michael Dunn
+5  A: 

I think that the interactive() function might work.

Harlan
Excellent, I had no idea this existed. I think this will change the way I write R.
Vince
With the Rscript shebang line and this our R scripts will be more pythonic than ever before!
Michael Dunn
A: 

Always wanted this, never could figure it out.

Brendan OConnor