[R] extracting items from R objects and using them in \Sexpr with Sweave--problem with $

Charles C. Berry cberry at tajo.ucsd.edu
Thu Nov 29 23:05:21 CET 2007

On Thu, 29 Nov 2007, Christopher W. Ryan wrote:

> Running R 2.5.1 on WinXP with Tinn-R, and using Sweave via MikTex.
> I am having trouble with the code below:
> -------------------------------------------------------
> <<lowerCI95>>=
> lowerCI95713 <- t.test(data713, alternative="greater")
> lowerCI95713.bound <- lowerCI95713$conf.int[1]
> @
> Thus a lower 95\% confidence bound on the mean octane concentration is
> \Sexpr{lowerCI95713.bound}.
> <<ttest>>=
> t.test713 <- t.test(data713, mu=87, alternative="greater")
> p.value <- t.test713$p.value
> @
> ---------------------------------------------------------
> When I Sweave and then pdflatex the files, the output pdf file contains
> the verbatim text:
> "Thus a lower 95% confidence bound on the mean octane concentration is
> lowerCI95713.bound."


 	data713 <- rnorm(10)

at the start of the first chunk ("provide commented, minimal, 
self-contained, reproducible code"), and running this as

> sessionInfo()
R version 2.6.0 (2007-10-03)

LC_COLLATE=English_United States.1252;LC_CTYPE=English_United 
States.1252;LC_NUMERIC=C;LC_TIME=English_United States.1252

attached base packages:
[1] tools     stats     graphics  grDevices utils     datasets  methods
[8] base

I got

"Thus a lower 95\% confidence bound on the mean octane concentration is

> That is, LaTeX is not recognizing and processing \Sexpr.

Nor should it. That is Sweave's job.

When I look at
> my .tex file in an editor with LaTeX syntax highlighting, I see that
> LaTeX is interpreting everything between the two $ in the original code
> (one before conf.int and one before p.value) as a math environment.  I
> think that's why the \Sexpr is not getting processed into the single
> number as it should.
> Any ideas how to make this work?  The $ is the only way I know how to
> access components of R objects.

First, give us "commented, minimal, self-contained, reproducible code".

I suspect there is something upstream of what you provided that 
caused the problem. Perhaps narrowing your focus will reveal what it is, 
but in any case we must guess what your problem is without reproducible 



p.s. Time for you and me both to upgrade to R-2.6.1

> Thank you.
> --Chris
> -- 
> Christopher W. Ryan, MD
> SUNY Upstate Medical University Clinical Campus at Binghamton
> 40 Arch Street, Johnson City, NY  13790
> cryanatbinghamtondotedu
> PGP public keys available at http://home.stny.rr.com/ryancw/
> "If you want to build a ship, don't drum up the men to gather wood,
> divide the work and give orders. Instead, teach them to yearn for the
> vast and endless sea."  [Antoine de St. Exupery]
> ______________________________________________
> R-help at r-project.org mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
> and provide commented, minimal, self-contained, reproducible code.

Charles C. Berry                            (858) 534-2098
                                             Dept of Family/Preventive Medicine
E mailto:cberry at tajo.ucsd.edu	            UC San Diego
http://famprevmed.ucsd.edu/faculty/cberry/  La Jolla, San Diego 92093-0901

More information about the R-help mailing list