Here is a very simple #! a script that creates the foo() function, whose sole purpose is to drive away its argument to 'bar' .
#! /home/gavin/R/2.13-patched/build/bin/Rscript --vanilla foo <- function(bar) { writeLines(paste("You supplied the following information:", bar)) }
We extract the command line arguments passed to the script from the shell using the commandArgs() function, and then pass them to foo() with the last line of the script.
If we have a bit of code in the foobar.R file, we can pass the argument and run the script using the Rscript interface. You also need to execute the above executable file ( chmod it).
Then the script can be called as it works as follows:
[ gavin@desktop ~]$ ./foobar.R Cl You supplied the following information: Cl
But pay attention to the information in ?Rscript , because, unfortunately, the standard Windows cmd shells are not aware of #! like scripts, so you might need to install another shell (help tells you that the Cygwin shell should work) in order to use the displayed functionality.
Update: Using source() and readline() .
An alternative, if you can do without having to work non-interactively (i.e. you do not open the R interface of R and do not run one line of code), this is just a source() script. For example, if it was in a call to script barfoo.R :
dynamicwilcox <- function() { ANSWER <- readline("What column do you want to work on? ") if(ANSWER=="Ph") { writeLines("column was 'Ph'") } else if(ANSWER=="Cl") { writeLines("column was 'Cl'") } else { writeLines(paste("Sorry, we don't know what to do with column", ANSWER)) } ANSWER
Then from the invitation of R Gui we could do:
R> source("barfoo.R") What column do you want to work on? Cl column was 'Cl'
or if you do not want to specify the full path, do the following:
R> source(file.choose())
readline() works great when used in an interactive R session, and really is the best tool for the job - this is exactly what was done for it.
The whole premise that you want to run the script in batch mode, but provide some input, does not make much sense. R expects scripts to be autonomous when run in batch mode. You may not be aware of this, but when you double-click your script, it runs in batch mode.