[R-SIG-Mac] Directing output to R.app GUI from compiled C code

Michael Braun braunm at MIT.EDU
Sun Mar 30 19:37:51 CEST 2008


Thank you for everyone's help on this.  I think I have narrowed the  
issue down to the buffer.  When I run R in the terminal, the line:

fprintf(stdout,"testing\n");

gets flushed after each line, but when I run under R.app, all calls  
get flushed at the end of my program.

If I add the line

fflush(stdout);

after each fprintf call in R,app, the text is flushed as expected.

The line

fprintf(stderr,"testing\n");

gets flushed line-by-line in both cases.

So, for some reason, I need to flush the stdout buffer after each line  
in R.app, but not when I run R in terminal.  This is an easy  
workaround, although I am interested by this would work differently in  
the terminal and GUI versions of R.





On Mar 29, 2008, at 7:00 PM, Simon Urbanek wrote:
>
> On Mar 29, 2008, at 6:03 PM, Michael Braun wrote:
>
>> My C code is complex (and obscure), so I'd hate to bore the rest of  
>> the list with the details.  But perhaps this simple example will  
>> narrow things down. (My shared library is compiled using R CMD  
>> SHLIB, and loaded into R using dyn.load("xxx.so").  I call the  
>> function using the .C() function.  The application is straight  
>> numerical computation, with no fancy graphics or anything like that.)
>>
>> If I print text using the R API function
>>
>> 	Rprintf("This is some text\n");
>>
>> then I see that text, whether I run R from either the terminal or  
>> using R.app.  But, if I use, from the standard C library,
>>
>> 	printf("This is some text\n");
>>
>> then I see it if I run R in the terminal window, but NOT in R.app.
>>
>> A more complicated example is if I try to print a gsl vector
>>
>> 	gsl_vector_fprintf(stdout, X, "%.3f");
>>
>> The result shows up in the terminal window, but not in R.app.  What  
>> I'd like to do is find something to replace stdout that would  
>> guarantee that the output goes to the appropriate R window.
>>
>> I'm not doing anything special to flush output (I'm a new C  
>> programmer--is this something I need to do?).  I get this problem  
>> if I am trying to print a single line, as above, or a HUGE number  
>> of gsl_vectors (it was being caught in an infinite loop of printing  
>> vectors--and not seeing the output to let me know I was in the  
>> loop--that led me to this question).
>>
>> I hope this is detailed enough.
>
> Not really, because what you describe above works as designed:
>
> #include <R.h>
> #include <stdio.h>
> void foo() {
>   Rprintf("Friendly output\n");
>   fprintf(stderr, "this goes to stderr\n");
>   printf("Hello, world!\n");
> }
>
> > dyn.load("foo.so")
> > .C("foo")
> Friendly output
> list()
> this goes to stderr
> Hello, world!
>
> As you can see the stdout output is in gray, stderr is in red and  
> regular console output is in black (if you use default colors). All  
> is there. (Note, however, the output order is not necessary linear,  
> because stdout/err must be captured asynchronously).
>
>
>> Please let me know if more information is needed.
>
> Which version of R and R.app are you using? Also the output can be  
> disabled in the preferences - if you did so, it won't appear. Can  
> you see the output of "system('ls')"?
>
> Cheers,
> S
>
> [This message also contains rich version of the e-mail to illustrate  
> the point. Apologies to plain text recipients.]
>
>
>>
>> On Mar 29, 2008, at 5:24 PM, Simon Urbanek wrote:
>>> Michael,
>>>
>>> On Mar 29, 2008, at 2:23 PM, Michael Braun wrote:
>>>
>>>> I am a recent convert from Linux to Mac (OS X 10.5.2 on a Mac  
>>>> Pro), and  am trying to direct some output from  compiled C code  
>>>> to the R.app GUI.  For simple things, of course, I use Rprintf  
>>>> from the R API.  But there are other cases (e.g., using the  
>>>> matrix print routines in GSL) for which Rprintf is  
>>>> inappropriate.  When I was in Linux-land, I could direct the  
>>>> output to stdout with no problem (and I can still do that running  
>>>> R from a terminal window on OS X).  But if I run my app under  
>>>> R.app, the output never makes it to the screen (naturally,  
>>>> because R.app is not standard output).
>>>>
>>>
>>> If we are to assist you, you should tell us more about your  
>>> application (the C code), because R.app *does* print stdout and  
>>> stderr (just try running system("ls")). Your application is very  
>>> likely doing something to prevent the output from being processed  
>>> (not flushing the output, printing unterminated lines, not  
>>> allowing R to process events...).
>>>
>>> Cheers,
>>> Simon
>>>
>>>
>>>> So, is there an output file that I can use in place of stdout that
>>>> will get my output to the R.app GUI (and preferably, to a terminal
>>>> window session as well)?
>>>>
>>>> Thanks!
>>>>
>>>> Michael
>>>>
>>>>
>>>>
>>>>
>>>> Michael Braun
>>>> Assistant Professor of Management Science (Marketing Group)
>>>> MIT Sloan School of Management
>>>> One Amherst St., E40-169
>>>> Cambridge, MA 02142
>>>> braunm at mit.edu
>>>> 617-253-3436
>>>>
>>>> _______________________________________________
>>>> R-SIG-Mac mailing list
>>>> R-SIG-Mac at stat.math.ethz.ch
>>>> https://stat.ethz.ch/mailman/listinfo/r-sig-mac
>>>>
>>>>
>>>
>>
>> Michael Braun
>> Assistant Professor of Management Science (Marketing Group)
>> MIT Sloan School of Management
>> One Amherst St., E40-169
>> Cambridge, MA 02142
>> braunm at mit.edu
>> 617-253-3436
>>
>>
>>
>>
>

Michael Braun
Assistant Professor of Management Science (Marketing Group)
MIT Sloan School of Management
One Amherst St., E40-169
Cambridge, MA 02142
braunm at mit.edu
617-253-3436



More information about the R-SIG-Mac mailing list