I am performing two loops in R: the external one is over 5 directories and the internal one is over the 15 files existing in each directory.
My question is: how to display the progress of the task in this scenario?
Please see the following example:
# these are the directories
vars <- c("clt", "hurs", "pr", "tas", "was")
# prepare progress info
pb <- txtProgressBar(min = 0, max = length(vars), style = 3)
# first, loop through variables
for (i in 1:length(vars)){
# do some stuff here: each file takes a bit long to finish
files <- c("was_Amon_CanESM2_historical_r1i1p1_185001-200512.nc", "was_Amon_CanESM2_historical_r1i1p1_185001-200512_SA.nc",
"was_Amon_GFDL-ESM2M_historical_r1i1p1_186101-200512.nc", "was_Amon_GFDL-ESM2M_historical_r1i1p1_186101-200512_SA.nc",
"was_Amon_GISS-E2-H_historical_r1i1p1_185001-200512.nc", "was_Amon_GISS-E2-H_historical_r1i1p1_185001-200512_SA.nc",
"was_Amon_GISS-E2-R-CC_historical_r1i1p1_185001-201012.nc", "was_Amon_GISS-E2-R-CC_historical_r1i1p1_185001-201012_SA.nc",
"was_Amon_GISS-E2-R_historical_r1i1p1_185001-200512.nc", "was_Amon_GISS-E2-R_historical_r1i1p1_185001-200512_SA.nc",
"was_Amon_HadGEM2-AO_historical_r1i1p1_186001-200512.nc", "was_Amon_HadGEM2-AO_historical_r1i1p1_186001-200512_SA.nc",
"was_Amon_HadGEM2-CC_historical_r1i1p1_185912-200511.nc", "was_Amon_HadGEM2-CC_historical_r1i1p1_185912-200511_SA.nc",
"was_Amon_IPSL-CM5A-MR_historical_r1i1p1_185001-200512.nc", "was_Amon_IPSL-CM5A-MR_historical_r1i1p1_185001-200512_SA.nc",
"was_Amon_IPSL-CM5B-LR_historical_r1i1p1_185001-200512.nc", "was_Amon_IPSL-CM5B-LR_historical_r1i1p1_185001-200512_SA.nc",
"was_Amon_MIROC4h_historical_r1i1p1_195001-200512.nc", "was_Amon_MIROC4h_historical_r1i1p1_195001-200512_SA.nc",
"was_Amon_MRI-CGCM3_historical_r1i1p1_185001-200512.nc", "was_Amon_MRI-CGCM3_historical_r1i1p1_185001-200512_SA.nc",
"was_Amon_NorESM1-M_historical_r1i1p1_185001-200512.nc", "was_Amon_NorESM1-M_historical_r1i1p1_185001-200512_SA.nc",
"was_Amon_bcc-csm1-1-m_historical_r1i1p1_185001-201212.nc", "was_Amon_bcc-csm1-1-m_historical_r1i1p1_185001-201212_SA.nc",
"was_Amon_bcc-csm1-1_historical_r1i1p1_185001-201212.nc", "was_Amon_bcc-csm1-1_historical_r1i1p1_185001-201212_SA.nc",
"was_Amon_inmcm4_historical_r1i1p1_185001-200512.nc", "was_Amon_inmcm4_historical_r1i1p1_185001-200512_SA.nc"
)
# loop through files
for (j in files){
# do some stuff here too
# inform progress on files
Sys.sleep(0.05)
setTxtProgressBar(pb, i)
}
}
I am showing here the progress on each directory only. However, because processing all files in a directory takes a long time, I would prefer to show the progress for each one of the 75 files.
However, I can't think of a way of doing this on a nested loop. Does anyone have any idea?
If each directory has 15 files, set the maximum of the progress bar to length(var)*15
. Then set a counter k
which will be incremented after each file:
#outside both loops
pb <- txtProgressBar(min = 0, max = length(vars)*15, style = 3)
k<-0
#inside the j loop just before the progress bar:
k<-k+1
setTxtProgressBar(pb, k)