[Rd] printf capture

Peter Dalgaard p.dalgaard at biostat.ku.dk
Thu Apr 12 14:21:43 CEST 2007


Prof Brian Ripley wrote:
> If you are using Rgui (it should work under rterm) there is no C-level 
> 'stdout' file stream (the normal state for Windows GUI programs), so no 
> way to capture it inside R.
>
> On Thu, 12 Apr 2007, mel wrote:
>
>   
>> printf capture
>>
>> Dear All,
>>
>> I'm running R-2.4.1 on WindowsXP.
>> I wrote a small C++ DLL using Rprintf() and all works fine
>> dyn.load(), is.loaded('f1'), Rprintf(), .C(), all is ok.
>>
>> Now, the worry :
>> I use also a 3rd party piece of C++ program which was not designed
>> for R and uses printf().
>>     
>
> If you have the program, can you not edit it before compiling it?
> Adding
>
> #define printf Rprintf
>
> will probably do the trick.
>
>   
If not:

If it really is a standalone program, look into variations of system() 
and shell().

If it is a DLL, you might get away with explicitly opening a file 
descriptor for stdout on entry and closing it on exit. Not sure exactly 
how this works, though, especially on Windows.
>> I though on sink(...) before .C('f1'), but it doesn't work.
>> I call sink(file="res.txt") and only the Rprintf() are catched
>>
>> Is there any way to however capture those printf() outputs ?
>>
>> Thanks for any hint/pointer (... and i apologize
>> if i missed something obvious !).
>>
>> Vincent
>>
>> (for what may be worth, a "minimal" stuff is at www.khi.fr/IB/rp2)
>>
>> ______________________________________________
>> R-devel at r-project.org mailing list
>> https://stat.ethz.ch/mailman/listinfo/r-devel
>>
>>     
>
>



More information about the R-devel mailing list