[R] beamer overlays with Sweave?

Duncan Murdoch murdoch.duncan at gmail.com
Tue Mar 8 13:35:02 CET 2011


On 11-03-07 10:20 PM, Ben Bolker wrote:
>
>    This may be asking too much, but I'm wondering if anyone has a
> solution (even a hack) for creating multiple (overlay) plots in an
> Sweave file and post-processing the overlays in beamer appropriately.
>
>    For example, suppose I have a series of figure blocks in my .Rnw file:
>
> <<plot1,fig=TRUE>>=
> [stuff]
> @
> <<plot2,fig=TRUE>>=
> [stuff]
> @
> <<plot3,fig=TRUE>>=
> [stuff]
> @
>
>    These three blocks create three figures that I want to have appear as
> a series of overlays in the final PDF file.
>
>    Sweave outputs the following LaTeX code:
>
> \includegraphics{plot1}
> \includegraphics{plot2}
> \includegraphics{plot3}
>
>    and I need to turn it into
>
> \only<1>{\includegraphics{plot1}}
> \only<2>{\includegraphics{plot2}}
> \only<3>{\includegraphics{plot3}}
>
>    I have few enough of these that I've been modifying them by hand
> (couldn't easily come up with the appropriate sed/awk incantation); it
> works, but it's annoying and error-prone.  I could spend some more time
> hacking it, but I wondered if anyone else had already solved this ...
>
>    (Brief googling of "beamer+Sweave+overlay" didn't find an obvious
> answer, but I might have missed something ...)

You can put the \only calls in the Rnw, e.g.


\only<1>{
<<fig=true>>=
m <- 1
<<plotchunk>>
@
}
\only<2>{
<<fig=true>>=
m <- 2
<<plotchunk>>
@
}

Duncan Murdoch



More information about the R-help mailing list