1 %\VignetteIndexEntry{xtable Gallery}
2 %\VignetteDepends{xtable}
3 %\VignetteKeywords{LaTeX,HTML,table}
4 %\VignettePackage{xtable}
6 %**************************************************************************
14 <<echo=FALSE,eval=FALSE>>=
15 makeme <- function() {
16 # I am a convenience function for debugging and can be ignored
17 setwd("C:/JonathanSwinton/PathwayModeling/src/R/SourcePackages/xtable/inst/doc")
18 Sweave("xtableGallery.RnW",stylepath=FALSE)
23 \documentclass[letterpaper]{article}
28 \author{Jonathan Swinton <jonathan@swintons.net>\\ with small contributions from others}
31 \SweaveOpts{prefix.string=figdir/fig,debug=TRUE,eps=FALSE,echo=TRUE}
33 \usepackage{longtable}
34 %\usepackage{hyperref}
39 This document gives a gallery of tables which can be made
40 by using the {\tt xtable} package to create \LaTeX\ output.
41 It doubles as a regression check for the package.
48 \subsection{Data frame}
53 ## Demonstrate data.frame
54 tli.table <- xtable(tli[1:10,])
55 digits(tli.table)[c(2,6)] <- 0
58 print(tli.table,floating=FALSE)
63 design.matrix <- model.matrix(~ sex*grade, data=tli[1:10,])
64 design.table <- xtable(design.matrix)
67 print(design.table,floating=FALSE)
72 fm1 <- aov(tlimth ~ sex + ethnicty + grade + disadvg, data=tli)
73 fm1.table <- xtable(fm1)
76 print(fm1.table,floating=FALSE)
80 fm2 <- lm(tlimth ~ sex*ethnicty, data=tli)
81 fm2.table <- xtable(fm2)
84 print(fm2.table,floating=FALSE)
86 \subsubsection{anova object}
89 print(xtable(anova(fm2)),floating=FALSE)
91 \subsubsection{Another anova object}
93 fm2b <- lm(tlimth ~ ethnicty, data=tli)
96 print(xtable(anova(fm2b,fm2)),floating=FALSE)
105 fm3 <- glm(disadvg ~ ethnicty*grade, data=tli, family=binomial())
106 fm3.table <- xtable(fm3)
109 print(fm3.table,floating=FALSE)
112 \subsubsection{anova object}
115 print(xtable(anova(fm3)),floating=FALSE)
119 \subsection{More aov}
123 ## Taken from help(aov) in R 1.1.1
124 ## From Venables and Ripley (1997) p.210.
125 N <- c(0,1,0,1,1,1,0,0,0,1,1,0,1,1,0,0,1,0,1,0,1,1,0,0)
126 P <- c(1,1,0,0,0,1,0,1,1,1,0,0,0,1,0,1,1,0,0,1,0,1,1,0)
127 K <- c(1,0,0,1,0,1,1,0,0,1,0,1,0,1,1,0,0,0,1,1,1,0,1,0)
128 yield <- c(49.5,62.8,46.8,57.0,59.8,58.5,55.5,56.0,62.8,55.8,69.5,55.0,
129 62.0,48.8,45.5,44.2,52.0,51.5,49.8,48.8,57.2,59.0,53.2,56.0)
130 npk <- data.frame(block=gl(6,4), N=factor(N), P=factor(P), K=factor(K), yield=yield)
131 npk.aov <- aov(yield ~ block + N*P*K, npk)
132 op <- options(contrasts=c("contr.helmert", "contr.treatment"))
133 npk.aovE <- aov(yield ~ N*P*K + Error(block), npk)
138 print(xtable(npk.aov),floating=FALSE)
141 \subsubsection{anova object}
143 print(xtable(anova(npk.aov)),floating=FALSE)
146 \subsubsection{Another anova object}
148 print(xtable(summary(npk.aov)),floating=FALSE)
155 print(xtable(npk.aovE),floating=FALSE)
160 print(xtable(summary(npk.aovE)),floating=FALSE)
167 ## Taken from help(lm) in R 1.1.1
168 ## Annette Dobson (1990) "An Introduction to Generalized Linear Models".
169 ## Page 9: Plant Weight Data.
170 ctl <- c(4.17,5.58,5.18,6.11,4.50,4.61,5.17,4.53,5.33,5.14)
171 trt <- c(4.81,4.17,4.41,3.59,5.87,3.83,6.03,4.89,4.32,4.69)
172 group <- gl(2,10,20, labels=c("Ctl","Trt"))
173 weight <- c(ctl, trt)
174 lm.D9 <- lm(weight ~ group)
177 print(xtable(lm.D9),floating=FALSE)
182 print(xtable(anova(lm.D9)),floating=FALSE)
185 \subsection{More glm}
189 ## Taken from help(glm) in R 1.1.1
190 ## Annette Dobson (1990) "An Introduction to Generalized Linear Models".
191 ## Page 93: Randomized Controlled Trial :
192 counts <- c(18,17,15,20,10,20,25,13,12)
195 d.AD <- data.frame(treatment, outcome, counts)
196 glm.D93 <- glm(counts ~ outcome + treatment, family=poisson())
199 print(xtable(glm.D93,align="r|llrc"),floating=FALSE)
204 if(require(stats,quietly=TRUE)) {
205 ## Demonstrate prcomp
206 ## Taken from help(prcomp) in mva package of R 1.1.1
208 pr1 <- prcomp(USArrests)
212 if(require(stats,quietly=TRUE)) {
213 print(xtable(pr1),floating=FALSE)
220 print(xtable(summary(pr1)),floating=FALSE)
226 # ## Demonstrate princomp
227 # ## Taken from help(princomp) in mva package of R 1.1.1
228 # pr2 <- princomp(USArrests)
231 \subsection{Time series}
234 temp.ts <- ts(cumsum(1+round(rnorm(100), 0)), start = c(1954, 7), frequency=12)
235 temp.table <- xtable(temp.ts,digits=0)
236 caption(temp.table) <- "Time series example"
239 print(temp.table,floating=FALSE)
241 <<savetofile,echo=FALSE>>=
243 for(i in c("latex","html")) {
244 outFileName <- paste("xtable.",ifelse(i=="latex","tex",i),sep="")
245 print(xtable(lm.D9),type=i,file=outFileName,append=TRUE,latex.environment=NULL)
246 print(xtable(lm.D9),type=i,file=outFileName,append=TRUE,latex.environment="")
247 print(xtable(lm.D9),type=i,file=outFileName,append=TRUE,latex.environment="center")
248 print(xtable(anova(glm.D93,test="Chisq")),type=i,file=outFileName,append=TRUE)
249 print(xtable(anova(glm.D93)),hline.after=c(1),size="small",type=i,file=outFileName,append=TRUE)
250 # print(xtable(pr2),type=i,file=outFileName,append=TRUE)
255 \section{Sanitization}
257 insane <- data.frame(Name=c("Ampersand","Greater than","Less than","Underscore","Per cent","Dollar","Backslash","Hash", "Caret", "Tilde","Left brace","Right brace"),
258 Character = I(c("&",">", "<", "_", "%", "$", "\\", "#", "^", "~","{","}")))
259 colnames(insane)[2] <- paste(insane[,2],collapse="")
262 <<pxti,results=tex>>=
263 print( xtable(insane))
265 Sometimes you might want to have your own sanitization function
267 wanttex <- xtable(data.frame( label=paste("Value_is $10^{-",1:3,"}$",sep="")))
270 print(wanttex,sanitize.text.function=function(str)gsub("_","\\_",str,fixed=TRUE))
273 \subsection{Markup in tables}
275 Markup can be kept in tables, including column and row names, by using a custom sanitize.text.function:
278 mat <- round(matrix(c(0.9, 0.89, 200, 0.045, 2.0), c(1, 5)), 4)
279 rownames(mat) <- "$y_{t-1}$"
280 colnames(mat) <- c("$R^2$", "$\\bar{R}^2$", "F-stat", "S.E.E", "DW")
284 print(mat, sanitize.text.function = function(x){x})
287 % By David Dahl to demonstrate contribution from David Whitting, 2007-10-09.
288 You can also have sanitize functions that are specific to column or row names. In the table below, the row name is not sanitized but column names and table elements are:
290 money <- matrix(c("$1,000","$900","$100"),ncol=3,dimnames=list("$\\alpha$",c("Income (US$)","Expenses (US$)","Profit (US$)")))
293 print(xtable(money),sanitize.rownames.function=function(x) {x})
296 \section{Format examples}
297 \subsection{Adding a centering environment }
299 print(xtable(lm.D9,caption="\\tt latex.environment=NULL"),latex.environment=NULL)
300 print(xtable(lm.D9,caption="\\tt latex.environment=\"\""),latex.environment="")
301 print(xtable(lm.D9,caption="\\tt latex.environment=\"center\""),latex.environment="center")
303 \subsection{Column alignment}
306 tli.table <- xtable(tli[1:10,])
309 align(tli.table) <- rep("r",6)
312 print(tli.table,floating=FALSE)
314 \subsubsection{Single string and column lines}
316 align(tli.table) <- "|rrl|l|lr|"
319 print(tli.table,floating=FALSE)
321 \subsubsection{Fixed width columns}
323 align(tli.table) <- "|rr|lp{3cm}l|r|"
326 print(tli.table,floating=FALSE)
329 \subsection{Significant digits}
332 Specify with a single argument
334 digits(tli.table) <- 3
337 print(tli.table,floating=FALSE,)
341 or one for each column, counting the row names
343 digits(tli.table) <- 1:(ncol(tli)+1)
346 print(tli.table,floating=FALSE,)
352 digits(tli.table) <- matrix( 0:4, nrow = 10, ncol = ncol(tli)+1 )
355 print(tli.table,floating=FALSE,)
358 \subsection{Suppress row names}
360 print((tli.table),include.rownames=FALSE,floating=FALSE)
363 If you want a vertical line on the left, you need to change the align attribute.
365 align(tli.table) <- "|r|r|lp{3cm}l|r|"
368 print((tli.table),include.rownames=FALSE,floating=FALSE)
371 Revert the alignment to what is was before.
373 align(tli.table) <- "|rr|lp{3cm}l|r|"
376 \subsection{Suppress column names}
378 print((tli.table),include.colnames=FALSE,floating=FALSE)
381 Note the doubled header lines which can be suppressed with, eg,
383 print(tli.table,include.colnames=FALSE,floating=FALSE,hline.after=c(0,nrow(tli.table)))
386 \subsection{Suppress row and column names}
388 print((tli.table),include.colnames=FALSE,include.rownames=FALSE,floating=FALSE)
391 \subsection{Horizontal lines}
393 print(xtable(anova(glm.D93)),hline.after=c(1),floating=FALSE)
396 \subsection{Table-level \LaTeX}
398 print(xtable(anova(glm.D93)),size="small",floating=FALSE)
402 \subsection{Long tables}
403 Remember to insert \verb|\usepackage{longtable}| in your LaTeX preamble.
404 See Table \ref{tabbig}.
408 ## Demonstration of longtable support.
409 x <- matrix(rnorm(1000), ncol = 10)
410 x.big <- xtable(x,label='tabbig',
411 caption='Example of longtable spanning several pages')
414 print(x.big,tabular.environment='longtable',floating=FALSE)
418 \subsection{Sideways tables}
419 Remember to insert \verb|\usepackage{rotating}| in your LaTeX preamble.
420 Sideways tables can't be forced in place with the `H' specifier, but you can
421 use the \verb|\clearpage| command to get them fairly nearby.
422 See Table \ref{tabsmall}.
426 x.small <- xtable(x,label='tabsmall',caption='A sideways table')
430 print(x.small,floating.environment='sidewaystable')
434 \section{Suppressing Printing}
435 By default the {\tt print} method will print the LaTeX or HTML to standard
436 output and also return the character strings invisibly. The printing to
437 standard output can be supproessed by specifying {\tt print.result = FALSE}.
440 # This will print the output and store it.
441 x.out <- print(tli.table)
443 # This will just store the value.
444 x.out2 <- print(tli.table, print.results = FALSE)
447 Formatted output can also be captured without the printing with the
448 {\tt toLatex} method.
451 x.ltx <- toLatex(tli.table)
456 \section{Acknowledgements}
457 Most of the examples in this gallery are taken from the {\tt xtable} documentation.
458 \section{R Session information}
460 toLatex(sessionInfo())