Take the 2-minute tour ×
TeX - LaTeX Stack Exchange is a question and answer site for users of TeX, LaTeX, ConTeXt, and related typesetting systems. It's 100% free, no registration required.

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.

share|improve this question
    
Bad start, because this is a big problem to make such thing in Sweave. –  mbq Jul 27 '10 at 12:41
1  
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
1  
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
show 2 more comments

migrated from stackoverflow.com Feb 23 '11 at 20:31

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

4 Answers

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')
}
@
share|improve this answer
add comment

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.

share|improve this answer
add comment

I just wanted to chime in to highly recommend RStudio. It's distributed as either a standalone application or as a web service. The latter requires a bit setup with a webserver (e.g., apache). It has fantastic support for both knitr and sweave along with some very basic "chunk" navigation. See this snapshot from their site.

enter image description here

share|improve this answer
    
+1 While it does not actually answer the question, it is definitely a good recommendation for someone who is just starting with R ans Sweave. –  Jan Hlavacek May 3 '13 at 5:00
add comment

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.

share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.