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

问题描述:

我需要使用循环处理一长串图像。处理所有事情需要相当长的时间,因此我想跟踪进展情况。R - 如何在循环列表中打印进度?

这是我的循环:

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

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

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

任何想法?提前致谢。

+1

我固然不知道R,但是我希望你可以只是之前在for循环定义一个变量,将其初始化为'1',并在循环内打印变量的值,然后递增。 – Joseph 2015-02-05 22:34:12

+1

'reproj'是什么?你的代码是不可复制的 – DatamineR 2015-02-05 22:36:56

+1

你可以循环使用数字'for(in 1:length(file.list))'而不是数值,然后设置'x = file.list [i]'作为循环的第一步。 – Frank 2015-02-05 22:44:03

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)) 
} 
+0

很酷,我不知道信息功能! – thiagoveloso 2015-02-05 22:54:30

可以使用系统窗口进度条下:

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

感谢您的建议,但此答案仅适用于Windows系统。大部分时间我在Mac上工作,大约有1%的时间在Linux系统上工作。 – thiagoveloso 2015-11-25 07:40:19

+1

在这种情况下,要获得GUI进度条,可以使用tcltk包中的tkProgressBar()函数。 – 2015-11-25 07:48:18

+0

再次感谢,我会看看。 – thiagoveloso 2015-11-25 07:57:23