Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to show the progress of code in R?

Tags:

r

progress-bar

I am now dealing with a large dataset and some functions may take hours to process. I wonder how I can show the progress of the code through a progress bar or number(1,2,3,...,100). Here is an example. Thanks.

require(Kendall)
mydata=matrix(rnorm(6000*300),ncol = 300)
result=as.data.frame(matrix(nrow = 6000,ncol = 2))
for (i in 1:6000) {
  abc=MannKendall(mydata[i,])
  result[i,1]=abc$tau
  result[i,2]=abc$sl
}

By the way, I find the link https://ryouready.wordpress.com/2009/03/16/r-monitor-function-progress-with-a-progress-bar/ very usefull. However, I do not know how to combine the code in the link with my own function. Anyone has an idea? Thanks. Here is the code from the above link.

total <- 20
# create progress bar
pb <- txtProgressBar(min = 0, max = total, style = 3)
for(i in 1:total){
   Sys.sleep(0.1)
   # update progress bar
   setTxtProgressBar(pb, i)
}
close(pb)
like image 585
Yang Yang Avatar asked Nov 18 '16 16:11

Yang Yang


1 Answers

You could add an if statement to output every 100 iterations or so

for (i in 1:6000) {
  abc=MannKendall(mydata[i,])
  result[i,1]=abc$tau
  result[i,2]=abc$sl
  if(i %% 100 == 0){
    cat(i)
    cat("..")
  }
}

which gives you output of

100..200..300..400..
like image 152
MeetMrMet Avatar answered Sep 20 '22 17:09

MeetMrMet