Command-line arguments: Difference between revisions

give an actual example!
(Added Bracmat)
(give an actual example!)
Line 1,009:
 
=={{header|R}}==
 
<lang R>commandArgs(TRUE)</lang>
Following adapted from [http://quantitative-ecology.blogspot.com/2007/08/including-arguments-in-r-cmd-batch-mode.html this post by Forester]:
 
Suppose you want to call your script <tt>test.r</tt> with the arguments <tt>a=1 b=c(2,5,6)</tt>, where <tt>b</tt> is a numeric vector. Suppose you also want to redirect your output to <tt>test.out</tt> (not that you have a choice: I still don't know how to make R send shell-script output to stdout). You would then run
 
<lang R>R CMD BATCH --vanilla '--args a=1 b=c(2,5,6)' test.r test.out &</lang>
 
from the commandline. Your resulting output <tt>test.out</tt> would contain (e.g., if you <tt>cat</tt> it)
 
<pre>R version 2.14.0 (2011-10-31) [or whatever version you are running,
followed by several more lines of R startup cruft,
before it echoes your code:]
> ## Read the commandline arguments
> args <lang- R>(commandArgs(TRUE)</lang>)
>
> ## args is now a list of character vectors
> ## First check to see if arguments are passed.
> ## Then cycle through each element of the list and evaluate the expressions.
> if (length(args)==0) {
+ print("No arguments supplied.")
+ # supply default values
+ a = 1
+ b = c(1,1,1)
+ } else {
+ for (i in 1:length(args)) {
+ eval(parse(text=args[[i]]))
+ }
+ }
> print(a*2)
[1] 2
> print(b*3)
[1] 6 15 18
>
> proc.time()
user system elapsed
0.168 0.026 0.178
</pre>
 
=={{header|RapidQ}}==
Anonymous user