Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I suppress the line numbers output using R CMD BATCH?

Tags:

r

stdout

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?

like image 844
tommy chheng Avatar asked Jul 17 '10 14:07

tommy chheng


People also ask

How do you suppress a line in R?

By using invisible() function we can suppress the output.

How do I show line numbers in R?

These recommended settings are less confusing when learning R and RStudio. There are a few more editor settings that I personally find useful, so I click on “Code” in the sidebar, and then on the tab “Display”. I put ticks in the boxes in front of “Show line numbers” and “Show margin”.


2 Answers

Use cat instead of print if you want to suppress the line numbers ([1], [2], ...) in the output.

I think you are also going to want to pass command line arguments. I think the easiest way to do that is to create a file with the RScript shebang:

For example, create a file called args.r:

#!/usr/bin/env Rscript
args <- commandArgs(TRUE)
cat(args, sep = "\n")

Make it executable with chmod +x args.r and then you can run it with ./args.r ARG1 ARG2

FWIW, passing command line parameters with the R CMD BATCH ... syntax is a pain. Here is how you do it: R CMD BATCH "--args ARG1 ARG2" args.r Note the quotes. More discussion here

UPDATE: changed shebang line above from #!/usr/bin/Rscript to #!/usr/bin/env Rscript in response to @mbq's comment (thanks!)

like image 188
David J. Avatar answered Sep 28 '22 08:09

David J.


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/

like image 39
Dirk Eddelbuettel Avatar answered Sep 28 '22 07:09

Dirk Eddelbuettel