title: R: Text progress bar in for loop
body: <p>I have some sample code which contains a for loop and creates some plots like this (my actual data creates several thousands of plots):</p>
  
  <pre><code>xy &lt;- 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 &lt;- 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 &lt;- 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 &lt;- 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()
  } 
  </code></pre>
  
  <p>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 <code>txtProgressBar</code> <a href="http://stat.ethz.ch/R-manual/R-patched/library/utils/html/txtProgressBar.html" rel="noreferrer">http://stat.ethz.ch/R-manual/R-patched/library/utils/html/txtProgressBar.html</a>
  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.</p>
  
  <p>How could I implement a progress bar into the for loop?</p>