Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

R - How to print progress in a loop over list?

I need to process a long list of images using a loop. It takes a considerable time to run everything, and therefore I would like to keep track of the progress.

This is my loop:

files.list <- c("LC82210802013322LGN00_B1.TIF", "LC82210802013322LGN00_B10.TIF", 
"LC82210802013322LGN00_B11.TIF", "LC82210802013322LGN00_B2.TIF", 
"LC82210802013322LGN00_B3.TIF", "LC82210802013322LGN00_B4.TIF", 
"LC82210802013322LGN00_B5.TIF", "LC82210802013322LGN00_B6.TIF", 
"LC82210802013322LGN00_B7.TIF", "LC82210802013322LGN00_B8.TIF", 
"LC82210802013322LGN00_B9.TIF", "LC82210802013322LGN00_BQA.TIF", 
"LC82210802013354LGN00_B1.TIF", "LC82210802013354LGN00_B10.TIF", 
"LC82210802013354LGN00_B11.TIF", "LC82210802013354LGN00_B2.TIF", 
"LC82210802013354LGN00_B3.TIF", "LC82210802013354LGN00_B4.TIF", 
"LC82210802013354LGN00_B5.TIF", "LC82210802013354LGN00_B6.TIF", 
"LC82210802013354LGN00_B7.TIF", "LC82210802013354LGN00_B8.TIF", 
"LC82210802013354LGN00_B9.TIF", "LC82210802013354LGN00_BQA.TIF", 
"LC82210802014021LGN00_B1.TIF", "LC82210802014021LGN00_B10.TIF", 
"LC82210802014021LGN00_B11.TIF", "LC82210802014021LGN00_B2.TIF", 
"LC82210802014021LGN00_B3.TIF", "LC82210802014021LGN00_B4.TIF", 
"LC82210802014021LGN00_B5.TIF", "LC82210802014021LGN00_B6.TIF", 
"LC82210802014021LGN00_B7.TIF", "LC82210802014021LGN00_B8.TIF", 
"LC82210802014021LGN00_B9.TIF", "LC82210802014021LGN00_BQA.TIF", 
"LC82210802014037LGN00_B1.TIF", "LC82210802014037LGN00_B10.TIF", 
"LC82210802014037LGN00_B11.TIF", "LC82210802014037LGN00_B2.TIF", 
"LC82210802014037LGN00_B3.TIF", "LC82210802014037LGN00_B4.TIF", 
"LC82210802014037LGN00_B5.TIF", "LC82210802014037LGN00_B6.TIF", 
"LC82210802014037LGN00_B7.TIF", "LC82210802014037LGN00_B8.TIF", 
"LC82210802014037LGN00_B9.TIF", "LC82210802014037LGN00_BQA.TIF", 
"LC82210802014085LGN00_B1.TIF", "LC82210802014085LGN00_B10.TIF", 
"LC82210802014085LGN00_B11.TIF", "LC82210802014085LGN00_B2.TIF", 
"LC82210802014085LGN00_B3.TIF", "LC82210802014085LGN00_B4.TIF", 
"LC82210802014085LGN00_B5.TIF", "LC82210802014085LGN00_B6.TIF", 
"LC82210802014085LGN00_B7.TIF", "LC82210802014085LGN00_B8.TIF", 
"LC82210802014085LGN00_B9.TIF", "LC82210802014085LGN00_BQA.TIF"
)

for (x in files.list) { #loop over files

  # Tell about progress
  cat('Processing image', x, 'of', length(files.list),'\n')
}

Of course, instead of showing the name of the file, I would like to show the index of the current file in the context of the length of the entire list.

I really need the names of the files within the loop, because I need to load and save a new version of each one of them.

Any ideas? Thanks in advance.

like image 545
thiagoveloso Avatar asked Feb 05 '15 22:02

thiagoveloso


3 Answers

for (x in 1:length(files.list)) { #loop over files

 # doing something on x-th file =>      files.list[x]


  # Tell about progress
  cat('Processing image', x, 'of', length(reproj),'\n')
}
like image 116
Marcin Kosiński Avatar answered Sep 25 '22 10:09

Marcin Kosiński


for (i in 1:length(files.list)) {
  x <- files.list[i]
  # do stuff with x
  message('Processing image ', i, ' of ', length(files.list))
}
like image 39
Chris Merkord Avatar answered Sep 21 '22 10:09

Chris Merkord


You can use system window progress bar as under:

 # put this before start of loop
    total = length of your loop

    # put this before closing braces of loop
    pb <- winProgressBar(title = "progress bar", min = 0, max =total , width = 300)

          Sys.sleep(0.1)
# Here i is loop itrator
          setWinProgressBar(pb, i, title=paste( round(i/total*100, 0),"% done"))

    # put this after closing braces of loop
    close(pb)
like image 25
Mithilesh Kumar Avatar answered Sep 25 '22 10:09

Mithilesh Kumar