在 R 中显示状态消息

发布于 2024-10-13 00:17:08 字数 159 浏览 8 评论 0原文

我想编写一个函数,向用户呈现一条状态消息,显示时间、完成百分比和进程的当前状态等信息。我可以处理组装消息,但我想做的不仅仅是打印到控制台并让它向上滚动,一条消息接着一条消息。我真的希望消息能够改变,而无需像 message() 那样滚动,并且没有任何图形。这可以用 R 实现吗?

I'd like to write a function that presents to the user a status message that shows something like the time, the percent complete, and the current status of a process. I can handle assembling the message, but I'd like to do something other than just print to the console and have it scroll up, one message after the other. I'd really like the message to change without scrolling like message() and without any graphics. Is this possible with R?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

柏林苍穹下 2024-10-20 00:17:08

像这样的事情怎么样?

for(i in 1:10) {
  Sys.sleep(0.2)
  # Dirk says using cat() like this is naughty ;-)
  #cat(i,"\r")
  # So you can use message() like this, thanks to Sharpie's
  # comment to use appendLF=FALSE.
  message(i,"\r",appendLF=FALSE)
  flush.console()
}

How about something like this?

for(i in 1:10) {
  Sys.sleep(0.2)
  # Dirk says using cat() like this is naughty ;-)
  #cat(i,"\r")
  # So you can use message() like this, thanks to Sharpie's
  # comment to use appendLF=FALSE.
  message(i,"\r",appendLF=FALSE)
  flush.console()
}
把人绕傻吧 2024-10-20 00:17:08

utils 包包含 txtProgressBar 和更新它的函数,可用于显示进程的完成百分比。

有关如何更新的示例,请参阅在调用 txtProgressBar 期间创建的 up1up2up3 函数无需滚动控制台即可处理。

The utils package contains the txtProgressBar and functions for updating it which can be used to show the percent complete of a process.

See the up1, up2 and up3 functions that are created during a call to txtProgressBar for examples of how updates are handled without scrolling the console.

作死小能手 2024-10-20 00:17:08

这里有一些 bling bling 的东西。来自 ?tcltk::tkProgressBar

pb <- tkProgressBar("test progress bar", "Some information in %",
        0, 100, 50)
Sys.sleep(0.5)
u <- c(0, sort(runif(20, 0 ,100)), 100)
for(i in u) {
    Sys.sleep(0.1)
    info <- sprintf("%d%% done", round(i))
    setTkProgressBar(pb, i, sprintf("test (%s)", info), info)
}
Sys.sleep(5)
close(pb)

替代文本

Here's some bling bling. From ?tcltk::tkProgressBar.

pb <- tkProgressBar("test progress bar", "Some information in %",
        0, 100, 50)
Sys.sleep(0.5)
u <- c(0, sort(runif(20, 0 ,100)), 100)
for(i in u) {
    Sys.sleep(0.1)
    info <- sprintf("%d%% done", round(i))
    setTkProgressBar(pb, i, sprintf("test (%s)", info), info)
}
Sys.sleep(5)
close(pb)

alt text

箹锭⒈辈孓 2024-10-20 00:17:08

可能有更优雅的方法来做到这一点,但这可以做到这一点:

test.message <- function() {
  for (i in 1:9){
    cat(i)
    Sys.sleep(1)
    cat("\b")
  }

}

如果您自动生成消息,您将需要计算要输出多少个 \b 字符以备份正确的数量,但这非常简单。

There may be more elegant ways to do this, but this could do it:

test.message <- function() {
  for (i in 1:9){
    cat(i)
    Sys.sleep(1)
    cat("\b")
  }

}

If you're automatically generating your message, you'll need to calculate how many \b characters to output to back up the correct amount, but that's pretty straightforward.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文