R在控制台中显示两个进度条堆叠(带代码) [英] R display in console two progress bars stacked (with code)

查看:246
本文介绍了R在控制台中显示两个进度条堆叠(带代码)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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



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


  1. 如果可能的话,我想使用进度包的progress_bar(如果不可能的话,基地或任何解决方案很酷!:))

  2. 我想留在控制台,而不是用tk或这样的解决方案绘制进度条( https://www.r-bloggers.com/multiple-progress-bars/



下面的例子模拟期望的功能和结果在进展中(首选)和Base R:

  ###示例包(首选)
库(p rogress)

###创建进度条
overallbar< - progress_bar $ new(
format =下载:what [:bar]:percent估计剩余的总数:: eta,
clear = FALSE,total = 1000,width = 60)
statusbar< - progress_bar $ new(
format =[:bar]:percent估计剩余的CURRENT FILE:: eta,
clear = FALSE,total = 100,width = 60)

###只显示状态栏,当我想显示

(我在1:1000){
overallbar $ tick()
statusbar $ tick()
Sys.sleep(1/1000)
}

###预期结果(仅限仿制)
现在下载此文件[] 100%估计剩余总体:0s
[============] 100%估计剩余CURRENT FILE:0s

使用BASE R INSTEAD的示例:

  ###基准R 
pb1 < - txtProgressBar(1,1000,style = 3)
pb2< - txtPro gressBar(title =File Progress,1,100,style = 3)


###和进度一样,base也只显示第二个进度条

cat(OVERALL PROGRESS:)
for(i in 1:1000){
setTxtProgressBar(pb1,i)
###东西对标题选项来说很时髦,不工作
###我通常使用进度包,你会明白,我想要一个标题
setTxtProgressBar(title =File Progress:,pb2,i)
Sys.sleep(1 / 1000)
}

###预期结果(仅限仿制)

总体进展:
| ========= ================================================== ============================= | 100%
文件进度:
| ==================================== ================================================== == | 100%


解决方案

酒吧通过简单地重新绘制同一条线而工作。您可以使用ANSI转义序列在行之间跳转。修改你的Base R例子,

  cat(OVERALL PROGRESS:\\\
\\\
)#2 newlines留空在标题之间
cat(File Progress:\\\
)#1换行符留在pb2
中(i在1:1000){
cat(\033 [2A )#向上2行pb1
setTxtProgressBar(pb1,i)
cat(\033 [2B)#向下2行pb2
setTxtProgressBar(title =File Progress: ,pb2,i)
Sys.sleep(1/1000)
}
cat(\033 [2B\\\
)#for good measure

这适用于 Linux ,可能还包括MacOS Terminal和甚至可能是Windows ,尽管我没有在那里测试。


IS IT POSSIBLE TO DO THIS? I'd like to display two progress bars at the same time, vertically stacked, in R as a function runs within another larger function.

One would be for the overall process status of the larger of the function, the other for individual processes within the function (downloads etc.).

  1. I'd like to use the progress package's progress_bar's if possible (if not possible, base or whatever solution is cool! :) )

  2. I'd like to stay in the console and not plot a progress bar with tk or a solution like this(https://www.r-bloggers.com/multiple-progress-bars/)

Thanks in advance!

Examples below of imitated desired functions and outcomes in both progress(preferred) and Base 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

AN EXAMPLE USING BASE R INSTEAD (less desired than progress package style):

###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%

解决方案

At least in the Base R case, progress bars work by simply re-drawing the same line. You can use ANSI escape sequences to jump between lines. Modifying your Base R example,

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

This works in Linux, probably also MacOS Terminal, and maybe even Windows although I did not test there.

这篇关于R在控制台中显示两个进度条堆叠(带代码)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆