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}
35 %\usepackage{hyperref}
40 This document gives a gallery of tables which can be made
41 by using the {\tt xtable} package to create \LaTeX\ output.
42 It doubles as a regression check for the package.
49 \subsection{Data frame}
54 ## Demonstrate data.frame
55 tli.table <- xtable(tli[1:10,])
56 digits(tli.table)[c(2,6)] <- 0
59 print(tli.table,floating=FALSE)
64 design.matrix <- model.matrix(~ sex*grade, data=tli[1:10,])
65 design.table <- xtable(design.matrix)
68 print(design.table,floating=FALSE)
73 fm1 <- aov(tlimth ~ sex + ethnicty + grade + disadvg, data=tli)
74 fm1.table <- xtable(fm1)
77 print(fm1.table,floating=FALSE)
81 fm2 <- lm(tlimth ~ sex*ethnicty, data=tli)
82 fm2.table <- xtable(fm2)
85 print(fm2.table,floating=FALSE)
87 \subsubsection{anova object}
90 print(xtable(anova(fm2)),floating=FALSE)
92 \subsubsection{Another anova object}
94 fm2b <- lm(tlimth ~ ethnicty, data=tli)
97 print(xtable(anova(fm2b,fm2)),floating=FALSE)
106 fm3 <- glm(disadvg ~ ethnicty*grade, data=tli, family=binomial())
107 fm3.table <- xtable(fm3)
110 print(fm3.table,floating=FALSE)
113 \subsubsection{anova object}
116 print(xtable(anova(fm3)),floating=FALSE)
120 \subsection{More aov}
124 ## Taken from help(aov) in R 1.1.1
125 ## From Venables and Ripley (1997) p.210.
126 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)
127 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)
128 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)
129 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,
130 62.0,48.8,45.5,44.2,52.0,51.5,49.8,48.8,57.2,59.0,53.2,56.0)
131 npk <- data.frame(block=gl(6,4), N=factor(N), P=factor(P), K=factor(K), yield=yield)
132 npk.aov <- aov(yield ~ block + N*P*K, npk)
133 op <- options(contrasts=c("contr.helmert", "contr.treatment"))
134 npk.aovE <- aov(yield ~ N*P*K + Error(block), npk)
139 print(xtable(npk.aov),floating=FALSE)
142 \subsubsection{anova object}
144 print(xtable(anova(npk.aov)),floating=FALSE)
147 \subsubsection{Another anova object}
149 print(xtable(summary(npk.aov)),floating=FALSE)
156 print(xtable(npk.aovE),floating=FALSE)
161 print(xtable(summary(npk.aovE)),floating=FALSE)
168 ## Taken from help(lm) in R 1.1.1
169 ## Annette Dobson (1990) "An Introduction to Generalized Linear Models".
170 ## Page 9: Plant Weight Data.
171 ctl <- c(4.17,5.58,5.18,6.11,4.50,4.61,5.17,4.53,5.33,5.14)
172 trt <- c(4.81,4.17,4.41,3.59,5.87,3.83,6.03,4.89,4.32,4.69)
173 group <- gl(2,10,20, labels=c("Ctl","Trt"))
174 weight <- c(ctl, trt)
175 lm.D9 <- lm(weight ~ group)
178 print(xtable(lm.D9),floating=FALSE)
183 print(xtable(anova(lm.D9)),floating=FALSE)
186 \subsection{More glm}
190 ## Taken from help(glm) in R 1.1.1
191 ## Annette Dobson (1990) "An Introduction to Generalized Linear Models".
192 ## Page 93: Randomized Controlled Trial :
193 counts <- c(18,17,15,20,10,20,25,13,12)
196 d.AD <- data.frame(treatment, outcome, counts)
197 glm.D93 <- glm(counts ~ outcome + treatment, family=poisson())
200 print(xtable(glm.D93,align="r|llrc"),floating=FALSE)
205 if(require(stats,quietly=TRUE)) {
206 ## Demonstrate prcomp
207 ## Taken from help(prcomp) in mva package of R 1.1.1
209 pr1 <- prcomp(USArrests)
213 if(require(stats,quietly=TRUE)) {
214 print(xtable(pr1),floating=FALSE)
221 print(xtable(summary(pr1)),floating=FALSE)
227 # ## Demonstrate princomp
228 # ## Taken from help(princomp) in mva package of R 1.1.1
229 # pr2 <- princomp(USArrests)
232 \subsection{Time series}
235 temp.ts <- ts(cumsum(1+round(rnorm(100), 0)), start = c(1954, 7), frequency=12)
236 temp.table <- xtable(temp.ts,digits=0)
237 caption(temp.table) <- "Time series example"
240 print(temp.table,floating=FALSE)
242 <<savetofile,echo=FALSE>>=
244 for(i in c("latex","html")) {
245 outFileName <- paste("xtable.",ifelse(i=="latex","tex",i),sep="")
246 print(xtable(lm.D9),type=i,file=outFileName,append=TRUE,latex.environment=NULL)
247 print(xtable(lm.D9),type=i,file=outFileName,append=TRUE,latex.environment="")
248 print(xtable(lm.D9),type=i,file=outFileName,append=TRUE,latex.environment="center")
249 print(xtable(anova(glm.D93,test="Chisq")),type=i,file=outFileName,append=TRUE)
250 print(xtable(anova(glm.D93)),hline.after=c(1),size="small",type=i,file=outFileName,append=TRUE)
251 # print(xtable(pr2),type=i,file=outFileName,append=TRUE)
256 \section{Sanitization}
258 insane <- data.frame(Name=c("Ampersand","Greater than","Less than","Underscore","Per cent","Dollar","Backslash","Hash", "Caret", "Tilde","Left brace","Right brace"),
259 Character = I(c("&",">", "<", "_", "%", "$", "\\", "#", "^", "~","{","}")))
260 colnames(insane)[2] <- paste(insane[,2],collapse="")
263 <<pxti,results=tex>>=
264 print( xtable(insane))
266 Sometimes you might want to have your own sanitization function
268 wanttex <- xtable(data.frame( label=paste("Value_is $10^{-",1:3,"}$",sep="")))
271 print(wanttex,sanitize.text.function=function(str)gsub("_","\\_",str,fixed=TRUE))
274 \subsection{Markup in tables}
276 Markup can be kept in tables, including column and row names, by using a custom sanitize.text.function:
279 mat <- round(matrix(c(0.9, 0.89, 200, 0.045, 2.0), c(1, 5)), 4)
280 rownames(mat) <- "$y_{t-1}$"
281 colnames(mat) <- c("$R^2$", "$\\bar{R}^2$", "F-stat", "S.E.E", "DW")
285 print(mat, sanitize.text.function = function(x){x})
288 % By David Dahl to demonstrate contribution from David Whitting, 2007-10-09.
289 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:
291 money <- matrix(c("$1,000","$900","$100"),ncol=3,dimnames=list("$\\alpha$",c("Income (US$)","Expenses (US$)","Profit (US$)")))
294 print(xtable(money),sanitize.rownames.function=function(x) {x})
297 \section{Format examples}
298 \subsection{Adding a centering environment }
300 print(xtable(lm.D9,caption="\\tt latex.environment=NULL"),latex.environment=NULL)
301 print(xtable(lm.D9,caption="\\tt latex.environment=\"\""),latex.environment="")
302 print(xtable(lm.D9,caption="\\tt latex.environment=\"center\""),latex.environment="center")
304 \subsection{Column alignment}
307 tli.table <- xtable(tli[1:10,])
310 align(tli.table) <- rep("r",6)
313 print(tli.table,floating=FALSE)
315 \subsubsection{Single string and column lines}
317 align(tli.table) <- "|rrl|l|lr|"
320 print(tli.table,floating=FALSE)
322 \subsubsection{Fixed width columns}
324 align(tli.table) <- "|rr|lp{3cm}l|r|"
327 print(tli.table,floating=FALSE)
330 \subsection{Significant digits}
333 Specify with a single argument
335 digits(tli.table) <- 3
338 print(tli.table,floating=FALSE,)
342 or one for each column, counting the row names
344 digits(tli.table) <- 1:(ncol(tli)+1)
347 print(tli.table,floating=FALSE,)
353 digits(tli.table) <- matrix( 0:4, nrow = 10, ncol = ncol(tli)+1 )
356 print(tli.table,floating=FALSE,)
359 \subsection{Suppress row names}
361 print((tli.table),include.rownames=FALSE,floating=FALSE)
364 If you want a vertical line on the left, you need to change the align attribute.
366 align(tli.table) <- "|r|r|lp{3cm}l|r|"
369 print((tli.table),include.rownames=FALSE,floating=FALSE)
372 Revert the alignment to what is was before.
374 align(tli.table) <- "|rr|lp{3cm}l|r|"
377 \subsection{Suppress column names}
379 print((tli.table),include.colnames=FALSE,floating=FALSE)
382 Note the doubled header lines which can be suppressed with, eg,
384 print(tli.table,include.colnames=FALSE,floating=FALSE,hline.after=c(0,nrow(tli.table)))
387 \subsection{Suppress row and column names}
389 print((tli.table),include.colnames=FALSE,include.rownames=FALSE,floating=FALSE)
392 \subsection{Rotate row and column names}
393 The {\tt rotate.rownames } and {\tt rotate.colnames} arguments can be
394 used to rotate the row and/or column names.
397 print((tli.table),rotate.rownames=TRUE,rotate.colnames=TRUE)
400 \subsection{Horizontal lines}
402 \subsubsection{Line locations}
404 Use the {\tt hline.after} argument to specify the position of the horizontal lines.
407 print(xtable(anova(glm.D93)),hline.after=c(1),floating=FALSE)
410 \subsubsection{Line styles}
412 The \LaTeX package {\tt booktabs} can be used to specify different
413 line style tags for top, middle, and bottom lines. Specifying
414 {\tt booktabs = TRUE} will lead to separate tags being generated
415 for the three line types.
417 Insert \verb|\usepackage{booktabs}| in your \LaTeX preamble and
418 define the {\tt toprule}, {\tt midrule}, and {\tt bottomrule}
419 tags to specify the line styles.
422 print(tli.table , booktabs=TRUE)
425 \subsection{Table-level \LaTeX}
427 print(xtable(anova(glm.D93)),size="small",floating=FALSE)
431 \subsection{Long tables}
432 Remember to insert \verb|\usepackage{longtable}| in your \LaTeX preamble.
433 See Table \ref{tabbig}.
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)
446 \subsubsection{Long tables with the header on each page}
448 The {\tt add.to.row} argument can be used to display the header
449 for a long table on each page, and to add a "continued" footer
450 on all pages except the last page. See Table \ref{tabbig2}.
454 x<-matrix(rnorm(1000), ncol = 10)
458 addtorow$pos[[1]]<-c(0)
459 addtorow$command<-c(paste(
463 " {\\footnotesize Continued on next page} \n",
465 " \\endlastfoot \n",sep=""))
466 x.big2 <- xtable(x, label = "tabbig2",
467 caption = "Example of longtable with the header on each page")
468 print(x.big2, tabular.environment = "longtable", floating = FALSE,
469 include.rownames=FALSE,add.to.row=addtorow,hline.after=c(-1) )
472 \subsection{Sideways tables}
473 Remember to insert \verb|\usepackage{rotating}| in your LaTeX preamble.
474 Sideways tables can't be forced in place with the `H' specifier, but you can
475 use the \verb|\clearpage| command to get them fairly nearby.
476 See Table \ref{tabsmall}.
480 x.small <- xtable(x,label='tabsmall',caption='A sideways table')
484 print(x.small,floating.environment='sidewaystable')
488 \section{Suppressing Printing}
489 By default the {\tt print} method will print the LaTeX or HTML to standard
490 output and also return the character strings invisibly. The printing to
491 standard output can be suppressed by specifying {\tt print.results = FALSE}.
494 x.out <- print(tli.table, print.results = FALSE)
497 Formatted output can also be captured without printing with the
498 {\tt toLatex} method. This function returns an object of class
502 x.ltx <- toLatex(tli.table)
507 \section{Acknowledgements}
508 Most of the examples in this gallery are taken from the {\tt xtable} documentation.
509 \section{R Session information}
511 toLatex(sessionInfo())