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