Pretty printing progress bars from .Call in R

I wanted to do something that takes a while in C (called from R) and wanted to print a progress bar.

In the R function, I did

pBar <- txtProgressBar( min = 0, max = n, style = 3 )
ret <- .Call("myfunction", arg1, arg2, as.integer(type), pBar, PACKAGE="annotSnpStats")

In the C function, I had

SEXP myfunction(SEXP arg1, SEXP arg2, SEXP Rtype, SEXP pBar) {

  int nprotect=0;
  SEXP utilsPackage, percentComplete;
  PROTECT(utilsPackage = eval(lang2(install("getNamespace"), ScalarString(mkChar("utils"))), R_GlobalEnv));
  PROTECT(percentComplete = allocVector(INTSXP, 1));
  int *rPercentComplete = INTEGER(percentComplete);

  for(i=0; i<nx; i++) { // index rows of x
  *rPercentComplete = i; //this value increments
  eval(lang4(install("setTxtProgressBar"), pBar, percentComplete, R_NilValue), utilsPackage);




And … it … just … worked! Happy day.

To see the full R function, go to dups in snp-match.R and the C is in countdiffs in comparisons.c.