1

I wrote an R script which does a lot of heavy calculation. The script enters a while loop and proceeds it around 16 times (which lasts between 3 and 5 minutes a loop in average).

I would like to be able to "track" the calculation progresses: be able to see how much has been done and how much remains to be done. So I just printed the number of the iteration and the calculation time for a finished loop.

What I would like to add is a kind of a "progression bar", which I represent by a ggplot2 histogram. Every time a calculation is finished, I update the plot (the "finished" area grows bigger and the "remaining" area subsequently diminishes), and I print it. But, no plot shows in the "plot" area of R studio while the function is still executing. All plots appears once the overall calculation (of the 16 loops) is finished.

So my question is: Is it possible to display a plot at the end of a while loop, even if the "overall" calculation is not over?

If I am not being clear, please tell me.

Thank you in advance for your answers.

EDIT: Sys.sleep() works very well for what I intended.

If anyone is interested in the code, here follows:

# Big calculation

iter <- iter + 1
d <- data.frame(x1 = c(1,1), x2 = c(iter/maxIt, 1 - iter/maxIt), 
                x3 = c("finished", "remaining"))
print(qplot(d$x1, d$x2, fill = factor(d$x3), geom = "histogram", stat = "identity"))
Sys.sleep(1)

# Preparing the next loop
pierrez
  • 91
  • 1
  • 6

1 Answers1

0
timearray <- NULL # array of time used by each iteration
for (wait in c(3,2,5,4,6)) {
    s <- Sys.time() # iteration time start
    Sys.sleep(3) # do your calculation -- I just wait here
    s <- Sys.time() - s # now s is last iteration time
    timearray <- c(timearray, s) # add this new value to array
    plot(timearray, type='h', main='Iteration time') # plot the array
}

Could it be something like that?

  • `Sys.sleep()` works perfectly: it gives R studio enough time to update the plot window before calculating the next loop. – pierrez Sep 30 '14 at 12:17