在R中的同一行重复打印

时间:2022-10-25 00:07:18

I was just wondering what is the best way in R to keep on printing on the same line in a loop, to avoid swamping your console? Let's say to print a value indicating your progress, as in

我只是想知道R中最好的方法是在循环中继续在同一行上打印,以避免淹没你的控制台?比方说打印一个表示进度的值,如

for (i in 1:10) {print(i)}

Edit:

I tried inserting carriage returns before each value as in

我尝试在每个值之前插入回车符,如

for (i in 1:10000) {cat("\r",i)}

but that also doesn't quite work as it will just update the value on the screen after the loop, just returning 10000 in this case.... Any thoughts?

但这也不是很有效,因为它只会在循环后更新屏幕上的值,在这种情况下只返回10000 ....任何想法?

NB this is not to make a progress bar, as I know there are various features for that, but just to be able to print some info during the progression of some loop without swamping the console

注意,这不是制作进度条,因为我知道它有各种功能,但只是为了能够在某些循环的过程中打印一些信息而不会淹没控制台

3 个解决方案

#1


7  

You have the answer, it's just looping too quickly for you to see. Try:

你有答案,它只是循环太快你无法看到。尝试:

for (i in 1:10) {Sys.sleep(1); cat("\r",i)}

EDIT: Actually, this is very close to @Simon O'Hanlon's answer, but given the confusion in the comments and the fact that it isn't exactly the same, I'll leave it here.

编辑:实际上,这与@Simon O'Hanlon的答案非常接近,但考虑到评论中的混淆以及它不完全相同的事实,我将把它留在这里。

#2


1  

Try using cat()...

尝试使用cat()......

for (i in 1:10) {cat(paste(i," "))}
#1  2  3  4  5  6  7  8  9  10  

cat() performs much less conversion than print() (from the horses mouth).

cat()执行的转换比print()(来自马口)的转换少得多。

To repeatedly print in the same place, you need to clear the console. I am not aware of another way to do this, but thanks to this great answer this works (in RStudio on Windows at least):

要在同一个地方重复打印,您需要清除控制台。我不知道另一种方法可以做到这一点,但由于这个很好的答案,这是有效的(至少在Windows上的RStudio):

for (i in 1:1e3) {
  cat( i )
  Sys.sleep(0.01)
  cat("\014")
}

#3


0  

Well... are you worried about hangs, or just about being notified when the job completes?

嗯...你担心挂起,还是只是在工作完成时得到通知?

In the first case, I'd stick w/ my j%%N suggestion, where N is large enough that you don't swamp the console.

在第一种情况下,我会坚持我的j %% N建议,其中N足够大,你不会淹没控制台。

In the second case, add a final line to your script or function which, e.g., calls "Beep" .

在第二种情况下,在脚本或函数中添加最后一行,例如,调用“Beep”。

#1


7  

You have the answer, it's just looping too quickly for you to see. Try:

你有答案,它只是循环太快你无法看到。尝试:

for (i in 1:10) {Sys.sleep(1); cat("\r",i)}

EDIT: Actually, this is very close to @Simon O'Hanlon's answer, but given the confusion in the comments and the fact that it isn't exactly the same, I'll leave it here.

编辑:实际上,这与@Simon O'Hanlon的答案非常接近,但考虑到评论中的混淆以及它不完全相同的事实,我将把它留在这里。

#2


1  

Try using cat()...

尝试使用cat()......

for (i in 1:10) {cat(paste(i," "))}
#1  2  3  4  5  6  7  8  9  10  

cat() performs much less conversion than print() (from the horses mouth).

cat()执行的转换比print()(来自马口)的转换少得多。

To repeatedly print in the same place, you need to clear the console. I am not aware of another way to do this, but thanks to this great answer this works (in RStudio on Windows at least):

要在同一个地方重复打印,您需要清除控制台。我不知道另一种方法可以做到这一点,但由于这个很好的答案,这是有效的(至少在Windows上的RStudio):

for (i in 1:1e3) {
  cat( i )
  Sys.sleep(0.01)
  cat("\014")
}

#3


0  

Well... are you worried about hangs, or just about being notified when the job completes?

嗯...你担心挂起,还是只是在工作完成时得到通知?

In the first case, I'd stick w/ my j%%N suggestion, where N is large enough that you don't swamp the console.

在第一种情况下,我会坚持我的j %% N建议,其中N足够大,你不会淹没控制台。

In the second case, add a final line to your script or function which, e.g., calls "Beep" .

在第二种情况下,在脚本或函数中添加最后一行,例如,调用“Beep”。