4

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?

1 Answer 1

7

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}
5
  • So Sweave chunks can't be indented? Hmmm, this might be a problem for my OCD self. Jul 10, 2013 at 11:49
  • 2
    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, 2013 at 11:53
  • Oh, well. The important thing [fragile] did the trick. Thanks a bunch! Jul 10, 2013 at 13:50
  • 1
    @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, 2013 at 17:52
  • What does fragile do here? I would plus 10 if I could.
    – dayne
    May 25, 2018 at 11:50

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .