在R控制台中覆盖当前输出

时间:2022-03-14 20:43:46

I have been playing around with the R function txtProgressBar(). How can I hijack the function's ability to overwrite the current output in the console?

我一直在摆弄R函数txtProgressBar()。如何劫持函数覆盖控制台中的当前输出的能力?

i.e. the progress bar updates like this:

例如,进度条更新如下:

> some R function
============

becomes

就变成了

> some R function
========================

NOT

> some R function
============
========================

For example, how do I write function that will display the current time in the console:

例如,如何编写在控制台上显示当前时间的函数:

> some R function
13:01

becomes

就变成了

> some R function
13:02

NOT

> some R function
13:01
13:01
13:01
13:01
13:02
13:02
13:02
13:02

4 个解决方案

#1


13  

Instead of "\b\b\b\b" you can just use "\r" to go to the beginning of the line and overwrite everything on the line (make sure to still use cat and don't put in a line feed).

您可以使用“\b\b\b\b”来代替“\b\b\b”,转到行首并覆盖行上的所有内容(确保仍然使用cat,不要放入换行)。

Though if you want to display progress it might be better to use winProgressBar (windows only) or tkProgressBar (tcltk package, all platforms) which can be updated with a label in addition to the progress bar.

尽管如果您希望显示进度,最好使用winProgressBar (windows only)或tkProgressBar (tcltk包,所有平台),除了进度条之外,还可以使用标签进行更新。

On windows you can also use the setWindowTitle or setStatusBar functions to put that type of information into the top or bottom of the larger window.

在windows上,您还可以使用setWindowTitle或setStatusBar函数将该类型的信息放入较大窗口的顶部或底部。

#2


13  

This program seems to work:

这个程序似乎是有效的:

while (1) {
cat('\b\b\b\b\b\b',format(Sys.time(),'%H:%M'))
flush.console() 
}

Are there any reasons this might be a bad idea?

这可能是个坏主意,有什么原因吗?

/edit: even better (thanks @Greg Snow):

/编辑:更好(感谢@Greg Snow):

while (1) {
cat('\r',format(Sys.time(),'%H:%M:%S'))
flush.console() 
}

#3


4  

Sure you can:

当然,您可以:

while(1) {
  cat("\b\b\b\b\b\b\b\b",format(Sys.time(), "%H:%M:%S"),sep="")
}

#4


1  

I do not think overwriting is possible on the console. There is no backspace escape sequence. The progress bar can be drawn because the cat function will not emit a cr unless told to do so.

我认为在控制台上不可能写得过多。没有后空转义序列。可以绘制进度条,因为cat函数不会发出cr,除非被告知这样做。

Edit: I was wrong. The backspace character is recognized:

编辑:我错了。退格字符识别:

for (i in 1:1000) {
     cat(as.character(Sys.time())) 
     flush.console() 
     for(i in 1:19) {cat("\8")} }

#1


13  

Instead of "\b\b\b\b" you can just use "\r" to go to the beginning of the line and overwrite everything on the line (make sure to still use cat and don't put in a line feed).

您可以使用“\b\b\b\b”来代替“\b\b\b”,转到行首并覆盖行上的所有内容(确保仍然使用cat,不要放入换行)。

Though if you want to display progress it might be better to use winProgressBar (windows only) or tkProgressBar (tcltk package, all platforms) which can be updated with a label in addition to the progress bar.

尽管如果您希望显示进度,最好使用winProgressBar (windows only)或tkProgressBar (tcltk包,所有平台),除了进度条之外,还可以使用标签进行更新。

On windows you can also use the setWindowTitle or setStatusBar functions to put that type of information into the top or bottom of the larger window.

在windows上,您还可以使用setWindowTitle或setStatusBar函数将该类型的信息放入较大窗口的顶部或底部。

#2


13  

This program seems to work:

这个程序似乎是有效的:

while (1) {
cat('\b\b\b\b\b\b',format(Sys.time(),'%H:%M'))
flush.console() 
}

Are there any reasons this might be a bad idea?

这可能是个坏主意,有什么原因吗?

/edit: even better (thanks @Greg Snow):

/编辑:更好(感谢@Greg Snow):

while (1) {
cat('\r',format(Sys.time(),'%H:%M:%S'))
flush.console() 
}

#3


4  

Sure you can:

当然,您可以:

while(1) {
  cat("\b\b\b\b\b\b\b\b",format(Sys.time(), "%H:%M:%S"),sep="")
}

#4


1  

I do not think overwriting is possible on the console. There is no backspace escape sequence. The progress bar can be drawn because the cat function will not emit a cr unless told to do so.

我认为在控制台上不可能写得过多。没有后空转义序列。可以绘制进度条,因为cat函数不会发出cr,除非被告知这样做。

Edit: I was wrong. The backspace character is recognized:

编辑:我错了。退格字符识别:

for (i in 1:1000) {
     cat(as.character(Sys.time())) 
     flush.console() 
     for(i in 1:19) {cat("\8")} }