Adding progress bar to replicate function in R
Asked Answered
L

2

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.

Lorimer answered 2/8, 2017 at 9:19 Comment(3)
you could add some sort of indicator in analyse function along with Sys.sleep(1) to explicitly print progressDyarchy
That is one solution, but I would rather have in the form of a progress bar if possibleLorimer
further we can use the indicator i mentioned as global variable and access it using txtProgressBar or other progress indicator at some time intervalsDyarchy
L
11

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

Lorimer answered 17/1, 2018 at 9:43 Comment(0)
O
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
Otherworld answered 2/8, 2017 at 10:36 Comment(2)
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.Lorimer
I did not notice the changes. Can you post a reproducible replicate example?Otherworld

© 2022 - 2024 — McMap. All rights reserved.