[Rcpp-devel] progress bar in for loop

baptiste auguie baptiste.auguie at googlemail.com
Sun Aug 8 16:55:20 CEST 2010


Oops, please ignore my previous request, I found an answer on
StackOverflow. I'm now using this routine,

int progress_bar(double x, double N)
{
    // how wide you want the progress meter to be
    int totaldotz=40;
    double fraction = x / N;
    // part of the progressmeter that's already "full"
    int dotz = round(fraction * totaldotz);

    // create the "meter"
    int ii=0;
    printf("%3.0f%% [",fraction*100);
    // part  that's full already
    for ( ; ii < dotz;ii++) {
        printf("=");
    }
    // remaining part (spaces)
    for ( ; ii < totaldotz;ii++) {
        printf(" ");
    }
    // and back to line begin - do not forget the fflush to avoid
output buffering problems!
    printf("]\r");
    fflush(stdout);
}

Sorry for the noise,

baptiste

On 8 August 2010 16:37, baptiste auguie <baptiste.auguie at googlemail.com> wrote:
> Dear list,
>
> There's one feature that I miss after porting a for loop from R to C++
> : the possibility to add a progress bar. I used to write,
>
> library(plyr)
> l_ply(seq(1,10), function(ii) Sys.sleep(0.2), .progress = "text")
>
> My naive attempt in C++ was unsuccessful,
>
> using namespace std;
>
>  cout << "["; //start progress bar
>  for(ll=0; ll<N; ll++){ // loop over some variable
>    if ( !( ((ll+1)*100/N) % 5) ) //  display a character every 5 steps
>      cout << "=";
>  }
>  cout << "["; //stop progress bar
>
> For some reason (compiler?) nothing is displayed until the end of the
> execution, where the full text is suddenly displayed at once. Is there
> a workaround?
>
> Best regards,
>
> baptiste
>


More information about the Rcpp-devel mailing list