Rcpp:Mac显示加载轮并且几乎冻结 [英] Rcpp: Mac shows loading wheel and almost freeze

查看:103
本文介绍了Rcpp:Mac显示加载轮并且几乎冻结的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我创建了一个依赖于Rcpp的R包. 该软件包中的一个函数应该每n次迭代显示一次打印语句. 因此,我希望每隔几秒钟在R控制台上看到一条新行.

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.

奇怪的是,当我在R GUI中运行函数时,光标变成加载轮,R几乎"冻结.计算完成后,载重轮消失一次.

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!

我还发现,如果代码在终端上运行,新行将按预期每20秒出现一次. 但是我更喜欢在R GUI上运行它.

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

我正在使用Mac.

推荐答案

问题是关于Mac上的R.app,而不是Windows上的Rgui.下面的解决方案对我有用:使用R_FlushConsole和R_ProcessEvents跟随Rprintf,如下所示:

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

这篇关于Rcpp:Mac显示加载轮并且几乎冻结的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆