How to create a dynamic progress bar in R?

Asked

Viewed 445 times

2

To follow the processing status in my routines I use the progress bars of the package pbapply, but I couldn’t find a dynamic way to follow more than one process. To illustrate the problem I considered a list containing three elements of different dimensions and want to join them taking into account the common element between them (ID):

 lista<-list(A=data.frame(ID=1:100, Med=rnorm(100,50,100)),
        B=data.frame(ID=1:50, Med=rnorm(100,50,50)),
        C=data.frame(ID=51:100, Med=rnorm(100,50,25)))

 result<-data.frame(ID=1:100,matrix(NA,nrow = 100,ncol = length(lista)))

 for(t in 1:length(lista)){
     local<-dim(lista[[t]])[1]
     for(t2 in 1:local){
         posilocal<-which(result$ID==lista[[t]]$ID[t2])
         result[posilocal,(t+1)]<-lista[[t]]$Med[t2]
     }
  }

How can I create a progress bar that shows the situation of the process, considering the evolution of t and t2?

1 answer

1


It is not ideal, but you can try to do so by using the package progress.

library(progress)
total <- sum(sapply(lista, function(x) dim(x)[1]))
pb <- progress_bar$new(total = total)
for(t in 1:length(lista)){
  local<-dim(lista[[t]])[1]
  for(t2 in 1:local){
    posilocal<-which(result$ID==lista[[t]]$ID[t2])
    result[posilocal,(t+1)]<-lista[[t]]$Med[t2]
    Sys.sleep(3/100)
    pb$tick()
  }
}

First calculate the total size of the loop and then make a slider that goes through it like this.

It seems to be more complicated to put a bar that appears for each process. This is a limitation of Rstudio as I understand it. Read this discussion can help you understand. It seems that the multi-bar support is in the package plans progress.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.