大约 3 年前曾在这里提出过类似的问题 How do I print a variable inside a for loop to the console in real time, as the loop is running, in R? 。与我运行 for 循环时的问题类似,它会将每个输出变量显示到屏幕上,而不清除前一个变量。
a=matrix(c(1,8,2,5),nrow=2)
for(i in 1:4){
print(a*i)
}
[,1] [,2]
[1,] 1 2
[2,] 8 5
[,1] [,2]
[1,] 2 4
[2,] 16 10
[,1] [,2]
[1,] 3 6
[2,] 24 15
[,1] [,2]
[1,] 4 8
[2,] 32 20
为简单起见,我仅对低维矩阵使用四次迭代,但我真正的 for 循环有超过 1000 次迭代和一个大维矩阵,这使得输出丑陋且占用大量内存。
我尝试了solution在上面提出的问题中
for(i in 1:10){
Sys.sleep(0.1)
print(i)
flush.console()
}
但它没有改变我的 R 输出(安装在 ubuntu 中)中的任何内容,并且仍然打印出所有变量
注意:我不是在寻找进度条
最佳答案
也许system('clear')
,如Function to clear the console in R的答案中所建议的那样。例如:
for(i in 1:10){
system('clear')
print(matrix(rnorm(9), nc = 3))
Sys.sleep(0.5)
}
关于r - 如何在ubuntu中安装的R中打印循环内的变量?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39886394/