Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

R: Text progress bar in for loop

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?

like image 550
kurdtc Avatar asked Nov 13 '14 22:11

kurdtc


People also ask

How do I add a progress bar in R?

The txtProgressBar function The most common functions used to add a progress bar in R are the txtProgressBar and setTxtProgressBar functions from R base. In the following block of code we show you how to set a progress bar inside a for loop, briefly describing the different arguments that you can customize.

How do I use txtProgressBar?

Create a progress bar for a loop using the txtProgressBar function, then use the setTxtProgressBar command within the loop to print the progress bar, which updates as the loop progresses. min – The minimum value in the loop. max – The maximum value in the loop.

How do I show progress in R?

A progress indicator for the R console is what you need. The txtProgressBar() command can help you here. The command allows you to set-up a progress indicator that displays in the R console and shows your progress towards the “end point”.

How do I add a shiny progress bar in R?

The simplest way to add a progress indicator is to put withProgress() inside of the reactive() , observer() , or renderXx() function that contains the long-running computation.


3 Answers

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

like image 166
OganM Avatar answered Oct 31 '22 11:10

OganM


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%
like image 23
rawr Avatar answered Oct 31 '22 11:10

rawr


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)
} 
like image 5
Huanyuan Zhang-Zheng Avatar answered Oct 31 '22 12:10

Huanyuan Zhang-Zheng