[R] how to use \Sexpr{} with sweave

Duncan Murdoch murdoch at stats.uwo.ca
Sun Oct 7 12:37:20 CEST 2007

cryan at binghamton.edu wrote:
> I'm trying to learn Sweave.  So far things are going well with the chunks of code identified by << >>=
>
> But I'm having trouble with the in-line text use of \Sexpr.
>
> Here is a short example .Rnw file:
>
> \documentclass[12pt]{article}
> \usepackage[margin=1.25in]{geometry}
> \usepackage{graphicx}
> \usepackage{Sweave}
>
> \begin{document}
>
> <<test>>=
> pnorm(1)
> @
>
>
> \Sexpr{2+6}
>
> \end{document}
>
> The .tex file that it yields contains
>
> \Sexpr{2+6} as its next-to-last line.  \usepackage{Sweave} is in its preamble.
>
> When I pdflatex that file, the resulting pdf file shows the proper value of pnorm(1), but it does not contain "8" near its end.  Instead it contains "2+6".  I had expected the computed value.
>
> Also, pdflatex gives me a message that "there was an error opening the document.  The file cannot be found." I have to go find it and open it, instead of it opening automatically like a non-problematic case would.
>
> Running R 2.5.1 on WinXP, with MikTex.
>
> Any advice? Thanks.
How did you produce the .tex file?  I just cut and pasted your file into
test.Rnw, and Sweave( "test.Rnw" ) produced the .tex file shown below,
with no 2+6 in it.

\documentclass[12pt]{article}
\usepackage[margin=1.25in]{geometry}
\usepackage{graphicx}
\usepackage{Sweave}

\begin{document}

\begin{Schunk}
\begin{Sinput}
> pnorm(1)
\end{Sinput}
\begin{Soutput}
[1] 0.8413447
\end{Soutput}
\end{Schunk}

8

\end{document}