xtable (xtable package)

xtable(x)
Generate a nicely formatted table for LaTeX or a basic table for HTML.
  • x – An R object that could conceivably be formatted into a table. A wide range of objects are accepted.

Example. A simple data frame is generated, then output for LaTeX and for HTML are generated.
> 
> #install.packages("xtable")
> library(xtable)
> 
> d <- data.frame(tail=c(12,11,32,0),
+                 legs=c(4,4,4,2),
+                 height=c(31,35,62,68))
> d
  tail legs height
1   12    4     31
2   11    4     35
3   32    4     62
4    0    2     68
> 
> xtable(d)
% latex table generated in R 2.14.1 by xtable 1.7-0 package
% Sun Mar 25 16:34:57 2012
\begin{table}[ht]
\begin{center}
\begin{tabular}{rrrr}
  \hline
 & tail & legs & height \\ 
  \hline
1 & 12.00 & 4.00 & 31.00 \\ 
  2 & 11.00 & 4.00 & 35.00 \\ 
  3 & 32.00 & 4.00 & 62.00 \\ 
  4 & 0.00 & 2.00 & 68.00 \\ 
   \hline
\end{tabular}
\end{center}
\end{table}
> 
> print(xtable(d), type="html") # output omitted
> 
Output from the HTML option above:







tail legs height
1 12.00 4.00 31.00
2 11.00 4.00 35.00
3 32.00 4.00 62.00
4 0.00 2.00 68.00

Leave a Reply