r - 无法从 R 中的 while 循环中打印到控制台

标签 r loops console output

我在 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/

相关文章:

r - 当 NA == TRUE 时复制 df 的行加上 R 中的上下行

带有 as.factor() : any way to specify the argument as a variable content instead of directly by name? 的 R 公式

r - 在多个条件下过滤 Shiny 的数据表

javascript - 同位素 - 无法读取未定义的属性 'filteredItems'

c - 控制台无输出

r - 使用 R 跨文件夹中的文件错误 map_df

c++ - 难以理解 OpenMP 代码中的某些意外行为

bash - 超时重试 Bash 命令

c++ - int 值从 0 跳到 32679 没有变化

C# 控制台应用程序构建但不启动应用程序