Yang Yang - 7 months ago 33

R Question

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)

Answer

This should work:

```
mydata=matrix(rnorm(6000*300),ncol = 300)
result=as.data.frame(matrix(nrow = 6000,ncol = 2))
progression<-winProgressBar(title = "Progress bar", min = 0,max = 6000 , width = 300)
for (i in 1:6000) {
abc=MannKendall(mydata[i,])
result[i,1]=abc$tau
result[i,2]=abc$sl
progress=i
setWinProgressBar(progression, progress, title=paste(round(progress/6000)*100,"% done"))
}
```

Source (Stackoverflow)