1

Consider the two scenarios a)shutdown output redirection and b)Rscript output redirection to file out.txt The output as seen in STDOUT in both scenarios, fails to get redirected to file out.txt only in scenario b. Am I missing something here or is this peculiar to Rscript.exe?

C:\>C:\\WINDOWS\\system32\\shutdown.exe -t:30 > out.txt

C:\>dir out.txt
 Volume in drive C has no label.
 Volume Serial Number is 3568-1B07

 Directory of C:\

07/18/2012  07:43 PM               841 out.txt
               1 File(s)            841 bytes
               0 Dir(s)  17,618,206,720 bytes free

C:\>G:\\Progra~1\\R\\R-2.14.0\\bin\\Rscript.exe --version > out.txt
R scripting front-end version 2.14.0 (2011-10-31)

C:\>dir out.txt
 Volume in drive C has no label.
 Volume Serial Number is 3568-1B07

 Directory of C:\

07/18/2012  07:44 PM                 0 out.txt
               1 File(s)              0 bytes
               0 Dir(s)  17,618,239,488 bytes free

Any ideas or explanation on why Rscript.exe output is not getting redirected to file out.txt?? Maybe a very simple explanation awaits.

Thanking you,

useR
  • 23
  • 5
  • Not sure about the redirect, but you can use the `sink()` command in R to accomplish what you're looking for. – Jeff Allen Jul 18 '12 at 14:24
  • I'm guessing the data is being written to STDERR and not STDOUT. Try appending `2>&1` to the commands to redirect STDERR to STDOUT, which will then end up in the outfile. – DaveRandom Jul 18 '12 at 14:30
  • @JeffAllen Thanks Jeff. Actually I am looking to do this via a php script embedding the above commands which checks for the R version. – useR Jul 18 '12 at 14:30
  • @DaveRandom Thanks Dave. The output was being written to STDERR and 2>&1 accounted for it. – useR Jul 18 '12 at 14:33

2 Answers2

1

You can't use a redirect to get the version information, but you can use the redirect for actual R commands. For example, if you have a file a.r with just one command in it, like 1+1 then:

Rscript a.r > out.txt

will display the results. I think it has something to do with the "output" that R sends the version information to. I think it is output to STDERR rather than STDOUT (or the equivalent concept in DOS), and so redirection doesn't work.

nograpes
  • 18,623
  • 1
  • 44
  • 67
1

The answer is pretty simple. The version information is written to STDERR (standard error stream), yet you're only redirecting STDOUT (standard out). If you want both to go the file, then use

G:\\Progra~1\\R\\R-2.14.0\\bin\\Rscript.exe --version >out.txt 2>&1
Aleks G
  • 56,435
  • 29
  • 168
  • 265
  • Thanks @Aleks. STDOUT indeed seems to be filehandle for STDOUT i.e c:\Rscript.exe --version" > STDOUT >2&1 I guess the above works without involving an additional file, saving the overhead of disk I/O. – useR Jul 18 '12 at 15:27