Skip to content

globally set digits=3 in Sweave

4 messages · Liviu Andronic, Duncan Murdoch

#
Dear all
I would like to globally set
options(digits = 4)

in an Sweave document. I've read this thread [1] where it was
suggested to set the above option and pass every printed number
through format(). Aanother way to do so would be to round(expr, 4).
However I would like to set the digits option once per document so
that every number printed respects it, without the need to pass every
time through a formatting/rounding function. Is this possible in
Sweave?

Thank you
Liviu
[1] https://stat.ethz.ch/pipermail/r-help/2003-September/039481.html
#
On 8/20/2009 6:19 AM, Liviu Andronic wrote:
Why not put

<<echo=FALSE>>=
options(digits=4)
@

somewhere near the top of your document?

Duncan Murdoch
#
On 8/20/09, Duncan Murdoch <murdoch at stats.uwo.ca> wrote:
I did so, but it has no practical effect on \Sexpr{} (which I'm
interested in, and failed to mention in the original e-mail).

With
<<echo=FALSE>>=
options(digits=4)
@

, the following
<<>>=
tmp <- pi
tmp
@

will yield
[1] 3.142


, while
\Sexpr{tmp}

will yield
3.14159265358979


and
\Sexpr{format(tmp)}

will yield
3.142


Liviu
#
On 8/20/2009 12:17 PM, Liviu Andronic wrote:
Right, \Sexpr does a simple as.character() on its argument.  You might 
be able to override as.character() for numeric data, but that's probably 
a bad idea.  So format(tmp) is probably the best choice.

Duncan Murdoch