tags:

views:

61

answers:

2

When running

R CMD BATCH [options] filename.r

I want to control where the output is printed. I can suppress the creation of the .Rout file with

R CMD BATCH [options] filename.r /dev/null

but is it possible to direct the output to the screen? Like when I run it by

R [options] < filename.r

?

+1  A: 

Try Rscript or R --no-save < filename.R:

biocoreap1:Desktop vinceb$ Rscript test.R
test
biocoreap1:Desktop vinceb$ R --no-save < test.R

R version 2.10.1 (2009-12-14)
Copyright (C) 2009 The R Foundation for Statistical Computing
ISBN 3-900051-07-0

[...]

[Previously saved workspace restored]

> cat('test\n')
test
> 
Vince
Thanks - that's the way I've been doing it (using input redirection), but want to emulate its behavior (the part where it prints to screen) with the R CMD BATCH - David Smith [notes][1] that there are advantages to R CMD BATCH, and I also use emacs eshell which does not support input redirection. And I know about Rscript also, but that requires that I add a line to the header and the make it executable, etc.[1] http://blog.revolutionanalytics.com/2009/06/running-scripts-with-r-cmd-batch.html
Stephen
There is no shebang in the Rscript script I tested above: It's just `cat('test\n')`. You only need executable + shebang if you want to run it with `./filename.R`, not `Rscript filename.R`
Vince
Ah! That's nice - but there's nothing printed to screen except through explicit calls to cat() or print() or write*()? I am hoping for what's printed to screen in `R --no-save < test.R`, or the comments printed to file in the R CMD BATCH case...
Stephen
(Mainly so that I notice when there's an error)
Stephen
You see when you have an error with `Rscript`. Try something like `lm('BUG`)`.
Vince
Hmm... yes, I see... doesn't always indicate where the error was (in a long script with similar statement calls), as does printing the input and output to screen. Maybe this is the best way though?
Stephen
@Vince - thanks - I will keep Rscript in mind, but this time it was the output that directing to /dev/tty provides that I was looking for. But thanks though!
Stephen
+1  A: 

Guess you're on linux. Tried already to redirect to /dev/console ?

Edit -add info from the comments -:

/dev/console apparently doesn't work, /dev/tty does. Depending on the system, /dev/tty0 might be an option too

Cheers

Joris Meys
Ah! That was what I was hoping for... but did not work. Thanks though.
Stephen
Alternatively you can try /dev/tty or dev/tty0, but success isn't guaranteed either. I can't test it here, I have no Linux available on this computer.
Joris Meys
`/dev/tty` was it! Thank you -
Stephen