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}
36 %\usepackage{hyperref}
41 This document gives a gallery of tables which can be made
42 by using the {\tt xtable} package to create \LaTeX\ output.
43 It doubles as a regression check for the package.
50 \subsection{Data frame}
55 ## Demonstrate data.frame
56 tli.table <- xtable(tli[1:10,])
57 digits(tli.table)[c(2,6)] <- 0
60 print(tli.table,floating=FALSE)
65 design.matrix <- model.matrix(~ sex*grade, data=tli[1:10,])
66 design.table <- xtable(design.matrix)
69 print(design.table,floating=FALSE)
74 fm1 <- aov(tlimth ~ sex + ethnicty + grade + disadvg, data=tli)
75 fm1.table <- xtable(fm1)
78 print(fm1.table,floating=FALSE)
82 fm2 <- lm(tlimth ~ sex*ethnicty, data=tli)
83 fm2.table <- xtable(fm2)
86 print(fm2.table,floating=FALSE)
88 \subsubsection{anova object}
91 print(xtable(anova(fm2)),floating=FALSE)
93 \subsubsection{Another anova object}
95 fm2b <- lm(tlimth ~ ethnicty, data=tli)
98 print(xtable(anova(fm2b,fm2)),floating=FALSE)
107 fm3 <- glm(disadvg ~ ethnicty*grade, data=tli, family=binomial())
108 fm3.table <- xtable(fm3)
111 print(fm3.table,floating=FALSE)
114 \subsubsection{anova object}
117 print(xtable(anova(fm3)),floating=FALSE)
121 \subsection{More aov}
125 ## Taken from help(aov) in R 1.1.1
126 ## From Venables and Ripley (1997) p.210.
127 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)
128 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)
129 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)
130 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,
131 62.0,48.8,45.5,44.2,52.0,51.5,49.8,48.8,57.2,59.0,53.2,56.0)
132 npk <- data.frame(block=gl(6,4), N=factor(N), P=factor(P), K=factor(K), yield=yield)
133 npk.aov <- aov(yield ~ block + N*P*K, npk)
134 op <- options(contrasts=c("contr.helmert", "contr.treatment"))
135 npk.aovE <- aov(yield ~ N*P*K + Error(block), npk)
140 print(xtable(npk.aov),floating=FALSE)
143 \subsubsection{anova object}
145 print(xtable(anova(npk.aov)),floating=FALSE)
148 \subsubsection{Another anova object}
150 print(xtable(summary(npk.aov)),floating=FALSE)
157 print(xtable(npk.aovE),floating=FALSE)
162 print(xtable(summary(npk.aovE)),floating=FALSE)
169 ## Taken from help(lm) in R 1.1.1
170 ## Annette Dobson (1990) "An Introduction to Generalized Linear Models".
171 ## Page 9: Plant Weight Data.
172 ctl <- c(4.17,5.58,5.18,6.11,4.50,4.61,5.17,4.53,5.33,5.14)
173 trt <- c(4.81,4.17,4.41,3.59,5.87,3.83,6.03,4.89,4.32,4.69)
174 group <- gl(2,10,20, labels=c("Ctl","Trt"))
175 weight <- c(ctl, trt)
176 lm.D9 <- lm(weight ~ group)
179 print(xtable(lm.D9),floating=FALSE)
184 print(xtable(anova(lm.D9)),floating=FALSE)
187 \subsection{More glm}
191 ## Taken from help(glm) in R 1.1.1
192 ## Annette Dobson (1990) "An Introduction to Generalized Linear Models".
193 ## Page 93: Randomized Controlled Trial :
194 counts <- c(18,17,15,20,10,20,25,13,12)
197 d.AD <- data.frame(treatment, outcome, counts)
198 glm.D93 <- glm(counts ~ outcome + treatment, family=poisson())
201 print(xtable(glm.D93,align="r|llrc"),floating=FALSE)
206 if(require(stats,quietly=TRUE)) {
207 ## Demonstrate prcomp
208 ## Taken from help(prcomp) in mva package of R 1.1.1
210 pr1 <- prcomp(USArrests)
214 if(require(stats,quietly=TRUE)) {
215 print(xtable(pr1),floating=FALSE)
222 print(xtable(summary(pr1)),floating=FALSE)
228 # ## Demonstrate princomp
229 # ## Taken from help(princomp) in mva package of R 1.1.1
230 # pr2 <- princomp(USArrests)
233 \subsection{Time series}
236 temp.ts <- ts(cumsum(1+round(rnorm(100), 0)), start = c(1954, 7), frequency=12)
237 temp.table <- xtable(temp.ts,digits=0)
238 caption(temp.table) <- "Time series example"
241 print(temp.table,floating=FALSE)
243 <<savetofile,echo=FALSE>>=
245 for(i in c("latex","html")) {
246 outFileName <- paste("xtable.",ifelse(i=="latex","tex",i),sep="")
247 print(xtable(lm.D9),type=i,file=outFileName,append=TRUE,latex.environment=NULL)
248 print(xtable(lm.D9),type=i,file=outFileName,append=TRUE,latex.environment="")
249 print(xtable(lm.D9),type=i,file=outFileName,append=TRUE,latex.environment="center")
250 print(xtable(anova(glm.D93,test="Chisq")),type=i,file=outFileName,append=TRUE)
251 print(xtable(anova(glm.D93)),hline.after=c(1),size="small",type=i,file=outFileName,append=TRUE)
252 # print(xtable(pr2),type=i,file=outFileName,append=TRUE)
257 \section{Sanitization}
259 insane <- data.frame(Name=c("Ampersand","Greater than","Less than","Underscore","Per cent","Dollar","Backslash","Hash", "Caret", "Tilde","Left brace","Right brace"),
260 Character = I(c("&",">", "<", "_", "%", "$", "\\", "#", "^", "~","{","}")))
261 colnames(insane)[2] <- paste(insane[,2],collapse="")
264 <<pxti,results=tex>>=
265 print( xtable(insane))
267 Sometimes you might want to have your own sanitization function
269 wanttex <- xtable(data.frame( label=paste("Value_is $10^{-",1:3,"}$",sep="")))
272 print(wanttex,sanitize.text.function=function(str)gsub("_","\\_",str,fixed=TRUE))
275 \subsection{Markup in tables}
277 Markup can be kept in tables, including column and row names, by using a custom sanitize.text.function:
280 mat <- round(matrix(c(0.9, 0.89, 200, 0.045, 2.0), c(1, 5)), 4)
281 rownames(mat) <- "$y_{t-1}$"
282 colnames(mat) <- c("$R^2$", "$\\bar{R}^2$", "F-stat", "S.E.E", "DW")
286 print(mat, sanitize.text.function = function(x){x})
289 % By David Dahl to demonstrate contribution from David Whitting, 2007-10-09.
290 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:
292 money <- matrix(c("$1,000","$900","$100"),ncol=3,dimnames=list("$\\alpha$",c("Income (US$)","Expenses (US$)","Profit (US$)")))
295 print(xtable(money),sanitize.rownames.function=function(x) {x})
298 \section{Format examples}
299 \subsection{Adding a centering environment }
301 print(xtable(lm.D9,caption="\\tt latex.environment=NULL"),latex.environment=NULL)
302 print(xtable(lm.D9,caption="\\tt latex.environment=\"\""),latex.environment="")
303 print(xtable(lm.D9,caption="\\tt latex.environment=\"center\""),latex.environment="center")
305 \subsection{Column alignment}
308 tli.table <- xtable(tli[1:10,])
311 align(tli.table) <- rep("r",6)
314 print(tli.table,floating=FALSE)
316 \subsubsection{Single string and column lines}
318 align(tli.table) <- "|rrl|l|lr|"
321 print(tli.table,floating=FALSE)
323 \subsubsection{Fixed width columns}
325 align(tli.table) <- "|rr|lp{3cm}l|r|"
328 print(tli.table,floating=FALSE)
331 \subsection{Significant digits}
334 Specify with a single argument
336 digits(tli.table) <- 3
339 print(tli.table,floating=FALSE,)
343 or one for each column, counting the row names
345 digits(tli.table) <- 1:(ncol(tli)+1)
348 print(tli.table,floating=FALSE,)
354 digits(tli.table) <- matrix( 0:4, nrow = 10, ncol = ncol(tli)+1 )
357 print(tli.table,floating=FALSE,)
360 \subsection{Suppress row names}
362 print((tli.table),include.rownames=FALSE,floating=FALSE)
365 If you want a vertical line on the left, you need to change the align attribute.
367 align(tli.table) <- "|r|r|lp{3cm}l|r|"
370 print((tli.table),include.rownames=FALSE,floating=FALSE)
373 Revert the alignment to what is was before.
375 align(tli.table) <- "|rr|lp{3cm}l|r|"
378 \subsection{Suppress column names}
380 print((tli.table),include.colnames=FALSE,floating=FALSE)
383 Note the doubled header lines which can be suppressed with, eg,
385 print(tli.table,include.colnames=FALSE,floating=FALSE,hline.after=c(0,nrow(tli.table)))
388 \subsection{Suppress row and column names}
390 print((tli.table),include.colnames=FALSE,include.rownames=FALSE,floating=FALSE)
393 \subsection{Rotate row and column names}
394 The {\tt rotate.rownames } and {\tt rotate.colnames} arguments can be
395 used to rotate the row and/or column names.
398 print((tli.table),rotate.rownames=TRUE,rotate.colnames=TRUE)
401 \subsection{Horizontal lines}
403 \subsubsection{Line locations}
405 Use the {\tt hline.after} argument to specify the position of the horizontal lines.
408 print(xtable(anova(glm.D93)),hline.after=c(1),floating=FALSE)
411 \subsubsection{Line styles}
413 The \LaTeX package {\tt booktabs} can be used to specify different
414 line style tags for top, middle, and bottom lines. Specifying
415 {\tt booktabs = TRUE} will lead to separate tags being generated
416 for the three line types.
418 Insert \verb|\usepackage{booktabs}| in your \LaTeX preamble and
419 define the {\tt toprule}, {\tt midrule}, and {\tt bottomrule}
420 tags to specify the line styles.
423 print(tli.table , booktabs=TRUE)
426 \subsection{Table-level \LaTeX}
428 print(xtable(anova(glm.D93)),size="small",floating=FALSE)
432 \subsection{Long tables}
433 Remember to insert \verb|\usepackage{longtable}| in your \LaTeX preamble.
437 ## Demonstration of longtable support.
438 x <- matrix(rnorm(1000), ncol = 10)
439 x.big <- xtable(x,label='tabbig',
440 caption='Example of longtable spanning several pages')
443 print(x.big,tabular.environment='longtable',floating=FALSE)
447 %% The column name alignment is off in the following example.
448 %% It needs some revision before exposing it. - CR, 7/2/2012
451 %\subsubsection{Long tables with the header on each page}
453 %The {\tt add.to.row} argument can be used to display the header
454 %for a long table on each page, and to add a "continued" footer
455 %on all pages except the last page.
459 %x<-matrix(rnorm(1000), ncol = 10)
461 %addtorow$pos<-list()
462 %addtorow$pos[[1]]<-c(0)
463 %addtorow$command<-c(paste(
467 % " {\\footnotesize Continued on next page} \n",
469 % " \\endlastfoot \n",sep=""))
470 %x.big2 <- xtable(x, label = "tabbig2",
471 % caption = "Example of longtable with the header on each page")
472 %print(x.big2, tabular.environment = "longtable", floating = FALSE,
473 %include.rownames=FALSE, add.to.row=addtorow, hline.after=c(-1) )
476 \subsection{Sideways tables}
477 Remember to insert \verb|\usepackage{rotating}| in your LaTeX preamble.
478 Sideways tables can't be forced in place with the `H' specifier, but you can
479 use the \verb|\clearpage| command to get them fairly nearby.
483 x.small <- xtable(x,label='tabsmall',caption='A sideways table')
487 print(x.small,floating.environment='sidewaystable')
491 \subsection{Rescaled tables}
492 Specify a \code{scalebox} value to rescale the table.
496 x.rescale <- xtable(x,label='tabrescaled',caption='A rescaled table')
500 print(x.rescale, scalebox=0.7)
503 \subsection{Table Width}
504 The {\tt tabularx} tabular environment provides more alignment options,
505 and has a \code{width} argument to specify the table width.
507 Remember to insert \verb|\usepackage{tabularx}| in your \LaTeX preamble.
510 df.width <- data.frame(
511 "label 1 with much more text than is needed" = c("item 1", "A"),
512 "label 2 is also very long" = c("item 2","B"),
513 "label 3" = c("item 3","C"),
514 "label 4" = c("item 4 but again with too much text","D"),
517 x.width <- xtable(df.width,
518 caption="Using the 'tabularx' environment")
519 align(x.width) <- "|l|X|X|l|X|"
523 print(x.width, tabular.environment="tabularx",
527 \section{Suppressing Printing}
528 By default the {\tt print} method will print the LaTeX or HTML to standard
529 output and also return the character strings invisibly. The printing to
530 standard output can be suppressed by specifying {\tt print.results = FALSE}.
533 x.out <- print(tli.table, print.results = FALSE)
536 Formatted output can also be captured without printing with the
537 {\tt toLatex} method. This function returns an object of class
541 x.ltx <- toLatex(tli.table)
546 \section{Acknowledgements}
547 Most of the examples in this gallery are taken from the {\tt xtable} documentation.
548 \section{R Session information}
550 toLatex(sessionInfo())