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 PrintValue(variable); 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, Ott 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.
ripley@stats.ox.ac.uk
2003-Feb-02 16:19 UTC
[R] Re: printing reals from C with digits -- once more
On Sun, 2 Feb 2003, Ott Toomet wrote:> 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.R *does* use C printing formats, and all you need to so is to track those down in the sources.> 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 > > PrintValue(variable); > > prints the whole vector.You need to create a vector of length 1 to print one value.> StringFromReal() (and printing as CHAR) is otherwise nice but I dont > know how can I make it to understand options(digits).It's not a public entry point, and support for options(digits) is in the callers.> Can anybody suggest me a way for this? > > Best wishes, > > Ott > > > BTW, can I be sure that everything defined in /usr/lib/R/include and > R_ext are (more or less) safe to use?No, read R_exts for what is in the API and what is not. -- Brian D. Ripley, ripley at stats.ox.ac.uk Professor of Applied Statistics, http://www.stats.ox.ac.uk/~ripley/ University of Oxford, Tel: +44 1865 272861 (self) 1 South Parks Road, +44 1865 272866 (PA) Oxford OX1 3TG, UK Fax: +44 1865 272595
Apparently Analagous Threads
- printing reals from C with digits
- formatReal()-bug (or Is there anybody out there w/o IEEE754?)
- as.numeric(levels(factor(x))) may be a decreasing sequence
- [LLVMdev] [LLVM, llvm-diff] Question about FunctionDifferenceEngine and DiffConsumer::printValue
- [LLVMdev] [LLVM, llvm-diff] Question about FunctionDifferenceEngine and DiffConsumer::printValue