5

I have coded an MCMC sampler using Rcpp (specifically, RcppArmadillo). I would like my sampler to print periodic status messages to the R console, but the messages appear all at once when the sampler terminates. The output statement is :

Rcout << "Progress => " 
      << double(iterations) / double(maxit) * 100 
      << "%" 
      << std::endl;

Can this code be augmented so that its output appears on the R console immediately?

R version: 2.14.1

platform: Darwin 9.8.0

Rcpp version: 0.9.10

Romain Francois
  • 17,432
  • 3
  • 51
  • 77

3 Answers3

2

This behaviour has been implemented in Rcpp 0.10.0 (to be released soon) following advice from answers to this question.

In the meantime, you can manually call R_FlushConsole

Community
  • 1
  • 1
Romain Francois
  • 17,432
  • 3
  • 51
  • 77
0

flush() and flush.console() are still broken. R 3.0.2 / OSX

Allen Day
  • 78
  • 1
  • 5
0

You need a hat trick:

R_FlushConsole();
R_ProcessEvents();
R_CheckUserInterrupt();

Copied from: parallel-computing-in-r-with-c-within-r-functions

Community
  • 1
  • 1
Matt Chambers
  • 2,229
  • 1
  • 25
  • 43