[R] Capturing buffered output from Rterm

Duncan Murdoch murdoch.duncan at gmail.com
Thu Jun 10 12:47:35 CEST 2010


Keith Jewell wrote:
> In MS Windows I
>   a) invoke Rterm from a batch file (test.bat)
>   b) to execute commands from a script (m:\test.rsc)
>   c) capturing output in a log file (m:\test.log)
> BUT if the script results in an error the error message is NOT written to 
> the log file, leaving me problems when the error is from a complicated 
> function.
>
> Simplified example:.
>
> test.bat ------------------------
> REM ensure 'R' is in path
> path \\Server02\stats\R\R-Current\bin\;%PATH%
> Rterm  --no-init-file --no-restore-data --no-save --silent < m:\test.rsc > 
> m:\test.log
> ---------------------
>
> m:\test.rsc -----------------
> print("this is a test")
> #generate an error
> nls()
> ------------------
>
> The error message:
>   "Error in .Internal(inherits(x, what, which)) : 'x' is missing"
> is is NOT written to the log file, which just ends
>
> m:\test.log --------------------------
>   <snip>
>   
>> print("this is a test")
>>     
> [1] "this is a test"
>   
>> #generate an error
>> nls()
>>     
> ---------------------
>
> I surmise this is due to output buffering (?). In an S-Plus version I turned 
> off buffering with
>   guiSetOption(option.name="BufferOutputWindows", value.string="F")
> but I don't think this is available in R (?).
>
> Has anyone any suggestions?

It's not output buffering, it's because error messages go to a different 
file handle than regular ones.  You need to redirect both stdout and stderr.

I'm not sure of the syntax to do that in Windows CMD, but R CMD BATCH 
test.rsc instead of Rterm would do it.  (The output should go to test.Rout.)

Duncan Murdoch



More information about the R-help mailing list