]> git.donarmstrong.com Git - xtable.git/blob - pkg/inst/doc/xtableGallery.snw
79d1626e6f43c9680a323d11705e8216391e2a56
[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{hyperref}
35 \begin{document}
36
37 \maketitle
38 \section{Summary}
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.
42
43 <<>>=
44 library(xtable)
45 @
46
47 \section{Gallery}
48 \subsection{Data frame}
49 Load example dataset
50 <<>>=
51 data(tli)
52
53 ## Demonstrate data.frame
54 tli.table <- xtable(tli[1:10,])
55 digits(tli.table)[c(2,6)] <- 0
56 @
57 <<results=tex>>=
58 print(tli.table,floating=FALSE)
59 @
60
61 \subsection{Matrix}
62 <<>>=
63 design.matrix <- model.matrix(~ sex*grade, data=tli[1:10,])
64 design.table <- xtable(design.matrix)
65 @
66 <<results=tex>>=
67 print(design.table,floating=FALSE)
68 @
69
70 \subsection{aov}
71 <<>>=
72 fm1 <- aov(tlimth ~ sex + ethnicty + grade + disadvg, data=tli)
73 fm1.table <- xtable(fm1)
74 @
75 <<results=tex>>=
76 print(fm1.table,floating=FALSE)
77 @
78 \subsection{lm}
79 <<>>=
80 fm2 <- lm(tlimth ~ sex*ethnicty, data=tli)
81 fm2.table <- xtable(fm2)
82 @
83 <<results=tex>>=
84 print(fm2.table,floating=FALSE)
85 @
86 \subsubsection{anova object}
87
88 <<results=tex>>=
89 print(xtable(anova(fm2)),floating=FALSE)
90 @
91 \subsubsection{Another anova object}
92 <<>>=
93 fm2b <- lm(tlimth ~ ethnicty, data=tli)
94 @
95 <<results=tex>>=
96 print(xtable(anova(fm2b,fm2)),floating=FALSE)
97 @
98
99
100 \subsection{glm}
101
102 <<>>=
103
104 ## Demonstrate glm
105 fm3 <- glm(disadvg ~ ethnicty*grade, data=tli, family=binomial())
106 fm3.table <- xtable(fm3)
107 @
108 <<results=tex>>=
109 print(fm3.table,floating=FALSE)
110 @
111
112 \subsubsection{anova object}
113 @
114 <<results=tex>>=
115 print(xtable(anova(fm3)),floating=FALSE)
116 @
117
118
119 \subsection{More aov}
120 <<>>=
121
122 ## Demonstrate 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)
134 options(op)
135 #summary(npk.aov)
136 @
137 <<results=tex>>=
138 print(xtable(npk.aov),floating=FALSE)
139 @
140
141 \subsubsection{anova object}
142 <<results=tex>>=
143 print(xtable(anova(npk.aov)),floating=FALSE)
144 @
145
146 \subsubsection{Another anova object}
147 <<results=tex>>=
148 print(xtable(summary(npk.aov)),floating=FALSE)
149 @
150
151 <<>>=
152 #summary(npk.aovE)
153 @
154 <<results=tex>>=
155 print(xtable(npk.aovE),floating=FALSE)
156 @
157
158
159 <<results=tex>>=
160 print(xtable(summary(npk.aovE)),floating=FALSE)
161 @
162
163 \subsection{More lm}
164 <<>>=
165
166 ## Demonstrate lm
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)
175 @
176 <<results=tex>>=
177 print(xtable(lm.D9),floating=FALSE)
178 @
179
180
181 <<results=tex>>=
182 print(xtable(anova(lm.D9)),floating=FALSE)
183 @
184
185 \subsection{More glm}
186 <<>>=
187
188 ## Demonstrate 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)
193 outcome <- gl(3,1,9)
194 treatment <- gl(3,3)
195 d.AD <- data.frame(treatment, outcome, counts)
196 glm.D93 <- glm(counts ~ outcome + treatment, family=poisson())
197 @
198 <<results=tex>>=
199 print(xtable(glm.D93,align="r|llrc"),floating=FALSE)
200 @
201
202 \subsection{prcomp}
203 <<prcomp>>=
204 if(require(stats,quietly=TRUE)) {
205   ## Demonstrate prcomp
206   ## Taken from help(prcomp) in mva package of R 1.1.1
207   data(USArrests)
208   pr1 <- prcomp(USArrests)
209 }
210 @
211 <<results=tex>>=
212 if(require(stats,quietly=TRUE)) {
213   print(xtable(pr1),floating=FALSE)
214 }
215 @
216
217
218 @
219 <<results=tex>>=
220   print(xtable(summary(pr1)),floating=FALSE)
221 @
222
223
224
225 <<>>=
226 #  ## Demonstrate princomp
227 #  ## Taken from help(princomp) in mva package of R 1.1.1
228 #  pr2 <- princomp(USArrests)
229 #  print(xtable(pr2))
230 @
231 \subsection{Time series}
232
233 <<>>=
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"
237 @
238 <<results=tex>>=
239     print(temp.table,floating=FALSE)
240 @
241 <<savetofile,echo=FALSE>>=
242 if (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)
251          }
252
253 @
254
255 \section{Sanitization}
256 <<>>=
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="")
260 @
261
262 <<pxti,results=tex>>=
263 print( xtable(insane))
264 @
265 Sometimes you might want to have your own sanitization function
266 <<>>=
267 wanttex <- xtable(data.frame( label=paste("Value_is $10^{-",1:3,"}$",sep="")))
268 @
269 <<results=tex>>=
270 print(wanttex,sanitize.text.function=function(str)gsub("_","\\_",str,fixed=TRUE))
271 @
272
273 \subsection{Markup in tables}
274
275 Markup can be kept in tables, including column and row names, by using a custom sanitize.text.function:
276
277 <<>>=
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")
281 mat <- xtable(mat)
282 @
283 <<results=tex>>=
284 print(mat, sanitize.text.function = function(x){x})
285 @
286
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:
289 <<>>=
290 money <- matrix(c("$1,000","$900","$100"),ncol=3,dimnames=list("$\\alpha$",c("Income (US$)","Expenses (US$)","Profit (US$)")))
291 @
292 <<results=tex>>=
293 print(xtable(money),sanitize.rownames.function=function(x) {x})
294 @
295
296 \section{Format examples}
297 \subsection{Adding a centering environment }
298 <<results=tex>>=
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")
302 @
303 \subsection{Column alignment}
304
305 <<>>=
306 tli.table <- xtable(tli[1:10,])
307 @
308 <<>>=
309 align(tli.table) <- rep("r",6)
310 @
311 <<results=tex>>=
312 print(tli.table,floating=FALSE)
313 @
314 \subsubsection{Single string and column lines}
315 <<>>=
316 align(tli.table) <- "|rrl|l|lr|"
317 @
318 <<results=tex>>=
319 print(tli.table,floating=FALSE)
320 @
321 \subsubsection{Fixed width columns}
322 <<>>=
323 align(tli.table) <- "|rr|lp{3cm}l|r|"
324 @
325 <<results=tex>>=
326 print(tli.table,floating=FALSE)
327 @
328
329 \subsection{Significant digits}
330
331
332 Specify with a single argument 
333 <<>>=
334 digits(tli.table) <- 3
335 @
336 <<results=tex>>=
337 print(tli.table,floating=FALSE,)
338 @
339
340
341 or one for each column, counting the row names
342 <<>>=
343 digits(tli.table) <- 1:(ncol(tli)+1)
344 @
345 <<results=tex>>=
346 print(tli.table,floating=FALSE,)
347 @
348
349
350 or as a full matrix
351 <<>>=
352 digits(tli.table) <- matrix( 0:4, nrow = 10, ncol = ncol(tli)+1 )
353 @
354 <<results=tex>>=
355 print(tli.table,floating=FALSE,)
356 @
357
358 \subsection{Suppress row names}
359 <<results=tex>>=
360 print((tli.table),include.rownames=FALSE,floating=FALSE)
361 @
362
363 If you want a vertical line on the left, you need to change the align attribute.
364 <<>>=
365 align(tli.table) <- "|r|r|lp{3cm}l|r|"
366 @
367 <<results=tex>>=
368 print((tli.table),include.rownames=FALSE,floating=FALSE)
369 @
370
371 Revert the alignment to what is was before.
372 <<>>=
373 align(tli.table) <- "|rr|lp{3cm}l|r|"
374 @
375
376 \subsection{Suppress column names}
377 <<results=tex>>=
378 print((tli.table),include.colnames=FALSE,floating=FALSE)
379 @
380 \\
381 Note the doubled header lines which can be suppressed with, eg,
382 <<results=tex>>=
383 print(tli.table,include.colnames=FALSE,floating=FALSE,hline.after=c(0,nrow(tli.table)))
384 @
385
386 \subsection{Suppress row and column names}
387 <<results=tex>>=
388 print((tli.table),include.colnames=FALSE,include.rownames=FALSE,floating=FALSE)
389 @
390
391 \subsection{Rotate row and column names}
392 The {\tt rotate.rownames } and {\tt rotate.colnames} arguments can be
393 used to rotate the row and/or column names.
394
395 <<results=tex>>=
396 print((tli.table),rotate.rownames=TRUE,rotate.colnames=TRUE)
397 @
398
399 \subsection{Horizontal lines}
400 <<results=tex>>=
401 print(xtable(anova(glm.D93)),hline.after=c(1),floating=FALSE)
402 @
403
404 \subsection{Table-level \LaTeX}
405 <<results=tex>>=
406 print(xtable(anova(glm.D93)),size="small",floating=FALSE)
407 @
408
409
410 \subsection{Long tables}
411 Remember to insert \verb|\usepackage{longtable}| in your LaTeX preamble.
412 See Table \ref{tabbig}.
413
414 <<longtable>>=
415
416 ## Demonstration of longtable support.
417 x <- matrix(rnorm(1000), ncol = 10)
418 x.big <- xtable(x,label='tabbig',
419         caption='Example of longtable spanning several pages')
420 @
421 <<results=tex>>=
422 print(x.big,tabular.environment='longtable',floating=FALSE)
423 @
424
425 \subsubsection{Long tables with headers on each page}
426
427 The {\tt add.to.row} argument can be used to display the header 
428 for a long table on each page, and to add a "continued" footer
429 on all pages except the last page.
430
431 <<results=tex>>=
432 library(xtable)
433 x<-matrix(rnorm(1000), ncol = 10)
434
435 addtorow<-list()
436 addtorow$pos<-list()
437 addtorow$pos[[1]]<-c(0)
438 addtorow$command<-c(paste(
439     "\\hline \n",
440     "  \\endhead \n",
441     "  \\hline \n",
442     "  {\\footnotesize Continued on next page} \n",
443     "  \\endfoot \n",
444     "  \\endlastfoot \n",sep=""))
445 x.big <- xtable(x, label = "tabbig", 
446     caption = "Example of longtable spanning several pages")
447 print(x.big, tabular.environment = "longtable", floating = FALSE,
448 include.rownames=FALSE,add.to.row=addtorow,hline.after=c(-1) )
449 @
450
451 \subsection{Sideways tables} 
452 Remember to insert \verb|\usepackage{rotating}| in your LaTeX preamble.
453 Sideways tables can't be forced in place with the `H' specifier, but you can 
454 use the \verb|\clearpage| command to get them fairly nearby.
455 See Table \ref{tabsmall}.
456
457 <<>>=
458 x <- x[1:30,]
459 x.small <- xtable(x,label='tabsmall',caption='A sideways table')
460 @
461
462 <<results=tex>>=
463 print(x.small,floating.environment='sidewaystable')
464 @
465 \clearpage
466
467 \section{Suppressing Printing}
468 By default the {\tt print} method will print the LaTeX or HTML to standard
469 output and also return the character strings invisibly.  The printing to
470 standard output can be suppressed by specifying {\tt print.results = FALSE}.
471
472 <<>>=
473 # This will print the output and store it.
474 x.out <- print(tli.table)
475
476 # This will just store the value.
477 x.out2 <- print(tli.table, print.results = FALSE)
478 @
479
480 Formatted output can also be captured without printing with the
481 {\tt toLatex} method.  This function returns an object of class
482 {\tt "Latex"}.
483
484 <<>>=
485 x.ltx <- toLatex(tli.table)
486 class(x.ltx)
487 x.ltx
488 @
489
490 \section{Acknowledgements}
491 Most of the examples in this gallery are taken from the {\tt xtable} documentation.
492 \section{R Session information}
493 <<results=tex>>=
494 toLatex(sessionInfo())
495 @
496 \end{document}