tags:

views:

268

answers:

3

Hi All,

I am very new to LaTeX and Sweave but am excited that it may make my life much simpler when combined with R.

What I am looking to do: Create multiple crosstabs, most likely in a loop. As a result, the number of tables will not be determined ahead of time and I am hoping to have a page break in between each. Lastly, for the time being, I am looking to make this a report instead of a presentation (Beamer).

My question: I assume this is possible, but is it easier to do in Sweave or LaTeX and how should I get started. I have had some bumps in the road getting up and running, but I have been able to compile some very basic examples of both.

This would be my first document in LaTeX, so I am open to any and all suggestions.

Many many thanks in advance for you guidance.

+1  A: 

Have you taken a look at this post from the Learning R Blog? The author has created an automated report with the Brew package, but one of the comments on the post does the same using Sweave and LaTeX. It sounds like you are trying to create a similar report. I was able to adapt the example for one of my automated reports.

sheed03
+3  A: 

Have a look at the xtable package, which prints LaTeX tables neatly. I have used this a lot for Sweave auto-generated reports.

The following is a toy example of printing some tables for LaTeX in a Sweave document

<<echo=FALSE,print=FALSE,results=tex>>
## generate an example set of tables                                                                                                                         
library(xtable)
data(tli)
my.tables <- list()
for(iTable in 1:20){
 my.tables[[iTable]] <- tli[1:20 + iTable,]
}

## print these out, with page breaks in between                                                                                                              
for(iTable in 1:20){
  print(xtable(my.tables[[iTable]]))
  cat('\\clearpage\n')
}
@
nullglob
+2  A: 

This might give you an impression how it works (or better: How I would do it :-)

Below is your Sweave file "tstSweave.Rnw" (use this file http://gist.github.com/492318 if you want give it a try).

\documentclass{article}

\begin{document}

lalalala \newline\newline

<<echo = FALSE, results = tex>>=

library(xtable)


df <- data.frame(x = rbinom(100, 1, 0.2), 
                 y = rbinom(100, 1, 0.6), 
                 g = sample(1:10,100, replace = TRUE))

for(i in unique(df$g)){
  sub <- subset(df, g == i)
  mytab <- with(sub, table(x, y))
  cat(paste("Group ",i, ""))
  print(xtable(mytab))
  cat(" \\clearpage \\pagebreak ")
}   
@ 

lalalala


\end{document}

In R you can use the Sweave() function:

## set your working directory
setwd("d:/tmp")
Sweave(file = "tstSweave.Rnw")

which should give you a new file: tstSweave.txt

pdflatex tstSweave.tex

which should give you a PDF file: tstSweave.pdf

That's it.

Bernd