# Multiple Tables Sweave/Latex

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.

-
Bad start, because this is a big problem to make such thing in Sweave. –  mbq Jul 27 '10 at 12:41
That doesn't sound too difficult to me. Go ahead and do it! –  Matti Pastell Jul 27 '10 at 16:12
"Create multiple crosstabs, most likely in a loop."? This requires creating Rnw with R and then Sweaving (this is the way I have seen it done in case of making exam question sheets), what is not elegant at all. –  mbq Jul 27 '10 at 16:45
Or rather brew, as sheed03 suggested. –  mbq Jul 27 '10 at 16:50
An example would be nice. =) This can be a tricky one, so give us some guide. What kind of crosstab you'd like to make with Sweave? You should definitely avoid loops, IMHO... –  aL3xa Jul 27 '10 at 19:53

## migrated from stackoverflow.comFeb 23 '11 at 20:31

This question came from our site for professional and enthusiast programmers.

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')
}
@

-

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.

-