我在 Windows 的 Rgui.exe 窗口中运行这段代码:
i = 1
while(i <= 10){
cat(i, "\n")
i = i + 1
Sys.sleep(2)
}
cat() 片段不会每 2 秒输出一次,而是仅在 20 秒结束时集体输出,即 10 次迭代。
我习惯了 print() 和 cat() 调用立即得到输出,所以我不知道为什么会在这里发生。
如何让 R 在每次迭代中立即打印?
最佳答案
是“输出缓冲”;在使用 cat
之后尝试 flush.console()
。从帮助页面:
This does nothing except on console-based versions of R. On the macOS and Windows GUIs, it ensures that the display of output in the console is current, even if output buffering is on.
关于r - 无法从 R 中的 while 循环中打印到控制台,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44331410/