目录
3.创建Windows样式进度条:winProgressBar函数
4.具有tkProgressBar功能的TK进度条(类似Unix)
前言
在运行for循环时,想要知道当前程序的运行进度,目前
- (1)可以在循环体中加入print(i)来展示当前程序运行到第几次循环;
- (2)在R中通过设置进度条在Console展示当前for循环的运行进度。
本文主要介绍如何在R中设置进度条来检查for循环的进度。
需要两个R函数txtProgressBar和setTxtProgressBar,这两个R函数搭配for循环使用。使用结构如下:
# 使用style=1的进度条
pb <- utils::txtProgressBar(min = 0, max = 10, style = 1)
for(i in 1:10) {
Sys.sleep(1) # 模拟任务执行时间
utils::setTxtProgressBar(pb, i) # 更新进度条的值
}
一、txtProgressBar系列函数
1.txtProgressBar函数描述:
- 在txtProgressBar函数中,"txt"表示文本(text)的缩写。这是因为txtProgressBar函数创建的进度条是以文本形式显示在终端或控制台窗口中的,而不是以图形的方式显示。进度条通过一系列文本字符来表示当前任务的进度,可以根据进度的变化来更新进度条的显示。
-
在txtProgressBar函数中,"ProgressBar"表示进度条的意思。进度条是一种用于显示任务执行进度的图形化元素。它通常由一个水平的条形图和一个指示当前进度位置的滑块或标记组成。
-
在txtProgressBar函数中,进度条被表示为一个文本形式的图形元素,通过一系列文本字符来模拟进度条的外观。通过更新进度条的值,可以动态地改变进度条的显示,以反映任务的进度
2.txtProgressBar()参数介绍:
- txtProgressBar函数有以下参数:
- 1. min:进度条的最小值,默认为0。
- 2. max:进度条的最大值,默认为100。
- 3. initial:进度条的初始值,默认为0。
- 4.style:进度条的样式,可以设置1、2、3.(注意,这里1和2我在电脑运行中没有看出差别来,3的话,会在进度条的尽头同时同步一个百分数,用来表示目前进度已经进展到哪一步。)
- 5. width:进度条的宽度,默认为40。宽度指的是进度条的字符数。
- 6.char:设置进度条的文本字符形式。默认是“=”。
2.1 参数width
进度条的宽度,默认为40。宽度指的是进度条的字符数。设置不同的width效果如下:
2.2参数max
-
在`txtProgressBar`函数中,`max`参数用于设置进度条的最大值。它决定了进度条的长度和任务的完成度。
-
`max`参数的取值应该是一个非负数,表示任务的最大值或总量。通常情况下,你可以根据任务的特性和需求来确定最大值。
-
例如,如果你有一个需要处理100个文件的任务,你可以将`max`参数设置为100,表示任务的总量是100个文件。
2.3参数Style
pb <- txtProgressBar(min = 0, # Minimum value of the progress bar
max = n_iter, # Maximum value of the progress bar
style = 3, # Progress bar style (also available style = 1 and
# style = 2)
width = 50, # Progress bar width. Defaults to getOption("width")
char = "=") # Character used to create the bar
for(i in 1:50) {
Sys.sleep(0.1) # Remove this line and add your code
setTxtProgressBar(pb, i) # Sets the progress bar to the current state
}
close(pb) # Close the connection
其中
- 这段代码是在R语言中使用的函数,用于暂停程序的执行,以便等待一段时间。具体来说,Sys.sleep(0.1)表示暂停程序的执行0.1秒。这可以用于在程序中创建延迟,以便等待其他操作完成或在两个操作之间添加一定的时间间隔。
- style=1效果
- style=2效果
- style=3效果
style = 1和style = 2只是显示字符形式的进度条,没有进度条后面的百分比。它们的不同之处在于,style = 2每次都会重画线,这在其他代码可能正在写入R控制台时非常有用。style = 3用|标记范围的结束,并在条形的右侧给出一个百分比。
注意:Style=1和2我没有发现这两个参数的区别。
3.setTxtProgressBar()
函数功能描述:在for循环体内,每结束一次循环,通过setTxtProgressBar()函数来更新console中的进度条,知道所有的循环都结束!它的参数设置同txtProgressBar()。
4.close(pb)函数:关闭与进度条对象的连接
在使用txtProgressBar函数创建进度条对象后,当任务完成或不再需要进度条时,可以使用close(pb)函数关闭与进度条对象的连接。
关闭进度条对象的连接有以下几个原因:
- 1. 释放资源:进度条对象占用系统资源,包括内存和处理器资源。关闭进度条对象可以释放这些资源,以便其他任务可以使用。
- 2. 清理界面:进度条对象在控制台或终端窗口中显示进度条,关闭进度条对象后,可以清理控制台或终端窗口中的进度条显示,使界面更整洁。
- 3. 结束进度条更新:关闭进度条对象后,进度条将不再更新。这可以防止进度条在不需要时继续更新,从而减少不必要的计算和刷新。
总之,通过关闭与进度条对象的连接,可以释放资源、清理界面并停止进度条的更新,以提高系统性能并提供更好的用户体验。
可以不关闭进度条对象的连接吗?
- 是的,可以选择不关闭与进度条对象的连接。关闭与进度条对象的连接只是为了释放资源、清理界面和停止进度条的更新,这些并不是强制性的步骤。
二、其他一些可以实现进度条功能的函数
1.提高txtProgressBar函数产生进度条的性能
描述:在txtProgressBar函数自身的基础上,进一步显示已用时间和估计剩余时间
为了提供进一步的功能,我们开发了一种方法来显示经过的时间和剩余时间的估计,基于先前迭代运行的平均时间。请注意,为了显示小时、分钟和秒,我们使用了lubridate
包中的seconds_to_period
函数。
# install.packages("lubridate")
library(lubridate)
n_iter <- 20
pb <- txtProgressBar(min = 0,
max = n_iter,
style = 3,
width = n_iter, # Needed to avoid multiple printings
char = "=")
init <- numeric(n_iter)
end <- numeric(n_iter)
for(i in 1:n_iter){
init[i] <- Sys.time()
Sys.sleep(0.1) # Remove this line and add your code
end[i] <- Sys.time()
setTxtProgressBar(pb, i)
time <- round(seconds_to_period(sum(end - init)), 0)
# Estimated remaining time based on the mean time that took to run the previous
#iterations
est <- n_iter * (mean(end[end != 0] - init[init != 0])) - time
remainining <- round(seconds_to_period(est), 0)
cat(paste(" // Execution time:", time,
" // Estimated time remaining:", remainining), "")
}
close(pb)
运行效果:
可以看到运行结果由3部分组成:进度条,已经运行的时间,剩余运行时间。
2.progress包也提供了进度条功能
作为前面功能的替代,可以使用progress
包,它允许进一步定制。
有几个参数,您可以自定义,在Console中键入?progress
以获取更多详细信息,但最相关的是format
参数,它允许根据需要格式化进度条,具有不同类型的配置。
以下代码块显示了一个完整的示例,其中包含微调器、条形图、完成百分比、经过的时间和估计的剩余时间:
# install.packages("progress")
library(progress)
n_iter <- 100
pb <- progress_bar$new(format = "(:spin) [:bar] :percent [Elapsed time: :elapsedfull || Estimated time remaining: :eta]",
total = n_iter,
complete = "=", # Completion bar character
incomplete = "-", # Incomplete bar character
current = ">", # Current bar character
clear = FALSE, # If TRUE, clears the bar when finish
width = 100) # Width of the progress bar
for(i in 1:n_iter) {
# Updates the current state
pb$tick()
#---------------------
# Code to be executed
#---------------------
Sys.sleep(0.1) # Remove this line and add your code
#---------------------
}
运行结果:
3.创建Windows样式进度条:winProgressBar函数
在前面的部分中,我们展示了如何创建文本进度条,它将在控制台中打印。但是,如果愿意,您也可以使用winProgressBar
和setWinProgressBar
函数创建一个Windows样式的进度条。
添加它的过程类似于前面的例子,但在这种情况下也可以添加标题和标签。注意,如果在setWinProgressBar
函数上指定标签或标题,它将覆盖winProgressBar
函数的相应参数。
n_iter <- 50 # Number of iterations
pb <- winProgressBar(title = "Windows progress bar", # Window title
label = "Percentage completed", # Window label
min = 0, # Minimum value of the bar
max = n_iter, # Maximum value of the bar
initial = 0, # Initial value of the bar
width = 300L) # Width of the window
for(i in 1:n_iter) {
#---------------------
# Code to be executed
#---------------------
Sys.sleep(0.1) # Remove this line and add your code
#---------------------
pctg <- paste(round(i/n_iter *100, 0), "% completed")
setWinProgressBar(pb, i, label = pctg) # The label will override the label set on the
# winProgressBar function
}
close(pb) # Close the connection
运行结果:
4.具有tkProgressBar功能的TK进度条(类似Unix)
上一个小节讨论的函数仅适用于Windows设备。但是,tcltk
包中的tkProgressBar
和setTkProgressBar
函数将允许您创建类似Unix平台上的Tk进度条,如Linux:
# install.packages("tcltk")
library(tcltk)
n_iter <- 50 # Number of iterations
pb <- tkProgressBar(title = "Tk progress bar", # Window title
label = "Percentage completed", # Window label
min = 0, # Minimum value of the bar
max = n_iter, # Maximum value of the bar
initial = 0, # Initial value of the bar
width = 300) # Width of the window
for(i in 1:n_iter) {
#---------------------
# Code to be executed
#---------------------
Sys.sleep(0.1) # Remove this line and add your code
#---------------------
pctg <- paste(round(i/n_iter *100, 0), "% completed")
setTkProgressBar(pb, i, label = pctg)
}
close(pb) # Close the connection
运行效果:
5.带有应用功能的进度条"pbapplication"包
讨论如何使用pbapply
包,它将进度条添加到应用系列函数中。
# install.packages("pbapply")
library(pbapply)
pblapply(1:3, function(i){
Sys.sleep(1) # Remove this lines and add your code
i ^ 3 # and add your code
})
运行效果:
此外,您还可以自定义显示的进度条的类型与pboptions
函数的type
参数。可能的值是"timer"
(默认),它显示一个进度条与估计的剩余和经过的时间,"txt"
,它删除了时间,"win"
,它设置了一个Windows进度条,"tk"
为一个Tk进度条和"none"
,以避免显示的Bars
# Save the current options
op <- pboptions()
# Setting a new bar type
pboptions(type = "win")
pbsapply(1:3, Sys.sleep)
# Back to default
pboptions(op)
参考:
PROGRESS BAR in R ▰▰▱ [add Text, Windows and Tk R progress bars]
Creating a progress bar in R | Joseph Crispell (这个博文给出了For循环想要知道目前程序运行到哪里的几种办法,并且配合结果以动画显示,内容简单易懂。强烈推荐)
第一部分txtProgressBar()函数部分内容来自ChatGpt.(可能存在不准确的内容。)