tags:

views:

61

answers:

2

If I have an R script:

print("hi")
commandArgs()

And I run it using:

r CMD BATCH --slave --no-timing test.r output.txt

The output will contain:

[1] "hi"
[1] "/Library/Frameworks/R.framework/Resources/bin/exec/x86_64/R"
[2] "-f"                                                         
[3] "test.r"                                                     
[4] "--restore"                                                  
[5] "--save"                                                     
[6] "--no-readline"                                              
[7] "--slave"                                                    

How can i suppress the line numbers[1]..[7] in the output so only the output of the script appears?

+3  A: 

Use commandArgs(TRUE) and run your script with Rscript.

mbq
+3  A: 

Yes, mbq is right -- use Rscript, or, if it floats your boat, littler:

$ cat /tmp/tommy.r 
#!/usr/bin/r

cat("hello world\n")
print(argv[])
$ /tmp/tommy.r a b c
hello world
[1] "a" "b" "c"
$

You probably want to look at CRAN packages getopt and optparse for argument-parsing as you'd do in other scripting languages/

Dirk Eddelbuettel