]> git.donarmstrong.com Git - xtable.git/blob - pkg/inst/doc/xtableGallery.snw
Submitting multiple changes as R-Forge SVN has been down. Added the use of getOption...
[xtable.git] / pkg / inst / doc / xtableGallery.snw
1 %\VignetteIndexEntry{xtable Gallery}
2 %\VignetteDepends{xtable}
3 %\VignetteKeywords{LaTeX,HTML,table}
4 %\VignettePackage{xtable}
5
6 %**************************************************************************
7 %
8 % # $Id:$
9
10 % $Revision:  $
11 % $Author: $
12 % $Date:  $
13
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)
19 }
20 makeme()
21 @
22
23 \documentclass[letterpaper]{article}
24
25 \title{
26 The xtable gallery
27 }
28 \author{Jonathan Swinton <jonathan@swintons.net>\\ with small contributions from others}
29
30 \usepackage{Sweave}
31 \SweaveOpts{prefix.string=figdir/fig,debug=TRUE,eps=FALSE,echo=TRUE}
32 \usepackage{rotating} 
33 \usepackage{longtable} 
34 \usepackage{booktabs}
35 %\usepackage{hyperref}
36 \begin{document}
37
38 \maketitle
39 \section{Summary}
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.
43
44 <<>>=
45 library(xtable)
46 @
47
48 \section{Gallery}
49 \subsection{Data frame}
50 Load example dataset
51 <<>>=
52 data(tli)
53
54 ## Demonstrate data.frame
55 tli.table <- xtable(tli[1:10,])
56 digits(tli.table)[c(2,6)] <- 0
57 @
58 <<results=tex>>=
59 print(tli.table,floating=FALSE)
60 @
61
62 \subsection{Matrix}
63 <<>>=
64 design.matrix <- model.matrix(~ sex*grade, data=tli[1:10,])
65 design.table <- xtable(design.matrix)
66 @
67 <<results=tex>>=
68 print(design.table,floating=FALSE)
69 @
70
71 \subsection{aov}
72 <<>>=
73 fm1 <- aov(tlimth ~ sex + ethnicty + grade + disadvg, data=tli)
74 fm1.table <- xtable(fm1)
75 @
76 <<results=tex>>=
77 print(fm1.table,floating=FALSE)
78 @
79 \subsection{lm}
80 <<>>=
81 fm2 <- lm(tlimth ~ sex*ethnicty, data=tli)
82 fm2.table <- xtable(fm2)
83 @
84 <<results=tex>>=
85 print(fm2.table,floating=FALSE)
86 @
87 \subsubsection{anova object}
88
89 <<results=tex>>=
90 print(xtable(anova(fm2)),floating=FALSE)
91 @
92 \subsubsection{Another anova object}
93 <<>>=
94 fm2b <- lm(tlimth ~ ethnicty, data=tli)
95 @
96 <<results=tex>>=
97 print(xtable(anova(fm2b,fm2)),floating=FALSE)
98 @
99
100
101 \subsection{glm}
102
103 <<>>=
104
105 ## Demonstrate glm
106 fm3 <- glm(disadvg ~ ethnicty*grade, data=tli, family=binomial())
107 fm3.table <- xtable(fm3)
108 @
109 <<results=tex>>=
110 print(fm3.table,floating=FALSE)
111 @
112
113 \subsubsection{anova object}
114 @
115 <<results=tex>>=
116 print(xtable(anova(fm3)),floating=FALSE)
117 @
118
119
120 \subsection{More aov}
121 <<>>=
122
123 ## Demonstrate 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)
135 options(op)
136 #summary(npk.aov)
137 @
138 <<results=tex>>=
139 print(xtable(npk.aov),floating=FALSE)
140 @
141
142 \subsubsection{anova object}
143 <<results=tex>>=
144 print(xtable(anova(npk.aov)),floating=FALSE)
145 @
146
147 \subsubsection{Another anova object}
148 <<results=tex>>=
149 print(xtable(summary(npk.aov)),floating=FALSE)
150 @
151
152 <<>>=
153 #summary(npk.aovE)
154 @
155 <<results=tex>>=
156 print(xtable(npk.aovE),floating=FALSE)
157 @
158
159
160 <<results=tex>>=
161 print(xtable(summary(npk.aovE)),floating=FALSE)
162 @
163
164 \subsection{More lm}
165 <<>>=
166
167 ## Demonstrate lm
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)
176 @
177 <<results=tex>>=
178 print(xtable(lm.D9),floating=FALSE)
179 @
180
181
182 <<results=tex>>=
183 print(xtable(anova(lm.D9)),floating=FALSE)
184 @
185
186 \subsection{More glm}
187 <<>>=
188
189 ## Demonstrate 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)
194 outcome <- gl(3,1,9)
195 treatment <- gl(3,3)
196 d.AD <- data.frame(treatment, outcome, counts)
197 glm.D93 <- glm(counts ~ outcome + treatment, family=poisson())
198 @
199 <<results=tex>>=
200 print(xtable(glm.D93,align="r|llrc"),floating=FALSE)
201 @
202
203 \subsection{prcomp}
204 <<prcomp>>=
205 if(require(stats,quietly=TRUE)) {
206   ## Demonstrate prcomp
207   ## Taken from help(prcomp) in mva package of R 1.1.1
208   data(USArrests)
209   pr1 <- prcomp(USArrests)
210 }
211 @
212 <<results=tex>>=
213 if(require(stats,quietly=TRUE)) {
214   print(xtable(pr1),floating=FALSE)
215 }
216 @
217
218
219 @
220 <<results=tex>>=
221   print(xtable(summary(pr1)),floating=FALSE)
222 @
223
224
225
226 <<>>=
227 #  ## Demonstrate princomp
228 #  ## Taken from help(princomp) in mva package of R 1.1.1
229 #  pr2 <- princomp(USArrests)
230 #  print(xtable(pr2))
231 @
232 \subsection{Time series}
233
234 <<>>=
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"
238 @
239 <<results=tex>>=
240     print(temp.table,floating=FALSE)
241 @
242 <<savetofile,echo=FALSE>>=
243 if (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)
252          }
253
254 @
255
256 \section{Sanitization}
257 <<>>=
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="")
261 @
262
263 <<pxti,results=tex>>=
264 print( xtable(insane))
265 @
266 Sometimes you might want to have your own sanitization function
267 <<>>=
268 wanttex <- xtable(data.frame( label=paste("Value_is $10^{-",1:3,"}$",sep="")))
269 @
270 <<results=tex>>=
271 print(wanttex,sanitize.text.function=function(str)gsub("_","\\_",str,fixed=TRUE))
272 @
273
274 \subsection{Markup in tables}
275
276 Markup can be kept in tables, including column and row names, by using a custom sanitize.text.function:
277
278 <<>>=
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")
282 mat <- xtable(mat)
283 @
284 <<results=tex>>=
285 print(mat, sanitize.text.function = function(x){x})
286 @
287
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:
290 <<>>=
291 money <- matrix(c("$1,000","$900","$100"),ncol=3,dimnames=list("$\\alpha$",c("Income (US$)","Expenses (US$)","Profit (US$)")))
292 @
293 <<results=tex>>=
294 print(xtable(money),sanitize.rownames.function=function(x) {x})
295 @
296
297 \section{Format examples}
298 \subsection{Adding a centering environment }
299 <<results=tex>>=
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")
303 @
304 \subsection{Column alignment}
305
306 <<>>=
307 tli.table <- xtable(tli[1:10,])
308 @
309 <<>>=
310 align(tli.table) <- rep("r",6)
311 @
312 <<results=tex>>=
313 print(tli.table,floating=FALSE)
314 @
315 \subsubsection{Single string and column lines}
316 <<>>=
317 align(tli.table) <- "|rrl|l|lr|"
318 @
319 <<results=tex>>=
320 print(tli.table,floating=FALSE)
321 @
322 \subsubsection{Fixed width columns}
323 <<>>=
324 align(tli.table) <- "|rr|lp{3cm}l|r|"
325 @
326 <<results=tex>>=
327 print(tli.table,floating=FALSE)
328 @
329
330 \subsection{Significant digits}
331
332
333 Specify with a single argument 
334 <<>>=
335 digits(tli.table) <- 3
336 @
337 <<results=tex>>=
338 print(tli.table,floating=FALSE,)
339 @
340
341
342 or one for each column, counting the row names
343 <<>>=
344 digits(tli.table) <- 1:(ncol(tli)+1)
345 @
346 <<results=tex>>=
347 print(tli.table,floating=FALSE,)
348 @
349
350
351 or as a full matrix
352 <<>>=
353 digits(tli.table) <- matrix( 0:4, nrow = 10, ncol = ncol(tli)+1 )
354 @
355 <<results=tex>>=
356 print(tli.table,floating=FALSE,)
357 @
358
359 \subsection{Suppress row names}
360 <<results=tex>>=
361 print((tli.table),include.rownames=FALSE,floating=FALSE)
362 @
363
364 If you want a vertical line on the left, you need to change the align attribute.
365 <<>>=
366 align(tli.table) <- "|r|r|lp{3cm}l|r|"
367 @
368 <<results=tex>>=
369 print((tli.table),include.rownames=FALSE,floating=FALSE)
370 @
371
372 Revert the alignment to what is was before.
373 <<>>=
374 align(tli.table) <- "|rr|lp{3cm}l|r|"
375 @
376
377 \subsection{Suppress column names}
378 <<results=tex>>=
379 print((tli.table),include.colnames=FALSE,floating=FALSE)
380 @
381 \\
382 Note the doubled header lines which can be suppressed with, eg,
383 <<results=tex>>=
384 print(tli.table,include.colnames=FALSE,floating=FALSE,hline.after=c(0,nrow(tli.table)))
385 @
386
387 \subsection{Suppress row and column names}
388 <<results=tex>>=
389 print((tli.table),include.colnames=FALSE,include.rownames=FALSE,floating=FALSE)
390 @
391
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.
395
396 <<results=tex>>=
397 print((tli.table),rotate.rownames=TRUE,rotate.colnames=TRUE)
398 @
399
400 \subsection{Horizontal lines}
401
402 \subsubsection{Line locations}
403
404 Use the {\tt hline.after} argument to specify the position of the horizontal lines.
405
406 <<results=tex>>=
407 print(xtable(anova(glm.D93)),hline.after=c(1),floating=FALSE)
408 @
409
410 \subsubsection{Line styles}
411
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.
416
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.
420
421 <<results=tex>>=
422 print(tli.table , booktabs=TRUE)
423 @
424
425 \subsection{Table-level \LaTeX}
426 <<results=tex>>=
427 print(xtable(anova(glm.D93)),size="small",floating=FALSE)
428 @
429
430
431 \subsection{Long tables}
432 Remember to insert \verb|\usepackage{longtable}| in your \LaTeX preamble.
433 See Table \ref{tabbig}.
434
435 <<longtable>>=
436
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')
441 @
442 <<results=tex>>=
443 print(x.big,tabular.environment='longtable',floating=FALSE)
444 @
445
446 \subsubsection{Long tables with the header on each page}
447
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}.
451
452 <<results=tex>>=
453 library(xtable)
454 x<-matrix(rnorm(1000), ncol = 10)
455
456 addtorow<-list()
457 addtorow$pos<-list()
458 addtorow$pos[[1]]<-c(0)
459 addtorow$command<-c(paste(
460     "\\hline \n",
461     "  \\endhead \n",
462     "  \\hline \n",
463     "  {\\footnotesize Continued on next page} \n",
464     "  \\endfoot \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) )
470 @
471
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}.
477
478 <<>>=
479 x <- x[1:30,]
480 x.small <- xtable(x,label='tabsmall',caption='A sideways table')
481 @
482
483 <<results=tex>>=
484 print(x.small,floating.environment='sidewaystable')
485 @
486 \clearpage
487
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}.
492
493 <<>>=
494 x.out <- print(tli.table, print.results = FALSE)
495 @
496
497 Formatted output can also be captured without printing with the
498 {\tt toLatex} method.  This function returns an object of class
499 {\tt "Latex"}.
500
501 <<>>=
502 x.ltx <- toLatex(tli.table)
503 class(x.ltx)
504 x.ltx
505 @
506
507 \section{Acknowledgements}
508 Most of the examples in this gallery are taken from the {\tt xtable} documentation.
509 \section{R Session information}
510 <<results=tex>>=
511 toLatex(sessionInfo())
512 @
513 \end{document}