R在控制台中显示两个堆叠的进度条(带代码)

标签 r function console progress-bar progress

是否有可能做到这一点?我想在 R 中同时显示两个垂直堆叠的进度条,因为一个函数在另一个更大的函数中运行。

一个用于较大函数的整体进程状态,另一个用于函数内的单个进程(下载等)。

  • 如果可能,我想使用进度包的 progress_bar(如果不可能,基础或任何解决方案都很酷!:))
  • 我想留在控制台中,而不是用 tk 或这样的解决方案绘制进度条(https://www.r-bloggers.com/multiple-progress-bars/)

  • 提前致谢!

    下面的例子在进度(首选)和基础 R 中模拟的所需功能和结果:
     ###Example in progress package(preferred)
    library(progress)
    
    ###Create the progress bars
    overallbar <- progress_bar$new(
      format = " downloading :what [:bar] :percent Guestimated Remaining OVERALL: :eta",
      clear = FALSE, total = 1000, width = 60)
    statusbar <- progress_bar$new(
      format = " [:bar] :percent Guestimated Remaining CURRENT FILE: :eta",
      clear = FALSE, total = 100, width = 60)
    
    ###Only displays the statusbar when I'd like to display both
    
    for (i in 1:1000) {
      overallbar$tick()
      statusbar$tick()
      Sys.sleep(1 / 1000)
    }
    
    ###Desired outcome (imitation only)
    downloading THIS FILE NOW [] 100% Guestimated remaining OVERALL:  0s
    [============] 100% Guestimated remaining CURRENT FILE:  0s
    

    使用 BASE R INSTEAD 的示例(不如进度包样式):
    ###Base R
    pb1   <- txtProgressBar(1, 1000, style=3)
    pb2   <- txtProgressBar(title="File Progress",1, 100, style=3)
    
    
    ###Like progress, base also only displays the second progress bar 
    
    cat("OVERALL PROGRESS:")
    for (i in 1:1000) {
      setTxtProgressBar(pb1, i)
      ###something is funky with the title option, not working
      ###I usually use progress package, you get the idea, I'd like a title
      setTxtProgressBar(title="File Progress:", pb2, i)
      Sys.sleep(1 / 1000)
    }
    
    ###Desired outcome (imitation only)
    
    OVERALL PROGRESS:
    |========================================================================================| 100%
    File Progress:
    |========================================================================================| 100%
    

    最佳答案

    至少在 Base R 的情况下,进度条通过简单地重新绘制同一条线来工作。您可以使用 ANSI 转义序列在行之间跳转。修改您的 Base R 示例,

    cat("OVERALL PROGRESS:\n\n") # 2 newlines leaving a blank between headers
    cat("File Progress:\n") # 1 newline leaves in position for pb2
    for (i in 1:1000) {
      cat("\033[2A")   # up 2 lines for pb1
      setTxtProgressBar(pb1, i)
      cat("\033[2B")   # down 2 lines for pb2
      setTxtProgressBar(title="File Progress:", pb2, i)
      Sys.sleep(1 / 1000)
    }
    cat("\033[2B\n") # for good measure
    

    这适用于 Linux ,可能还有 MacOS 终端,和 maybe even Windows虽然我没有在那里测试。

    关于R在控制台中显示两个堆叠的进度条(带代码),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49205946/

    相关文章:

    r - R 中意外的快速排序行为

    r - 错误: Mapping should be created with `aes() or ` aes_()`

    r - 任何/所有的有效版本

    java - 如何创建定时器功能?

    objective-c - 如何轻松重定向 NSTextView 中的控制台输出?

    c++ - 控制台键盘命中检测和解释

    r - 执行计算时将单个数据框行拆分为多行

    javascript - 在确认对话框中单击 'OK' 后如何停止功能?

    javascript - 如何重置游戏分数和按钮文本

    haskell - Haskell 中具有多个参数的新类型