# How can I insert Sweave code inside a Beamer block?

I'm currently trying to automate a report-generating routine in R, and I imagine the best way to try this is through Sweave and beamerposter. The report should have one page with several sections in it, so I'm using one frame with several block environments inside it. The problem I'm having is with inserting Sweave-generated figures inside those blocks (or any Sweave code, for that matter). For example, this yields an error:

\documentclass{beamer}
\begin{document}
\SweaveOpts{concordance=TRUE}
\begin{frame}
\begin{block}{Title}
<<>>=
1 + 1
@
\end{block}
\end{frame}
\end{document}


From what I've read, block environments are supposed to contain only text, but I can't imagine another way to structure my report that would match my client's expectations (he want it to look as much as possible as their current Excel-generate report). So my primary question is: how can I insert Sweave code inside a block environment?

-

One problem of your MWE is that the both the start and end of the R chunk (<<>>= and @) are not at the beginning of the line.

Another problem is that you have to add the option [fragile] to the frame environment.

If you correct your both problems your example will work, also with figures if you provide the fig=T option, as in this example:

\documentclass{beamer}
\begin{document}
\SweaveOpts{concordance=TRUE}
\begin{frame}[fragile]{This is a \texttt{fragile} frame}
\begin{block}{This is a R chunk}
<<oneandone>>=
1+1
@
\end{block}
\begin{block}{This is a small figure}
\setkeys{Gin}{width=0.3\linewidth}
<<plot,fig=TRUE>>=
plot(c(1,2,3,4,4,4,3,2,1),xlab="x",ylab="y")
@
\end{block}
\end{frame}
\end{document}

-
So Sweave chunks can't be indented? Hmmm, this might be a problem for my OCD self. –  Waldir Leoncio Jul 10 '13 at 11:49
The R code itself can be indented; only the <<>>= and @ lines need to be flush, I think. (But the indentation won't show up in the displayed code.) –  Alan Munn Jul 10 '13 at 11:53
Oh, well. The important thing [fragile] did the trick. Thanks a bunch! –  Waldir Leoncio Jul 10 '13 at 13:50
@AlanMunn True. The code inside a R chunk in a noweb file can be formated as any independent R script. What is showed in the in the generated LaTeX file by default is not that code formated in any way, but the result of the executed code similarly as seen in a R console, that is, showing first an echo of each processed instruction preceded by >. –  Fran Jul 10 '13 at 17:52
@Fran Thanks for the clarification. –  Alan Munn Jul 10 '13 at 18:20