Reputation: 1621
I have some sample code which contains a for loop and creates some plots like this (my actual data creates several thousands of plots):
xy <- structure(list(NAME = structure(c(2L, 3L, 1L, 1L), .Label = c("CISCO","JOHN", "STEPH"), class = "factor"), ID = c(41L, 49L, 87L, 87L), X_START_YEAR = c(1965L, 1948L, 1959L, 2003L), Y_START_VALUE = c(940L,-1760L, 110L, 866L), X_END_YEAR = c(2005L, 2000L, 2000L, 2007L), Y_END_VALUE = c(940L, -1760L, 110L, 866L), LC = structure(c(1L,1L, 2L, 2L), .Label = c("CA", "US"), class = "factor")), .Names = c("NAME", "ID", "X_START_YEAR", "Y_START_VALUE", "X_END_YEAR", "Y_END_VALUE","LC"), class = "data.frame", row.names = c(NA, -4L))
ind <- split(xy,xy$ID) # split by ID for different plots
# Plots
for (i in ind){
xx = unlist(i[,grep('X_',colnames(i))])
yy = unlist(i[,grep('Y_',colnames(i))])
fname <- paste0(i[1, 'ID'],'.png')
png(fname, width=1679, height=1165, res=150)
par(mar=c(6,8,6,5))
plot(xx,yy,type='n',main=unique(i[,1]), xlab="Time [Years]", ylab="Value [mm]")
i <- i[,-1]
segments(i[,2],i[,3],i[,4],i[,5],lwd=2)
points(xx, yy, pch=21, bg='white', cex=0.8)
dev.off()
}
To see the progress of the for loop I would be interested to incorporate a progress bar to my code. As I found from the R documentation there is the txtProgressBar
http://stat.ethz.ch/R-manual/R-patched/library/utils/html/txtProgressBar.html
From the example of that page I understand that you have to write the for loop into a function to call it afterwards which I am struggling with my example.
How could I implement a progress bar into the for loop?
Upvotes: 51
Views: 68396
Reputation: 346
Now there is an easier solution by package svMisc
, simply put progress()
in your for loop, for example
library(svMisc)
for (i in 1:n){
# <YOUR CODES HERE>
progress(i)
}
As suggested below, if you got a long for loop, you will need to add n:
library(svMisc)
for (i in 1:n){
# <YOUR CODES HERE>
progress(i,n)
}
Upvotes: 6
Reputation: 310
I came across the same problem - I wanted to add a progress bar for my for
loop. As suggested above, using svMisc
is probably the easiest way to add a progress bar to your loop.
Nevertheless, in large n
s, it falls apart if you don't add a max.value
.
library(svMisc)
n=100
for (i in 1:n){
# <YOUR CODES HERE>
progress(i)
}
The above example will probably work well, but if you increase n
as in the following example
library(svMisc)
n=1031
for (i in 1:n){
# <YOUR CODES HERE>
progress(i)
}
The progress bar will get to 100, while R will still be working. In order to avoid that, you have to specify the ending value of the loop, as in the following.
for (i in 0:1031) {
progress(i, 1031)
Sys.sleep(0.02)
if (i == 1031) message("Done!")
}
That worked for me. For more information, see svMisc
documentation here.
Upvotes: 7
Reputation: 2663
for progress bar to work you need a number to track your progress. that is one of the reasons as a general rule I prefer using for with (i in 1:length(ind))
instead of directly putting the object I want there. Alternatively you'll just create another stepi
variable that you do stepi = stepi + 1
in every iteration.
you first need to create the progressbar object outside the loop
pb = txtProgressBar(min = 0, max = length(ind), initial = 0)
then inside you need to update with every iteration
setTxtProgressBar(pb,stepi)
or
setTxtProgressBar(pb,i)
Remember to close the progress bar to output the newline character. From the documentation:
The progress bar should be closed when finished with: this outputs the final newline character.
Simply add at the end of your loop:
close(pb)
This will work poorly if the loop also has print
commands in it
Upvotes: 67
Reputation: 20811
You could write a very simple one on the fly to show percent completed:
n <- 100
for (ii in 1:n) {
cat(paste0(round(ii / n * 100), '% completed'))
Sys.sleep(.05)
if (ii == n) cat(': Done')
else cat('\014')
}
# 50% completed
Or one to replicate the text bar:
n <- 100
for (ii in 1:n) {
width <- options()$width
cat(paste0(rep('=', ii / n * width), collapse = ''))
Sys.sleep(.05)
if (ii == n) cat('\014Done')
else cat('\014')
}
# ============================
Another one with text bar and percent complete:
options(width = 80)
n <- 100
for (ii in 1:n) {
extra <- nchar('||100%')
width <- options()$width
step <- round(ii / n * (width - extra))
text <- sprintf('|%s%s|% 3s%%', strrep('=', step),
strrep(' ', width - step - extra), round(ii / n * 100))
cat(text)
Sys.sleep(0.05)
cat(if (ii == n) '\n' else '\014')
}
|================================================== | 67%
Upvotes: 27