[R] Sweave \Sexpr() issue

Fernando Henrique Ferraz feferraz at ime.usp.br
Thu Sep 25 01:20:34 CEST 2003

   Hi, I'm having a little issue with \Sexpr{bla} relating to the number of digits it is using to print its output. I understand that what \Sexpr{bla} does is parsing 'bla' inside R and then returns it coerced into a character string. The problem I'm having is that I'm losing control over the number of digits it's using to print it's output.
   For example, let's take:
   > ex <- c(10,2,3,4,24)
   > options(digits=2)
   > sd(ex)
   [1] 9.2
   > options(digits=22)
   > sd(ex)
   [1] 9.154233993076646

  When I do a \Sexpr{sd(ex)} what it does is as.character(sd(ex)), which doesn't care about the "options(digits=n)" setting. So in my .tex output instead of getting something like 9.2 I always get a representation in 20 digits or so, as in   [1] 9.154233993076646.
  I have tried using \Sexpr{options(digits=2),sd(ex)}, but to no effect. Apparently 'options(digits)'  has no effect on the output of 'as.character', which is used in the \Sexpr implementation.
 > options(digits=2)
 > as.character(sd(ex))
 [1] "9.15423399307665"

 What I tried then was:
 > options(digits=2)
 > as.character(print(sd(ex)))
 [1] 9.2

 Which works inside R, but when I to that inside an \Sexpr (\Sexpr{options(digits=2);print(sd(ex))} it prints the full [1] "9.15423399307665".

 Anyone has any ideas?

Thank you,

More information about the R-help mailing list