5

I am using replicate to run my own analyse function multiple times (analyse returns a list):

results <- replicate(reps, analyse())

Is there a way to add progress bar, showing the percentage of replications finished at the moment? I have tried with txtProgressBar, but don't know how to make it work without a for-loop.

EDIT: reproducible example of replicate:

analyse <- function() {
  out <- list('a' = vector('list', 5), 'b' = vector('list', 5))
}
results <- replicate(3, analyse())

In my case, output of analyse is a deep list of lists with results. I would like the progress bar to update every time a new column of results is filled in, so after each replication.

Michal
  • 151
  • 1
  • 7
  • you could add some sort of indicator in `analyse` function along with `Sys.sleep(1)` to explicitly print progress – parth Aug 02 '17 at 09:34
  • That is one solution, but I would rather have in the form of a progress bar if possible – Michal Aug 02 '17 at 09:59
  • further we can use the `indicator` i mentioned as global variable and access it using `txtProgressBar` or other progress indicator at some time intervals – parth Aug 02 '17 at 10:46

2 Answers2

8

The solution is to use pbreplicate() function from pbapply package. This package also contains respective progress bar functions for apply(), lapply() and sapply().

Michal
  • 151
  • 1
  • 7
0

You could try the following example in a for loop

x  <- seq(1,100)
pb   <- txtProgressBar(1, 100, style=3)
StartTime <- Sys.time()
for(i in x){
   Sys.sleep(0.01)
   setTxtProgressBar(pb, i)
}

Sys.time() - StartTime
Prradep
  • 5,506
  • 5
  • 43
  • 84
  • How can I apply this example to `replicate` function? As I have mentioned in my question, I do know how to do it in a for-loop. However, I want to use `replicate`, becauese loops are to slow for my data. – Michal Aug 02 '17 at 11:03
  • I did not notice the changes. Can you post a reproducible `replicate` example? – Prradep Aug 02 '17 at 11:30