There's literally nothing on the internet showing how these work in conjunction with one another. I would really appreciate even the simplest example of a Rnw document that you would run with Sweave() in R.

If you could provide a title, a simple plot, centered on the page, and a page number. Please, lets see how you would do it.


  • possible duplicate of What's the best tutorial for beamer, sweave and ggplot2 combined? Commented Mar 22, 2011 at 9:06
  • @Brandon: I just see that the post I linked to does not quite provide the answer to your question, but I hope it still helps. Commented Mar 22, 2011 at 9:24
  • Thank you Hendrik, but no, that post was not particularly instructive as a) those examples are outdated and do not work properly and b) they do not combine all of the required elements of my request. The reason for this question is to see a real example, however simple, of these three in action together. Commented Mar 22, 2011 at 9:55
  • I should qualify that by saying that they "do not work properly" for me. The text and images are garbled or misplaced. Commented Mar 22, 2011 at 9:56
  • @Brandon if the above linked question isn't helpful, could you try and make your question more specific?
    – Seamus
    Commented Mar 22, 2011 at 11:43

1 Answer 1


Here is an example where I shamelessly copied some R code from Cross-Validated. It can be compiled in many ways, but personally I used

R CMD Sweave 1.Rnw
pdflatex 1.tex

where 1.Rnw actually reads:


\title{A sample Sweave demo}
\author{Author name}





n <- 1000
x <- rnorm(n, mean=2)
y <- 1.5 + 0.4*x + rnorm(n)
df <- data.frame(x=x, y=y)

# take a bootstrap sample
df <- df[sample(nrow(df), nrow(df), rep=TRUE),]

xc <- with(df, xyTable(x, y))
df2 <- cbind.data.frame(x=xc$x, y=xc$y, n=xc$number)
df.ell <- as.data.frame(with(df, ellipse(cor(x, y), 
p1 <- ggplot(data=df2, aes(x=x, y=y)) + 
  geom_point(aes(size=n), alpha=.6) + 
  stat_smooth(data=df, method="loess", se=FALSE, color="green") + 
  stat_smooth(data=df, method="lm") +
  geom_path(data=df.ell, colour="green", size=1.2)

\caption{Here goes the caption.}


With Beamer, you just have to replace the first line with


or add whatever customizations you want, replace \maketitle with something like \frame{\titlepage}, and then enclose every code chunks with a \begin{frame}[fragile] ... \end{frame} statement. Compilation goes the same way as aforementioned.

Code chunks can be customized using, e.g.

{formatcom = {\color{Sinput}},fontsize=\scriptsize} 
{formatcom = {\color{Soutput}},fontsize=\footnotesize}
{formatcom = {\color{Scode}},fontsize=\small} 

It requires fancyvrb and needs to be somewhere after the \begin{document}. Personally, I hold in an external configuration file, among other stuff,


Here is a snapshot:

enter image description here

  • When I run this, the R code that's on page three crosses the right edge of the page. Any thoughts on why that might be? Commented Mar 22, 2011 at 19:51
  • @Brandon Cannot reproduce it: the Sweave article is 2 pages long, the Beamer slides are 3 pages long. If you mean slide #2 in the above snapshot, where actually the R code is echoed, then I guess you'll have to reduce font size -- as shown near the end of my response (I often alternate between \footnotesize and \scriptsize for S chunks). I have other templates for Beamer + Sweave that I can share on github if you like.
    – chl
    Commented Mar 22, 2011 at 21:56
  • @chl, yes, that would be very helpful (re: templates). I'll try changing the font size. Still getting my head around this tex stuff. Commented Mar 22, 2011 at 22:28
  • @Brandon Ok, I'll do my best to gist them tomorrow.
    – chl
    Commented Mar 22, 2011 at 22:29
  • For customizing the code chunks, could you specify where in the 1.Rnw file they would go? As well as the colouring definitions? \DefineVerbatimEnvironment ... and \definecolor where do you put these? They seem to break the output when I place them in the 1.Rnw file. Commented Mar 22, 2011 at 22:44

You must log in to answer this question.

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