[R] printing reals from C with digits -- once more

Ott Toomet otoomet at econ.dk
Sun Feb 2 16:03:03 CET 2003

Thank for Brian D. Ripley for the answer to my previous question.  

My aim is to print single values from different real vectors with
different digits option.  This is quite a possible with Rprintf() but
that one supprots printing format as C does, not in R native way
(setting digits=7 or printing with "%12.7f" are two quite different
things).  So I would like to find something more R-ish.

PrintValue() seems promising (it understands when I change
options(digits) but I did not get it to print a single value.  I tried

  SEXP variable;
  PrintValue(VECTOR_ELT(variable, i));

which lead to crash, while


prints the whole vector.  

StringFromReal() (and printing as CHAR) is otherwise nice but I dont
know how can I make it to understand options(digits).

Can anybody suggest me a way for this?

Best wishes,


BTW, can I be sure that everything defined in /usr/lib/R/include and
R_ext are (more or less) safe to use?

 | From: Prof Brian D Ripley <ripley at stats.ox.ac.uk>
 | Date: Wed, 29 Jan 2003 09:01:44 +0000 (GMT Standard Time)
 | On Wed, 29 Jan 2003, Ott Toomet wrote:
 | > I want to print real numbers in C code with different values for
 | > digits.  How to do that?
 | Use Rprintf or PrintValue.  You'll need to work hard to convince me that
 | Rprintf is not adequate.
 | > As I have understood, what I should do is to call
 | >
 | > StringFromReal()
 | That's a coercion, not a printing routine.
 | > which calls FormatReal(), that one suggests the parameters (width,
 | > decimal places and exponential form).  FormatReal() includes
 | >
 | >     eps = pow(10.0, -(double)R_print.digits);
 | >
 | > So I guess I have to change the value of R_print.digits.
 | > R_print.digits is defined in include/Print.h in the package source,
 | > but unfortunately the installed version (in /usr/lib/R/include/R_ext) is
 | > quite a different.
 | R_ext/Print.h and Print.h are not the same thing: one is not a version of
 | the other.  The routines you mention are not documented in R-exts, and are
 | not part of the API.
 | > I guess that is because the structure is not meant
 | > to be accessible by user, although some system routines alter
 | > R_print.digits directly.
 | (Only the coercion and print routines!)
 | > So are there any good way to achieve it?
 | Temporarily change the options(digits) and call PrintValue().  You are not
 | meant to (and it is not safe to) mess with R's printing internals, and
 | these structures change even at patch releases.

More information about the R-help mailing list