]> git.donarmstrong.com Git - xtable.git/blob - pkg/vignettes/xtableGallery.Rnw
Applied patches supplied by Martin Gubri for spdep
[xtable.git] / pkg / vignettes / xtableGallery.Rnw
1 %\VignetteIndexEntry{xtable Gallery}\r
2 %\VignetteDepends{xtable}\r
3 %\VignetteKeywords{LaTeX, HTML, table}\r
4 %\VignettePackage{xtable}\r
5 % !Rnw weave = knitr\r
6 % \VignetteEngine{knitr::knitr}\r
7 %**************************************************************************\r
8 \documentclass{article}\r
9 \usepackage[a4paper,height=24cm]{geometry} % geometry first\r
10 \usepackage{array}\r
11 \usepackage{booktabs}\r
12 \usepackage{longtable}\r
13 \usepackage{parskip}\r
14 \usepackage{rotating}\r
15 \usepackage{tabularx}\r
16 \usepackage{titlesec}\r
17 \usepackage{hyperref} % hyperref last\r
18 \titleformat\subsubsection{\bfseries\itshape}{}{0pt}{}\r
19 \newcommand\p{\vspace{2ex}}\r
20 \newcommand\code[1]{\texttt{#1}}\r
21 \newcommand\pkg[1]{\textbf{#1}}\r
22 \setcounter{tocdepth}{2}\r
23 \begin{document}\r
24 \r
25 \title{The \pkg{xtable} Gallery}\r
26 \author{Jonathan Swinton and others}\r
27 \maketitle\r
28 \r
29 \tableofcontents\r
30 \r
31 \newpage\r
32 \r
33 \section{Introduction}\r
34 This document gives a gallery of tables which can be made using the\r
35 \pkg{xtable} package to create \LaTeX\ output. It doubles as a\r
36 regression check for the package.\r
37 \r
38 <<include=FALSE>>=\r
39 library(knitr)\r
40 opts_chunk$set(fig.path='figdir/fig', debug=TRUE, echo=TRUE)\r
41 set.seed(1234)\r
42 @\r
43 \r
44 The first step is to load the package and set an option for this document.\r
45 <<results='asis'>>=\r
46 library(xtable)\r
47 options(xtable.floating = FALSE)\r
48 options(xtable.timestamp = "")\r
49 @\r
50 \r
51 \section{Gallery}\r
52 \subsection{Data frame}\r
53 <<results='asis'>>=\r
54 data(tli)\r
55 xtable(tli[1:10, ])\r
56 @\r
57 \r
58 \subsection{Matrix}\r
59 <<results='asis'>>=\r
60 design.matrix <- model.matrix(~ sex*grade, data = tli[1:10, ])\r
61 xtable(design.matrix, digits = 0)\r
62 @\r
63 \r
64 \newpage\r
65 \subsection{aov}\r
66 <<results='asis'>>=\r
67 fm1 <- aov(tlimth ~ sex + ethnicty + grade + disadvg, data = tli)\r
68 xtable(fm1)\r
69 @\r
70 \r
71 \subsection{lm}\r
72 <<results='asis'>>=\r
73 fm2 <- lm(tlimth ~ sex*ethnicty, data = tli)\r
74 xtable(fm2)\r
75 @\r
76 \r
77 \subsubsection{Anova table (one model)}\r
78 <<results='asis'>>=\r
79 xtable(anova(fm2))\r
80 @\r
81 \r
82 \subsubsection{Anova table (two models)}\r
83 <<results='asis'>>=\r
84 fm2b <- lm(tlimth ~ ethnicty, data = tli)\r
85 xtable(anova(fm2b, fm2))\r
86 @\r
87 \r
88 \subsubsection{Anova list}\r
89 \r
90 <<aovlist>>=\r
91 Block <- gl(8, 4)\r
92 A <- factor(c(0,1,0,1,0,1,0,1,0,1,0,1,0,1,0,1,0,1,0,1,\r
93               0,1,0,1,0,1,0,1,0,1,0,1))\r
94 B <- factor(c(0,0,1,1,0,0,1,1,0,1,0,1,1,0,1,0,0,0,1,1,\r
95               0,0,1,1,0,0,1,1,0,0,1,1))\r
96 C <- factor(c(0,1,1,0,1,0,0,1,0,0,1,1,0,0,1,1,0,1,0,1,\r
97               1,0,1,0,0,0,1,1,1,1,0,0))\r
98 Yield <- c(101, 373, 398, 291, 312, 106, 265, 450, 106, 306, 324, 449,\r
99            272, 89, 407, 338, 87, 324, 279, 471, 323, 128, 423, 334,\r
100            131, 103, 445, 437, 324, 361, 302, 272)\r
101 aovdat <- data.frame(Block, A, B, C, Yield)\r
102 \r
103 old <- getOption("contrasts")\r
104 options(contrasts = c("contr.helmert", "contr.poly"))\r
105 (fit <- aov(Yield ~ A*B*C + Error(Block), data = aovdat))\r
106 class(fit)\r
107 summary(fit)\r
108 options(contrasts = old)\r
109 @\r
110 \r
111 \p\r
112 \r
113 <<xtableaovlist, results='asis'>>=\r
114 xtable(fit)\r
115 @\r
116 \r
117 \r
118 \newpage\r
119 \subsection{glm}\r
120 <<results='asis'>>=\r
121 fm3 <- glm(disadvg ~ ethnicty*grade, data = tli, family = binomial)\r
122 xtable(fm3)\r
123 @\r
124 \r
125 \subsubsection{Analysis of deviance}\r
126 <<results='asis'>>=\r
127 xtable(anova(fm3))\r
128 @\r
129 \r
130 \subsection{prcomp}\r
131 <<results='asis'>>=\r
132 pr1 <- prcomp(USArrests)\r
133 xtable(pr1)\r
134 @\r
135 \r
136 \p\r
137 <<results='asis'>>=\r
138 xtable(summary(pr1))\r
139 @\r
140 \r
141 <<include=FALSE>>=\r
142 # pr2 <- princomp(USArrests)\r
143 # xtable(pr2)\r
144 @\r
145 \r
146 \newpage\r
147 \r
148 \subsection{Time series}\r
149 <<results='asis'>>=\r
150 temp.ts <- ts(cumsum(1 + round(rnorm(100), 0)),\r
151               start = c(1954, 7), frequency = 12)\r
152 temp.table <- xtable(temp.ts, digits = 0)\r
153 temp.table\r
154 @\r
155 \r
156 \subsection{Flat tables}\r
157 \label{sec:flat-tables}\r
158 \r
159 See the \textbf{Details} section of the help for \code{ftable} for a\r
160 description of these tables, which are flat versions of\r
161 multi-dimensional contingency tables. They require special methods to\r
162 enable them to be printed using \pkg{xtable}\r
163 \r
164 \r
165 <<ftable>>=\r
166 data(mtcars)\r
167 mtcars$cyl <- factor(mtcars$cyl, levels = c("4","6","8"),\r
168                      labels = c("four","six","eight"))\r
169 tbl <- ftable(mtcars$cyl, mtcars$vs, mtcars$am, mtcars$gear,\r
170               row.vars = c(2, 4),\r
171               dnn = c("Cylinders", "V/S", "Transmission", "Gears"))\r
172 tbl\r
173 @ %def\r
174 \r
175 Here is the \LaTeX{} produced:\r
176 \r
177 <<ftablecheck>>=\r
178 xftbl <- xtableFtable(tbl, method = "compact")\r
179 print.xtableFtable(xftbl, booktabs = TRUE)\r
180 @ %def\r
181 \r
182 And here is a basic flat table:\r
183 \r
184 <<ftable1, results = 'asis'>>=\r
185 xftbl <- xtableFtable(tbl)\r
186 print.xtableFtable(xftbl)\r
187 @ %def\r
188 \r
189 This illustrates the \code{method} argument:\r
190 \r
191 <<ftable2, results = 'asis'>>=\r
192 xftbl <- xtableFtable(tbl, method = "col.compact")\r
193 print.xtableFtable(xftbl, rotate.rownames = TRUE)\r
194 @ %def\r
195 \r
196 Booktabs is incompatible with vertical lines in tables, so the\r
197 vertical dividing line is removed.\r
198 \r
199 <<ftable3, results = 'asis'>>=\r
200 xftbl <- xtableFtable(tbl, method = "compact")\r
201 print.xtableFtable(xftbl, booktabs = TRUE)\r
202 @ %def\r
203 \p\r
204 \r
205 Row and column variable names can be formatted specially using\r
206 sanitization, and row and column variable names and labels can be\r
207 rotated.\r
208 \r
209 If special formatting is required for row and column labels, that can\r
210 be done as a workaround by redefining the data and associated labels.\r
211 \r
212 <<ftable4, results = 'asis'>>=\r
213 italic <- function(x){\r
214   paste0('{\\emph{', x, '}}')\r
215 }\r
216 mtcars$cyl <- factor(mtcars$cyl, levels = c("four","six","eight"),\r
217                      labels = c("four",italic("six"),"eight"))\r
218 large <- function(x){\r
219   paste0('{\\Large ', x, '}')\r
220 }\r
221 bold <- function(x){\r
222   paste0('{\\bfseries ', x, '}')\r
223 }\r
224 tbl <- ftable(mtcars$cyl, mtcars$vs, mtcars$am, mtcars$gear,\r
225               row.vars = c(2, 4),\r
226               dnn = c("Cylinders", "V/S", "Transmission", "Gears"))\r
227 xftbl <- xtableFtable(tbl, method = "row.compact")\r
228 print.xtableFtable(xftbl,\r
229                    sanitize.rownames.function = large,\r
230                    sanitize.colnames.function = bold,\r
231                    rotate.colnames = TRUE,\r
232                    rotate.rownames = TRUE)\r
233 @ %def\r
234 \r
235 \r
236 \r
237 \newpage\r
238 \r
239 <<include=FALSE>>=\r
240 # ## Demonstrate saving to file\r
241 # for(i in c("latex", "html")) {\r
242 #   outFileName <- paste("xtable.", ifelse(i=="latex", "tex", i), sep = "")\r
243 #   print(xtable(lm.D9), type = i, file = outFileName, append = TRUE,\r
244 #         latex.environments = NULL)\r
245 #   print(xtable(lm.D9), type = i, file = outFileName, append = TRUE,\r
246 #         latex.environments = "")\r
247 #   print(xtable(lm.D9), type = i, file = outFileName, append = TRUE,\r
248 #         latex.environments = "center")\r
249 #   print(xtable(anova(glm.D93, test = "Chisq")),\r
250 #         type = i, file = outFileName,\r
251 #         append = TRUE)\r
252 #   print(xtable(anova(glm.D93)), hline.after = c(1),\r
253 #         size = "small", type = i,\r
254 #         file = outFileName, append = TRUE)\r
255 #   # print(xtable(pr2), type = i, file = outFileName, append = TRUE)\r
256 # }\r
257 @\r
258 \r
259 \section{Automatic formatting}\r
260 \subsection{Suggest alignment, digits, and display}\r
261 The functions \code{xalign}, \code{xdigits}, and \code{xdisplay} are\r
262 useful for formatting tables in a sensible way. Consider the output\r
263 produced by the default formatting.\r
264 \r
265 <<results='asis'>>=\r
266 data(mtcars)\r
267 dat <- mtcars[1:3, 1:6]\r
268 x <- xtable(dat)\r
269 x\r
270 @\r
271 \r
272 \p\r
273 Now change the default alignment, digits and display using helper functions\r
274 \code{xalign}, \code{xdigits}, and \code{xdisplay}. This produces a better\r
275 format as shown below.\r
276 \r
277 <<results='asis'>>=\r
278 align(x) <- xalign(x)\r
279 digits(x) <- xdigits(x)\r
280 display(x) <- xdisplay(x)\r
281 x\r
282 @\r
283 \r
284 \subsection{Shorthand notation}\r
285 For convenience, the three `autoformat' functions (\code{xalign},\r
286 \code{xdigits}, and \code{xdisplay}) can be applied together when an\r
287 \code{xtable} is created, using the \code{auto} argument:\r
288 \r
289 <<results='asis'>>=\r
290 xtable(dat, auto = TRUE)\r
291 @\r
292 \r
293 \p\r
294 Similarly, the \code{autoformat} function can be used to postprocess an\r
295 existing \code{xtable}:\r
296 \r
297 <<results='asis'>>=\r
298 x <- xtable(dat)\r
299 autoformat(x)\r
300 @\r
301 \r
302 \newpage\r
303 \r
304 \subsection{Math-Style Exponents}\r
305 If you prefer $5 \times 10^5$ in your tables to 5e5, the\r
306 \code{math.style.exponents} option to \code{print.xtable} is useful:\r
307 \r
308 <<results='asis'>>=\r
309 print(xtable(data.frame(text = c("foo","bar"),\r
310                         googols = c(10e10,50e10),\r
311                         small = c(8e-24,7e-5),\r
312                         row.names = c("A","B")),\r
313              display = c("s","s","g","g")),\r
314       math.style.exponents = TRUE)\r
315 @\r
316 \r
317 this option also supports the values \code{ensuremath} which uses\r
318 \code{\char`\\ensuremath} instead of \code{\$\$} and \code{UTF-8}\r
319 which uses UTF-8 to approximate the \LaTeX typesetting.\r
320 \r
321 \r
322 \section{Sanitization}\r
323 <<results='asis'>>=\r
324 insane <- data.frame(Name = c("Ampersand","Greater than","Less than",\r
325                             "Underscore","Per cent","Dollar",\r
326                             "Backslash","Hash","Caret","Tilde",\r
327                             "Left brace","Right brace"),\r
328                      Character = I(c("&",">","<","_","%","$",\r
329                                      "\\","#","^","~","{","}")))\r
330 colnames(insane)[2] <- paste(insane[, 2], collapse = "")\r
331 xtable(insane)\r
332 @\r
333 \r
334 \p\r
335 Sometimes you might want to have your own sanitization function.\r
336 \r
337 \r
338 <<results='asis'>>=\r
339 wanttex <- xtable(data.frame(Column =\r
340                              paste("Value_is $10^{-",1:3,"}$", sep = "")))\r
341 print(wanttex, sanitize.text.function =\r
342       function(str) gsub("_", "\\_", str, fixed = TRUE))\r
343 @\r
344 \r
345 \p\r
346 Sanitization can be useful in formatting column headings and row names:\r
347 \r
348 <<sanitize3>>=\r
349 dat <- mtcars[1:3, 1:6]\r
350 large <- function(x){\r
351   paste0('{\\Large{\\bfseries ', x, '}}')\r
352 }\r
353 italic <- function(x){\r
354   paste0('{\\emph{ ', x, '}}')\r
355 }\r
356 @ %def\r
357 \r
358 <<sanitize4, results = 'asis'>>=\r
359 print(xtable(dat),\r
360       sanitize.rownames.function = italic,\r
361       sanitize.colnames.function = large,\r
362       booktabs = TRUE)\r
363 @ %def\r
364 \r
365 \r
366 \r
367 \newpage\r
368 \r
369 \subsection{Markup in tables}\r
370 Markup can be included in tables, including in column and row names,\r
371 by using a custom \code{sanitize.text.function}.\r
372 \r
373 <<results='asis'>>=\r
374 mat <- round(matrix(c(0.9, 0.89, 200, 0.045, 2.0), c(1, 5)), 4)\r
375 rownames(mat) <- "$y_{t-1}$"\r
376 colnames(mat) <- c("$R^2$", "$\\bar{x}$", "F-stat", "S.E.E", "DW")\r
377 mat <- xtable(mat)\r
378 print(mat, sanitize.text.function = function(x) {x})\r
379 @\r
380 \r
381 % By David Dahl to demonstrate contribution from David Whitting, 2007-10-09.\r
382 \p\r
383 You can also have sanitize functions that are specific to column or\r
384 row names.  In the table below, the row name is not sanitized but\r
385 column names and table elements are.\r
386 \r
387 <<results='asis'>>=\r
388 money <- matrix(c("$1,000","$900","$100"), ncol = 3,\r
389                 dimnames = list("$\\alpha$",\r
390                                 c("Income (US$)","Expenses (US$)",\r
391                                   "Profit (US$)")))\r
392 print(xtable(money), sanitize.rownames.function = function(x) {x})\r
393 @\r
394 \r
395 \section{Format examples}\r
396 \subsection{Adding a centering environment}\r
397 <<results='asis'>>=\r
398 print(xtable(anova(fm3), caption = "\\tt latex.environments = \"\""),\r
399       floating = TRUE, latex.environments = "")\r
400 print(xtable(anova(fm3), caption = "\\tt latex.environments = \"center\""),\r
401       floating = TRUE, latex.environments = "center")\r
402 @\r
403 \r
404 \newpage\r
405 \r
406 \subsection{Column alignment}\r
407 <<results='asis'>>=\r
408 tli.table <- xtable(tli[1:10, ])\r
409 align(tli.table) <- rep("r", 6)\r
410 tli.table\r
411 @\r
412 \r
413 \subsubsection{Left aligned strings with column lines}\r
414 <<results='asis'>>=\r
415 align(tli.table) <- "|rrl|l|lr|"\r
416 tli.table\r
417 @\r
418 \r
419 \subsubsection{Fixed width columns}\r
420 <<results='asis'>>=\r
421 align(tli.table) <- "|rr|lp{3cm}l|r|"\r
422 tli.table\r
423 @\r
424 \r
425 \newpage\r
426 \r
427 \subsection{Number of digits}\r
428 One number for all columns,\r
429 <<results='asis'>>=\r
430 display(tli.table)[c(2,6)] <- "f"\r
431 digits(tli.table) <- 3\r
432 tli.table\r
433 @\r
434 \r
435 \p\r
436 or one for each column, including the row names,\r
437 <<results='asis'>>=\r
438 digits(tli.table) <- 1:(ncol(tli)+1)\r
439 tli.table\r
440 @\r
441 \r
442 \p\r
443 or as a full matrix.\r
444 <<results='asis'>>=\r
445 digits(tli.table) <- matrix(0:4, nrow = 10, ncol = ncol(tli)+1)\r
446 tli.table\r
447 @\r
448 \r
449 \newpage\r
450 \r
451 \subsection{Suppress row/column names}\r
452 \subsubsection{Suppress row names}\r
453 <<results='asis'>>=\r
454 tli.table <- xtable(tli[1:10, ])\r
455 print(tli.table, include.rownames = FALSE)\r
456 @\r
457 \r
458 \p\r
459 If you want a vertical line on the left, you need to change the \code{align}\r
460 attribute.\r
461 <<results='asis'>>=\r
462 align(tli.table) <- "|r|r|lp{3cm}l|r|"\r
463 print(tli.table, include.rownames = FALSE)\r
464 @\r
465 \r
466 \p\r
467 Revert the alignment to what is was before.\r
468 <<>>=\r
469 align(tli.table) <- "|rr|lp{3cm}l|r|"\r
470 @\r
471 \r
472 \newpage\r
473 \r
474 \subsubsection{Suppress column names}\r
475 <<results='asis'>>=\r
476 print(tli.table, include.colnames = FALSE)\r
477 @\r
478 \r
479 \p\r
480 Note the doubled header lines which can be suppressed.\r
481 <<results='asis'>>=\r
482 print(tli.table, include.colnames = FALSE,\r
483       hline.after = c(0,nrow(tli.table)))\r
484 @\r
485 \r
486 \subsubsection{Suppress row and column names}\r
487 <<results='asis'>>=\r
488 print(tli.table, include.colnames = FALSE, include.rownames = FALSE)\r
489 @\r
490 \r
491 \newpage\r
492 \r
493 \subsection{Rotate row/column names}\r
494 The \code{rotate.rownames} and \code{rotate.colnames} arguments can be\r
495 used to rotate the row and/or column names. This requires\r
496 \verb|\usepackage{rotating}| in the \LaTeX\ preamble.\r
497 \r
498 <<results='asis'>>=\r
499 print(tli.table, rotate.rownames = TRUE, rotate.colnames = TRUE)\r
500 @\r
501 \r
502 \newpage\r
503 \r
504 \subsection{Horizontal lines}\r
505 \subsubsection{Line locations}\r
506 Use the \code{hline.after} argument to specify the position of the\r
507 horizontal lines.\r
508 \r
509 <<results='asis'>>=\r
510 print(xtable(anova(fm3)), hline.after = c(1))\r
511 @\r
512 \r
513 \subsubsection{Line styles}\r
514 Specifying \code{booktabs = TRUE} will generate three line types. By\r
515 default, when no value is given for \code{hline.after}, a\r
516 \verb|\toprule| will be drawn above the table, a \verb|\midrule| after\r
517 the table headings and a \verb|\bottomrule| below the table. This\r
518 requires \verb|\usepackage{booktabs}| in the \LaTeX\ preamble.\r
519 \r
520 \p\r
521 \r
522 The top and bottom rules are slightly thicker than the mid rule. The\r
523 thickness of the lines can be set via the \LaTeX\ lengths\r
524 \verb|\heavyrulewidth| and \verb|\lightrulewidth|.\r
525 \r
526 <<results='asis'>>=\r
527 tli.table <- xtable(tli[1:10, ])\r
528 print(tli.table, include.rownames = FALSE, booktabs = TRUE)\r
529 @\r
530 \r
531 \p\r
532 \r
533 If \code{hline.after} includes \code{-1}, a \verb|\toprule| will be\r
534 drawn above the table. If \code{hline.after} includes the number of\r
535 rows in the table, a \verb|\bottomrule| will be drawn below the\r
536 table. For any other values specified in \code{hline.after}, a\r
537 \verb|\midrule| will be drawn after that line of the table.\r
538 \r
539 \p\r
540 The following table has more than one \verb|\midrule|.\r
541 \r
542 <<results='asis'>>=\r
543 bktbs <- xtable(matrix(1:10, ncol = 2))\r
544 hlines <- c(-1, 0, 1, nrow(bktbs))\r
545 print(bktbs, booktabs = TRUE, hline.after = hlines)\r
546 @\r
547 \r
548 \subsection{Table level commands}\r
549 <<results='asis'>>=\r
550 print(xtable(anova(fm3)), size = "large")\r
551 @\r
552 \r
553 \p\r
554 <<results='asis'>>=\r
555 print(xtable(anova(fm3)), size = "\\setlength{\\tabcolsep}{12pt}")\r
556 @\r
557 \r
558 \subsection{Long tables}\r
559 Requires \verb|\usepackage{longtable}| in the \LaTeX\ preamble.\r
560 \r
561 <<results='asis'>>=\r
562 x <- matrix(rnorm(1000), ncol = 10)\r
563 x.big <- xtable(x, caption = "A \\code{longtable} spanning several pages")\r
564 print(x.big, hline.after=c(-1, 0), tabular.environment = "longtable")\r
565 @\r
566 \r
567 Extra features of the \pkg{longtable} \LaTeX{} package can typically\r
568 be activated using \code{add.to.row}, as shown below.\r
569 \r
570 <<results='asis'>>=\r
571 add.to.row <- list(pos = list(0), command = NULL)\r
572 command <- paste0("\\hline\n\\endhead\n",\r
573                   "\\hline\n",\r
574                   "\\multicolumn{", dim(x)[2] + 1, "}{l}",\r
575                   "{\\footnotesize Continued on next page}\n",\r
576                   "\\endfoot\n",\r
577                   "\\endlastfoot\n")\r
578 add.to.row$command <- command\r
579 print(x.big, hline.after=c(-1), add.to.row = add.to.row,\r
580       tabular.environment = "longtable")\r
581 @\r
582 \r
583 \r
584 \newpage\r
585 \r
586 \subsection{Use of \code{add.to.row} argument}\r
587 The following frequency table has outer dimnames: \code{Grade3} and\r
588 \code{Grade6}.\r
589 \r
590 <<>>=\r
591 Grade3 <- c("A","B","B","A","B","C","C","D","A","B",\r
592             "C","C","C","D","B","B","D","C","C","D")\r
593 Grade6 <- c("A","A","A","B","B","B","B","B","C","C",\r
594             "A","C","C","C","D","D","D","D","D","D")\r
595 Cohort <- table(Grade3, Grade6)\r
596 Cohort\r
597 @\r
598 \r
599 \p\r
600 The default behavior of \code{print.xtable} is to strip outer dimnames.\r
601 <<results='asis'>>=\r
602 xtable(Cohort)\r
603 @\r
604 \r
605 \p\r
606 The desired column labels can be created using \code{add.to.row}, in this case\r
607 applying two commands to ``row number zero'' while suppressing the basic column\r
608 names.\r
609 \r
610 <<results='asis'>>=\r
611 addtorow <- list()\r
612 addtorow$pos <- list(0, 0)\r
613 addtorow$command <- c("& \\multicolumn{4}{c}{Grade 6} \\\\\n",\r
614                       "Grade 3 & A & B & C & D \\\\\n")\r
615 print(xtable(Cohort), add.to.row = addtorow, include.colnames = FALSE)\r
616 @\r
617 \r
618 \subsection{Sideways tables}\r
619 Requires \verb|\usepackage{rotating}| in the LaTeX\r
620 preamble.  Sideways tables can't be forced in place with the \code{[H]}\r
621 specifier, but you can use the \verb|\clearpage| command to get them\r
622 fairly nearby.\r
623 \r
624 <<results='asis'>>=\r
625 x <- x[1:30, ]\r
626 x.side <- xtable(x, caption = "A sideways table")\r
627 print(x.side, floating = TRUE, floating.environment = "sidewaystable")\r
628 @\r
629 \clearpage\r
630 \r
631 \subsection{Rescaled tables}\r
632 Specify a \code{scalebox} value to rescale the table.\r
633 <<results='asis'>>=\r
634 x <- x[1:20, ]\r
635 x.rescale <- xtable(x)\r
636 print(x.rescale, scalebox = 0.7)\r
637 @\r
638 \r
639 \subsection{Aligning fixed width columns}\r
640 Note that using specifications such as \verb|p{2cm}| always\r
641 produces a \textbf{left aligned} column. What if some other alignment\r
642 is desired?\r
643 \r
644 This is not really a problem with \pkg{xtable} but with the formatting\r
645 of tables with fixed width columns and different alignments using\r
646 standard \LaTeX.\r
647 \r
648 One solution is to use the \verb|array| package, defining new\r
649 column formats.\r
650 \r
651 \begin{verbatim}\r
652 \newcolumntype{L}[1]{>{\raggedright\let\newline\\\r
653     \arraybackslash\hspace{0pt}}m{#1}}\r
654 \newcolumntype{C}[1]{>{\centering\let\newline\\\r
655     \arraybackslash\hspace{0pt}}m{#1}}\r
656 \newcolumntype{R}[1]{>{\raggedleft\let\newline\\\r
657     \arraybackslash\hspace{0pt}}m{#1}}\r
658 \newcolumntype{P}[1]{>{\raggedright\tabularxbackslash}p{#1}}\r
659 \end{verbatim}\r
660 \r
661 These allow for very sophisticated cell formatting, namely\r
662 left-aligned, centred, or right-aligned text, with recognition of line\r
663 breaks for the first three new column types. If these lines are\r
664 included along with \verb|\usepackage{array}|, then the following is\r
665 possible.\r
666 \r
667 \newcolumntype{L}[1]{>{\raggedright\let\newline\\\r
668     \arraybackslash\hspace{0pt}}m{#1}}\r
669 \newcolumntype{C}[1]{>{\centering\let\newline\\\r
670     \arraybackslash\hspace{0pt}}m{#1}}\r
671 \newcolumntype{R}[1]{>{\raggedleft\let\newline\\\r
672     \arraybackslash\hspace{0pt}}m{#1}}\r
673 \newcolumntype{P}[1]{>{\raggedright\tabularxbackslash}p{#1}}\r
674 \r
675 <<results='asis'>>=\r
676 df <- data.frame(name = c("A","B"), right = c(1.4, 34.6),\r
677                  left = c(1.4, 34.6), text = c("txt1","txt2"))\r
678 print(xtable(df, align = c("l", "|c", "|R{3cm}", "|L{3cm}", "| p{3cm}|")),\r
679       floating = FALSE, include.rownames = FALSE)\r
680 @\r
681 \r
682 \newpage\r
683 \r
684 \subsection{Table width}\r
685 The \code{tabularx} environment is for typesetting tables whose\r
686 overall width is fixed. The column alignment code \code{X} denotes\r
687 columns that will be stretched to achieve the desired table\r
688 width. Requires \verb|\usepackage{tabularx}| in the \LaTeX\ preamble.\r
689 \r
690 <<results='asis'>>=\r
691 df.width <- data.frame(One = c("item 1", "A"), Two = c("item 2", "B"),\r
692                        Three = c("item 3", "C"), Four = c("item 4", "D"))\r
693 x.width <- xtable(df.width)\r
694 align(x.width) <- "|l|X|l|l|l|"\r
695 print(x.width, tabular.environment = "tabularx", width = "\\textwidth")\r
696 @\r
697 \r
698 \section{Suppressing printing}\r
699 By default the \code{print} method will print the \LaTeX\ or HTML to\r
700 standard output and also return the character strings invisibly.  The\r
701 printing to standard output can be suppressed by specifying\r
702 \code{print.results = FALSE}.\r
703 \r
704 <<>>=\r
705 x.out <- print(tli.table, print.results = FALSE)\r
706 @\r
707 \r
708 Formatted output can also be captured without printing with the\r
709 \code{toLatex} method.  This function returns an object of class\r
710 \code{"Latex"}.\r
711 \r
712 <<>>=\r
713 x.ltx <- toLatex(tli.table)\r
714 class(x.ltx)\r
715 x.ltx\r
716 @\r
717 \r
718 \r
719 \newpage\r
720 \r
721 \section{Acknowledgements}\r
722 Most of the examples in this gallery are taken from the \pkg{xtable}\r
723 documentation. Two examples (\code{add.to.row} and `Aligning fixed width\r
724 columns') are from Stack Exchange.\r
725 \r
726 \section{Session information}\r
727 <<results='asis'>>=\r
728 toLatex(sessionInfo())\r
729 @\r
730 \r
731 \end{document}\r