Generating LaTeX output from R data frame
I often use the latex
and describe
functions from the Hmisc
package, which allow a lot of fine-tuning.
library(Hmisc)
d <- data.frame(a=LETTERS[1:5], x=rnorm(5))
latex(d, file="") # If you want all the data
latex(describe(d), file="") # If you just want a summary
The xtable
package has some examples of how to generate tables - see vignette. When you're writing a chunk, make sure you set the chunk to <<results=tex>>
. See Sweave example, for instance, this.
This is how I would output a data.frame.
<<results=tex>>
xtable(my.data.frame)
@
And the raw result would looks something like this:
> xtable(my.data.frame)
% latex table generated in R 2.14.1 by xtable 1.6-0 package
% Tue Feb 14 10:03:03 2012
\begin{table}[ht]
\begin{center}
\begin{tabular}{rllr}
\hline
& p & q & r \\
\hline
1 & condition\_a & grp\_1 & 3 \\
2 & condition\_a & grp\_1 & 3 \\
3 & condition\_a & grp\_1 & 4 \\
4 & condition\_a & grp\_1 & 1 \\
5 & condition\_b & grp\_1 & 4 \\
6 & condition\_b & grp\_1 & 3 \\
7 & condition\_b & grp\_1 & 5 \\
8 & condition\_b & grp\_1 & 5 \\
9 & condition\_a & grp\_2 & 4 \\
10 & condition\_a & grp\_2 & 1 \\
11 & condition\_a & grp\_2 & 1 \\
12 & condition\_a & grp\_2 & 1 \\
13 & condition\_b & grp\_2 & 5 \\
14 & condition\_b & grp\_2 & 1 \\
15 & condition\_b & grp\_2 & 5 \\
16 & condition\_b & grp\_2 & 2 \\
\hline
\end{tabular}
\end{center}
\end{table}