[R] Pretty printing progress

Douglas Bates bates at stat.wisc.edu
Thu Jun 17 18:13:52 CEST 2010


On Thu, Jun 17, 2010 at 10:50 AM, Barry Rowlingson
<b.rowlingson at lancaster.ac.uk> wrote:
> On Thu, Jun 17, 2010 at 3:33 PM, Doran, Harold <HDoran at air.org> wrote:
>> I have a function that is an iterative process for estimating some MLEs. I want to print some progress to screen as the process iterates. I would like to try and line things up nicely in the R window, but am not sure the best way to do this. Below is a toy example. Suppose I want the value of 10 to be just below "iteration" and the value of -1234 to be just below 'Log Likelihood'.
>
>  Sure you just dont want to use the progress bar functions from the
> plyr package:
>
> ?plyr::create_progress_bar
>
>  another example of things being in the wrong package....

If you want to stick with a text display you can use the sprintf
function to format the strings that you print


> cat('Iteration      Log Likelihood\n', sprintf("%8d%20g\n", 10, -1234))
Iteration      Log Likelihood
       10               -1234

I would avoid the tab character as you can't count on the displays of
tabs to be consistent.  You may also want to change the display of the
log-likelihood to be a fixed number of decimal places rather than a
general format for floating point numbers, which can switch into the
"e" notation for very large or very small numbers.


> cat('Iteration      Log Likelihood\n', sprintf("%8d%20.4f\n", 10, -1234))
Iteration      Log Likelihood
       10          -1234.0000

The format of the format strings is another "little language" to learn
but it is a very powerful mechanism.



More information about the R-help mailing list