[Tex/LaTex] How to insert Sweave code inside a Beamer block

beamerbeamerposterrsweave

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?

Best Answer

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:

MWE

\documentclass{beamer}
\usetheme{Madrid}
\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}