R - 如何在列表循环中打印进度?

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

我需要使用循环处理一长串图像。 运行 一切都需要相当长的时间,因此我想跟踪进度。

这是我的循环:

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')
}

当然,我不想显示文件名,而是想在整个列表长度的上下文中显示当前文件的索引。

我真的需要循环中文件的名称,因为我需要加载和保存每个文件的新版本。

有什么想法吗?提前致谢。

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')
}
for (i in 1:length(files.list)) {
  x <- files.list[i]
  # do stuff with x
  message('Processing image ', i, ' of ', length(files.list))
}

您可以使用系统 window 进度条,如下所示:

 # 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)