[R] limit digits in text()? --> use formatC()

Martin Maechler maechler at stat.math.ethz.ch
Thu Jun 10 11:33:36 CEST 1999

>>>>> On Thu, 10 Jun 1999, Bill Simpson <wsimpson at gcal.ac.uk> said:

    Bill> I am labelling the axes of a persp() plot manually using
    Bill> text(). For one spot in the plot I use:
    Bill> text(-.62,.37,max(dprime)) The problem is that the label is
    Bill> printed as 1.6456330961.  I would like 1.6.

    Bill> I tried options(digits=2) and str(digits.d=2)--no effect. (I
    Bill> don't like these methods anyway since I just want to modify how
    Bill> the label in the plot is printed; I want to print all the digits
    Bill> otherwise.)

    Bill> How should I do it?  Is the only way to first set
    Bill> options(digits=2), then compute dprime, then do text(), then when
    Bill> done reset options(digits=10)?

    Bill> Thanks very much for any help.

And then Bill posted

    Bill> Sorry, I found the answer: 
    Bill> 	text(-.62,.37,round(max(dprime), digits=1))

which can still be improved especially when you write functions to be
re-used with different numbers..

My answer is:  Instead of round(.), use  

    formatC(...  ,  digits = ..., width = ... )

It is flexible (via optional arguments),
it is *not* influenced by  options()$digits
*AND* it does the right thing very often, without any extra arguments.

The width argument might be particularly useful both for plotting,
and for cat(.)ing  results out of for/while/... loops
r-help mailing list -- Read http://www.ci.tuwien.ac.at/~hornik/R/R-FAQ.html
Send "info", "help", or "[un]subscribe"
(in the "body", not the subject !)  To: r-help-request at stat.math.ethz.ch

More information about the R-help mailing list