3

While running R code in batch mode i need messages/errors/logs from R code to be shown on console screen instead of .Rout file.

I tried all the posts(links copied below) from stack overflow and none of them are showing me desired output.

  1. running-r-in-batch-mode-print-to-screen?
  2. unable-to-run-r-script-through-bat-files-in-windows-server
  3. r-cmd-batch-output-in-terminal

batch code: i tried with tty0 as well.

"C:\Program Files\R\R-3.2.2\bin\R.exe" CMD BATCH --slave "Test.R" /dev/tty

Test.R code

print(1:10)

It will be very helpful if some has can help me in showing the correct way of writing the code to show the output in console screen.And i am working on windows environment.

Any help is much appreciated, thanks.

Community
  • 1
  • 1
PPC
  • 167
  • 2
  • 11
  • Isn't the whole point of running R in batch mode that you don't have a person there to see what is happening? Batch mode is more for for automated analyses. If you want to see the output, look in the log file or run R interactively. – Richie Cotton Jul 24 '16 at 06:32
  • @RichieCotton my purpose of running R code in batch is to make 3rd person not to open R/Rstudio. And i need to see intermediate results so i want that output to be in console screen. – PPC Jul 24 '16 at 06:40
  • 2
    I think you want Rscript, not R CMD BATCH – shayaa Jul 24 '16 at 07:20
  • @shayaa i am not familiar with format of .bat files. Can you please give me some example? – PPC Jul 24 '16 at 17:15
  • Might want to read [this question and answer](http://stackoverflow.com/questions/3412911/r-exe-rcmd-exe-rscript-exe-and-rterm-exe-whats-the-difference) – shayaa Jul 24 '16 at 17:21
  • Thanks @shayaa. It worked – PPC Jul 25 '16 at 01:53
  • How about you write your solution up for posterity, unless you wish to ask @abhiieor to edit his post with the full solution. Neither you nor I have the requisite rep to edit another's post. – shayaa Jul 25 '16 at 01:58
  • @shayaa posted solution. Please upvote and mark it as solution. – PPC Jul 25 '16 at 02:33

2 Answers2

3

You can redirect the log that you want to print using stdout or stderr. Sample way to do that will be:

write("Hello World!!", stderr())

Here I am redirecting "Hello World!!" to stderr. If you use stdout() instead of stderr() in code snippet above then ideally it should write log to stdout. However I have seen some issues when trying to write into stdout.

abhiieor
  • 3,132
  • 4
  • 30
  • 47
  • Thanks for your reply. Its not working, it still showing output in .Rout file. I intentionally plugged a undeclared variable `ss` in the code so that it can throw error in console, but it has shown error in .Rout file.`write("Hello World!!",ss, stderr())` – PPC Jul 24 '16 at 06:58
  • @Shayaa is correct. Please run your R script with "Rscript " and you will be able to write as suggested above. – abhiieor Jul 24 '16 at 09:57
  • Thanks a lot for your reply .It worked with the help of `Stdout()` – PPC Jul 25 '16 at 01:53
1

I found out solution to my own question with the help of @shayaa and @abhiieor.

In case if some one needs to see their output in console they are suppose to use Rscript instead of R CMD BATCH in executable .bat file.

batch code:

"C:\Program Files\R\R-3.2.2\bin\Rscript.exe" Test.R
pause

Test.R code

print("-Hello World!!",stdout())

In above code stdout() will redirect output to console.

PPC
  • 167
  • 2
  • 11