[R] globally set digits=3 in Sweave

Duncan Murdoch murdoch at stats.uwo.ca
Thu Aug 20 19:26:52 CEST 2009


On 8/20/2009 12:17 PM, Liviu Andronic wrote:
> On 8/20/09, Duncan Murdoch <murdoch at stats.uwo.ca> wrote:
>>  Why not put
>>
>>  <<echo=FALSE>>=
>>  options(digits=4)
>>  @
>>
>>  somewhere near the top of your document?
>>
> 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
>> tmp <- pi
>> tmp
> [1] 3.142
> 
> 
> , while
> \Sexpr{tmp}
> 
> will yield
> 3.14159265358979
> 
> 
> and
> \Sexpr{format(tmp)}
> 
> will yield
> 3.142

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




More information about the R-help mailing list