Rcpp: Mac shows loading wheel and almost freeze

感情迁移 提交于 2019-12-07 23:15:34

问题


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.


回答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);



回答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.



来源:https://stackoverflow.com/questions/12225049/rcpp-mac-shows-loading-wheel-and-almost-freeze

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!