2

I created a R package which depends on Rcpp. A function in this package supposed to show printing statements at every n iterations. So I expect to see a new line on R console every few seconds.

The odd thing is that when I run my function in R GUI, the cursor becomes a loading wheel and R "almost" freezes. The loading wheel disappear once after the computation is done.

The minimal example of this situation is summarized as follow:

library(inline)
library(Rcpp)
test <- cxxfunction(
signature(),
body= '

RNGScope scope;
for (int i = 0; i < 100; i++)
{
sleep(1); // sleep one second at each iteration. this sleep is
// replaced by something in my code
if (i%20==0) Rprintf("\v%d iterations are done...",i);
}

return wrap(1);
' ,
plugin="Rcpp"
               )

test()// freeze for 100 seconds!

I also found that if the code is run on terminal, the new lines appear every 20 seconds as I expected. But I prefer to run it on R GUI.

I would appreciate if someone can tell me why this is happening..

I am using Mac.

FairyOnIce
  • 2,526
  • 7
  • 27
  • 48

2 Answers2

2

Rgui buffers the output. I don't use Rgui, but try and find setting which controls whether or not output is buffered or not. For R code you could use flush.console to force the output to be shown, but I'm not entirely sure how this would work with C++ code.

Paul Hiemstra
  • 59,984
  • 12
  • 142
  • 149
  • +1 -- this has almost nothing to do with Rcpp and is only about Rgui on Windows where, if memory serves, you have a config toggle to unset buffering. – Dirk Eddelbuettel Sep 01 '12 at 15:14
1

The question is about R.app on the Mac, not Rgui on Windows. The solution below works for me: follow Rprintf with R_FlushConsole and R_ProcessEvents, like this:

RNGScope scope;
for (int i = 0; i < 100; i++) {
    sleep(1); // sleep one second at each iteration. this sleep is
              // replaced by something in my code
if (i%20==0) {
  Rprintf("\v%d iterations are done...\n",i);
  R_FlushConsole();
  R_ProcessEvents();
}

return wrap(1);
Davor Cubranic
  • 1,051
  • 10
  • 16