]> git.donarmstrong.com Git - xtable.git/blob - pkg/vignettes/xtableGallery.Rnw
Set options(xtable.floating = FALSE) instead of repeating 37 times
[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 %**************************************************************************\r
9 %\r
10 % # $Id:$\r
11 \r
12 % $Revision:  $\r
13 % $Author: $\r
14 % $Date:  $\r
15 \r
16 \r
17 \documentclass[article, nojss]{jss}\r
18 \usepackage{amsthm}\r
19 \usepackage{amsmath}\r
20 \usepackage{graphicx}\r
21 \usepackage{color}\r
22 \usepackage{afterpage}\r
23 \usepackage{hyperref}\r
24 \r
25 \title{\r
26 The \pkg{xtable} Gallery\r
27 }\r
28 \author{Jonathan Swinton and others\\\r
29   <jonathan@swintons.net>\r
30 }\r
31 \r
32 \Plainauthor{Jonathan Swinton, and others} %% comma-separated\r
33 \r
34 \Plaintitle{The xtable Gallery} %% without formatting\r
35 \r
36 \Shorttitle{\pkg{xtable} Gallery} %% a short title (if necessary)\r
37 \r
38 <<include=FALSE>>=\r
39 library(knitr)\r
40 opts_chunk$set(\r
41 fig.path='figdir/fig', debug=TRUE, echo=TRUE\r
42 )\r
43 options(replace.assign = TRUE, width = 60,\r
44         tidy.opts = list(width.cutoff = 60))\r
45 @\r
46 \r
47 \usepackage{rotating}\r
48 \usepackage{longtable}\r
49 \usepackage{booktabs}\r
50 \usepackage{tabularx}\r
51 %\usepackage{hyperref}\r
52 \r
53 \Abstract{\r
54 \r
55   This document gives a gallery of tables which can be made by using\r
56   the \texttt{xtable} package to create \LaTeX\ output.  It doubles as\r
57   a regression check for the package.\r
58 \r
59 }\r
60 \r
61 \Keywords{Reproducible research, \LaTeX}\r
62 \r
63 \Address{\r
64   Jonathan Swinton\\\r
65   E-mail: \email{jonathan@swintons.net}\r
66 }\r
67 \r
68 \begin{document}\r
69 \r
70 \r
71 \r
72 \section{Gallery}\r
73 \subsection{Data frame}\r
74 Load example dataset\r
75 <<>>=\r
76 library(xtable)\r
77 options(xtable.floating = FALSE)\r
78 data(tli)\r
79 \r
80 ## Demonstrate data.frame\r
81 tli.table <- xtable(tli[1:10, ])\r
82 digits(tli.table)[c(2,6)] <- 0\r
83 @\r
84 <<results='asis'>>=\r
85 tli.table\r
86 @\r
87 \r
88 \newpage\r
89 \r
90 \subsection{Matrix}\r
91 <<>>=\r
92 design.matrix <- model.matrix(~ sex*grade, data = tli[1:10, ])\r
93 design.table <- xtable(design.matrix)\r
94 @\r
95 <<results='asis'>>=\r
96 design.table\r
97 @\r
98 \r
99 \subsection{aov}\r
100 <<>>=\r
101 fm1 <- aov(tlimth ~ sex + ethnicty + grade + disadvg, data = tli)\r
102 fm1.table <- xtable(fm1)\r
103 @\r
104 <<results='asis'>>=\r
105 fm1.table\r
106 @\r
107 \subsection{lm}\r
108 <<>>=\r
109 fm2 <- lm(tlimth ~ sex*ethnicty, data = tli)\r
110 fm2.table <- xtable(fm2)\r
111 @\r
112 <<results='asis'>>=\r
113 fm2.table\r
114 @\r
115 \r
116 \vspace{12pt}\textbf{\itshape An anova object}\r
117 \r
118 <<results='asis'>>=\r
119 xtable(anova(fm2))\r
120 @\r
121 \vspace{12pt}\textbf{\itshape Another anova object}\r
122 <<>>=\r
123 fm2b <- lm(tlimth ~ ethnicty, data = tli)\r
124 @\r
125 <<results='asis'>>=\r
126 xtable(anova(fm2b, fm2))\r
127 @\r
128 \r
129 \r
130 \subsection{glm}\r
131 \r
132 <<>>=\r
133 \r
134 ## Demonstrate glm\r
135 fm3 <- glm(disadvg ~ ethnicty*grade, data = tli, family = binomial())\r
136 fm3.table <- xtable(fm3)\r
137 @\r
138 <<results='asis'>>=\r
139 fm3.table\r
140 @\r
141 \r
142 \vspace{12pt}\textbf{\itshape An anova object}\r
143 <<results='asis'>>=\r
144 xtable(anova(fm3))\r
145 @\r
146 \r
147 \r
148 \subsection{More aov}\r
149 <<>>=\r
150 \r
151 ## Demonstrate aov\r
152 ## Taken from help(aov) in R 1.1.1\r
153 ## From Venables and Ripley (1997) p.210.\r
154 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)\r
155 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)\r
156 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)\r
157 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,\r
158            62.0,48.8,45.5,44.2,52.0,51.5,49.8,48.8,57.2,59.0,53.2,56.0)\r
159 npk <- data.frame(block = gl(6,4), N = factor(N), P = factor(P),\r
160                   K = factor(K), yield = yield)\r
161 npk.aov <- aov(yield ~ block + N*P*K, npk)\r
162 op <- options(contrasts = c("contr.helmert", "contr.treatment"))\r
163 npk.aovE <- aov(yield ~  N*P*K + Error(block), npk)\r
164 options(op)\r
165 @\r
166 <<results='asis'>>=\r
167 xtable(npk.aov)\r
168 @\r
169 \r
170 \vspace{12pt}\textbf{\itshape An anova object}\r
171 <<results='asis'>>=\r
172 xtable(anova(npk.aov))\r
173 @\r
174 \r
175 \vspace{12pt}\textbf{\itshape Another anova object}\r
176 <<results='asis'>>=\r
177 xtable(summary(npk.aov))\r
178 @\r
179 \r
180 <<results='asis'>>=\r
181 xtable(npk.aovE)\r
182 @\r
183 \r
184 \r
185 <<results='asis'>>=\r
186 xtable(summary(npk.aovE))\r
187 @\r
188 \r
189 \subsection{More lm}\r
190 <<>>=\r
191 \r
192 ## Demonstrate lm\r
193 ## Taken from help(lm) in R 1.1.1\r
194 ## Annette Dobson (1990) "An Introduction to Generalized Linear Models".\r
195 ## Page 9: Plant Weight Data.\r
196 ctl <- c(4.17,5.58,5.18,6.11,4.50,4.61,5.17,4.53,5.33,5.14)\r
197 trt <- c(4.81,4.17,4.41,3.59,5.87,3.83,6.03,4.89,4.32,4.69)\r
198 group <- gl(2,10,20, labels = c("Ctl","Trt"))\r
199 weight <- c(ctl, trt)\r
200 lm.D9 <- lm(weight ~ group)\r
201 @\r
202 <<results='asis'>>=\r
203 xtable(lm.D9)\r
204 @\r
205 \r
206 \r
207 <<results='asis'>>=\r
208 xtable(anova(lm.D9))\r
209 @\r
210 \r
211 \subsection{More glm}\r
212 <<>>=\r
213 \r
214 ## Demonstrate glm\r
215 ## Taken from help(glm) in R 1.1.1\r
216 ## Annette Dobson (1990) "An Introduction to Generalized Linear Models".\r
217 ## Page 93: Randomized Controlled Trial :\r
218 counts <- c(18,17,15,20,10,20,25,13,12)\r
219 outcome <- gl(3,1,9)\r
220 treatment <- gl(3,3)\r
221 d.AD <- data.frame(treatment, outcome, counts)\r
222 glm.D93 <- glm(counts ~ outcome + treatment, family = poisson())\r
223 @\r
224 <<results='asis'>>=\r
225 xtable(glm.D93, align = "r|llrc")\r
226 @\r
227 \r
228 \subsection{prcomp}\r
229 <<prcomp>>=\r
230 if(require(stats, quietly = TRUE)) {\r
231   ## Demonstrate prcomp\r
232   ## Taken from help(prcomp) in mva package of R 1.1.1\r
233   data(USArrests)\r
234   pr1 <- prcomp(USArrests)\r
235 }\r
236 @\r
237 <<results='asis'>>=\r
238 xtable(pr1)\r
239 @\r
240 \r
241 \r
242 <<results='asis'>>=\r
243 xtable(summary(pr1))\r
244 @\r
245 \r
246 \r
247 \r
248 <<echo = FALSE, results = 'Hide'>>=\r
249 #  ## Demonstrate princomp\r
250 #  ## Taken from help(princomp) in mva package of R 1.1.1\r
251 #  pr2 <- princomp(USArrests)\r
252 #  xtable(pr2)\r
253 @\r
254 \r
255 \subsection{Time series}\r
256 \r
257 <<>>=\r
258 temp.ts <- ts(cumsum(1 + round(rnorm(100), 0)),\r
259               start = c(1954, 7), frequency = 12)\r
260 temp.table <- xtable(temp.ts, digits = 0)\r
261 caption(temp.table) <- "Time series example"\r
262 @\r
263 <<results='asis'>>=\r
264 temp.table\r
265 @\r
266 <<savetofile,echo=FALSE>>=\r
267 if (FALSE) {\r
268     for(i in c("latex", "html")) {\r
269         outFileName <- paste("xtable.", ifelse(i=="latex", "tex", i), sep = "")\r
270         print(xtable(lm.D9), type = i, file = outFileName, append = TRUE,\r
271               latex.environments = NULL)\r
272         print(xtable(lm.D9), type = i, file = outFileName, append = TRUE,\r
273               latex.environments = "")\r
274         print(xtable(lm.D9), type = i, file = outFileName, append = TRUE,\r
275               latex.environments = "center")\r
276         print(xtable(anova(glm.D93, test = "Chisq")),\r
277               type = i, file = outFileName,\r
278               append = TRUE)\r
279         print(xtable(anova(glm.D93)), hline.after = c(1),\r
280               size = "small", type = i,\r
281               file = outFileName, append = TRUE)\r
282       # print(xtable(pr2), type = i, file = outFileName, append = TRUE)\r
283     }\r
284 }\r
285 @\r
286 \r
287 \section{Helper functions for formatting}\r
288 \label{sec:helperfns}\r
289 The functions \code{xalign}, \code{xdigits}, and \code{xdisplay} are\r
290 useful for formatting tables in a sensible way.\r
291 \r
292 <<preliminary>>=\r
293 dat <- mtcars[1:3, 1:6]\r
294 @ %def\r
295 \r
296 Consider the output produced by the default formatting.\r
297 \r
298 <<current>>=\r
299 x <- xtable(dat)\r
300 x\r
301 @ %def\r
302 \r
303 In a \LaTeX\ document this appears as follows.\r
304 \r
305 <<currentresult, echo = FALSE, results = 'asis'>>=\r
306 x\r
307 @ %def\r
308 \r
309 Now change the default alignment, digits and display using helper functions\r
310 \texttt{xalign}, \texttt{xdigits}, and \texttt{xdisplay}.\r
311 \r
312 \r
313 <<proposed>>=\r
314 align(x) <- xalign(x)\r
315 digits(x) <- xdigits(x)\r
316 display(x) <- xdisplay(x)\r
317 x\r
318 @ %def\r
319 \r
320 This produces a better format as shown below.\r
321 \r
322 <<proposedresult, echo = FALSE, results = 'asis'>>=\r
323 x\r
324 @ %def\r
325 \r
326 \r
327 \r
328 \section{Sanitization}\r
329 <<>>=\r
330 insane <- data.frame(Name = c("Ampersand","Greater than","Less than",\r
331                             "Underscore","Per cent","Dollar",\r
332                             "Backslash","Hash","Caret","Tilde",\r
333                             "Left brace","Right brace"),\r
334                      Character = I(c("&",">","<","_","%","$",\r
335                                      "\\","#","^","~","{","}")))\r
336 colnames(insane)[2] <- paste(insane[, 2], collapse = "")\r
337 @\r
338 \r
339 <<pxti,results='asis'>>=\r
340 xtable(insane)\r
341 @\r
342 \r
343 \vspace{12pt}\r
344 Sometimes you might want to have your own sanitization function\r
345 <<>>=\r
346 wanttex <- xtable(data.frame(label =\r
347                              paste("Value_is $10^{-",1:3,"}$", sep = "")))\r
348 @\r
349 <<results='asis'>>=\r
350 print(wanttex,\r
351       sanitize.text.function =\r
352       function(str)gsub("_", "\\_", str, fixed = TRUE))\r
353 @\r
354 \r
355 \subsection{Markup in tables}\r
356 \r
357 Markup can be included in tables, including in column and row names, by using\r
358 a custom \code{sanitize.text.function()}:\r
359 \r
360 <<>>=\r
361 mat <- round(matrix(c(0.9, 0.89, 200, 0.045, 2.0), c(1, 5)), 4)\r
362 rownames(mat) <- "$y_{t-1}$"\r
363 colnames(mat) <- c("$R^2$", "$\\bar{R}^2$", "F-stat", "S.E.E", "DW")\r
364 mat <- xtable(mat)\r
365 @\r
366 <<results='asis'>>=\r
367 print(mat, sanitize.text.function = function(x){x})\r
368 @\r
369 \r
370 % By David Dahl to demonstrate contribution from David Whitting, 2007-10-09.\r
371 You can also have sanitize functions that are specific to column or\r
372 row names.  In the table below, the row name is not sanitized but\r
373 column names and table elements are:\r
374 <<>>=\r
375 money <- matrix(c("$1,000","$900","$100"), ncol = 3,\r
376                 dimnames = list("$\\alpha$",\r
377                               c("Income (US$)","Expenses (US$)",\r
378                                 "Profit (US$)")))\r
379 @\r
380 <<results='asis'>>=\r
381 print(xtable(money), sanitize.rownames.function = function(x) {x})\r
382 @\r
383 \r
384 \section{Format examples}\r
385 \subsection{Adding a centering environment }\r
386 <<results='asis'>>=\r
387    print(xtable(lm.D9, caption = "\\tt latex.environments = NULL"),\r
388          latex.environments = NULL)\r
389     print(xtable(lm.D9, caption = "\\tt latex.environments = \"\""),\r
390           latex.environments = "")\r
391     print(xtable(lm.D9, caption = "\\tt latex.environments = \"center\""),\r
392           latex.environments = "center")\r
393 @\r
394 \subsection{Column alignment}\r
395 \r
396 <<>>=\r
397 tli.table <- xtable(tli[1:10, ])\r
398 @\r
399 <<>>=\r
400 align(tli.table) <- rep("r", 6)\r
401 @\r
402 <<results='asis'>>=\r
403 tli.table\r
404 @\r
405 \r
406 \vspace{12pt}\r
407 \textbf{\itshape Single string and column lines}\r
408 <<>>=\r
409 align(tli.table) <- "|rrl|l|lr|"\r
410 @\r
411 <<results='asis'>>=\r
412 tli.table\r
413 @\r
414 \vspace{12pt}\textbf{\itshape Fixed width columns}\r
415 <<>>=\r
416 align(tli.table) <- "|rr|lp{3cm}l|r|"\r
417 @\r
418 <<results='asis'>>=\r
419 tli.table\r
420 @\r
421 \r
422 \subsection{Significant digits}\r
423 \r
424 \r
425 Specify with a single argument\r
426 <<>>=\r
427 digits(tli.table) <- 3\r
428 @\r
429 <<results='asis'>>=\r
430 tli.table\r
431 @\r
432 \r
433 \r
434 \vspace{12pt}\r
435 or one for each column, counting the row names,\r
436 <<>>=\r
437 digits(tli.table) <- 1:(ncol(tli)+1)\r
438 @\r
439 <<results='asis'>>=\r
440 tli.table\r
441 @\r
442 \r
443 \vspace{12pt}\r
444 or as a full matrix\r
445 <<>>=\r
446 digits(tli.table) <- matrix( 0:4, nrow = 10, ncol = ncol(tli)+1 )\r
447 @\r
448 <<results='asis'>>=\r
449 tli.table\r
450 @\r
451 \r
452 \subsection{Suppress row names}\r
453 <<results='asis'>>=\r
454 print(tli.table, include.rownames = FALSE)\r
455 @\r
456 \r
457 \vspace{12pt} If you want a vertical line on the left, you need to\r
458 change the \code{align} attribute.\r
459 <<>>=\r
460 align(tli.table) <- "|r|r|lp{3cm}l|r|"\r
461 @\r
462 <<results='asis'>>=\r
463 print(tli.table, include.rownames = FALSE)\r
464 @\r
465 \r
466 \vspace{12pt} Revert the alignment to what is was before.\r
467 <<>>=\r
468 align(tli.table) <- "|rr|lp{3cm}l|r|"\r
469 @\r
470 \r
471 \subsection{Suppress column names}\r
472 <<results='asis'>>=\r
473 print(tli.table, include.colnames = FALSE)\r
474 @\r
475 \r
476 \vspace{12pt}\r
477 Note the doubled header lines which can be suppressed with, eg,\r
478 <<results='asis'>>=\r
479 print(tli.table, include.colnames = FALSE,\r
480       hline.after = c(0,nrow(tli.table)))\r
481 @\r
482 \r
483 \subsection{Suppress row and column names}\r
484 <<results='asis'>>=\r
485 print(tli.table, include.colnames = FALSE, include.rownames = FALSE)\r
486 @\r
487 \r
488 \subsection{Rotate row and column names}\r
489 The \texttt{rotate.rownames } and \texttt{rotate.colnames} arguments can be\r
490 used to rotate the row and/or column names.\r
491 \r
492 <<results='asis'>>=\r
493 print(tli.table, rotate.rownames = TRUE, rotate.colnames = TRUE)\r
494 @\r
495 \r
496 \subsection{Horizontal lines}\r
497 \r
498 \vspace{12pt}\textbf{\itshape Line locations}\r
499 \r
500 Use the \texttt{hline.after} argument to specify the position of the\r
501 horizontal lines.\r
502 \r
503 <<results='asis'>>=\r
504 print(xtable(anova(glm.D93)), hline.after = c(1))\r
505 @\r
506 \r
507 \vspace{12pt}\textbf{\itshape Line styles}\r
508 \r
509 The \LaTeX package \pkg{ booktabs} can be used to specify different\r
510 line style tags for top, middle, and bottom lines.  Specifying\r
511 \code{ booktabs = TRUE} will lead to separate tags being generated\r
512 for the three line types.\r
513 \r
514 Insert \verb|\usepackage{booktabs}| in your \LaTeX preamble and define\r
515 the \texttt{toprule}, \texttt{midrule}, and \texttt{bottomrule}\r
516 tags to specify the line styles. By default, when no value is given\r
517 for \texttt{hline.after}, a \texttt{toprule} will be drawn above the\r
518 table, a \texttt{midrule} after the table headings and a\r
519 \texttt{bottomrule} below the table. The width of the top and bottom\r
520 rules can be set by supplying a value to \verb+\heavyrulewidth+. The\r
521 width of the midrules can be set by supplying a value to\r
522 \verb+\lightrulewidth+. The following tables have\r
523 \verb+\heavyrulewidth = 2pt+ and \verb+\lightrulewidth = 0.5pt+, to\r
524 ensure the difference in weight is noticeable.\r
525 \r
526 There is no support for \verb+\cmidrule+ or \verb+\specialrule+\r
527 although they are part of the \texttt{booktabs} package.\r
528 \r
529 \heavyrulewidth = 2pt\r
530 \lightrulewidth = 0.5pt\r
531 \r
532 <<results='asis'>>=\r
533 print(tli.table, booktabs = TRUE)\r
534 @\r
535 \r
536 \vspace{12pt}\r
537 If \texttt{hline.after} includes $-1$, a \texttt{toprule} will be\r
538 drawn above the table. If \texttt{hline.after} includes the number of\r
539 rows in the table, a \texttt{bottomrule} will be drawn below the\r
540 table. For any other values specified in \texttt{hline.after}, a\r
541 \texttt{midrule} will be drawn after that line of the table.\r
542 \r
543 The next table has more than one \texttt{midrule}.\r
544 \r
545 <<>>=\r
546 bktbs <- xtable(matrix(1:10, ncol = 2))\r
547 hlines <- c(-1,0,1,nrow(bktbs))\r
548 @\r
549 This command produces the required table.\r
550 <<results='asis'>>=\r
551 print(bktbs, booktabs = TRUE, hline.after = hlines)\r
552 @\r
553 \r
554 \r
555 \subsection{Table-level LaTeX}\r
556 <<results='asis'>>=\r
557 print(xtable(anova(glm.D93)), size = "small")\r
558 @\r
559 \r
560 \r
561 \subsection{Long tables}\r
562 Remember to insert \verb|\usepackage{longtable}| in your \LaTeX preamble.\r
563 \r
564 <<longtable>>=\r
565 \r
566 ## Demonstration of longtable support.\r
567 x <- matrix(rnorm(1000), ncol = 10)\r
568 x.big <- xtable(x, label = 'tabbig',\r
569                 caption = 'Example of \\code{longtable} spanning several pages')\r
570 @\r
571 <<results='asis'>>=\r
572 print(x.big, tabular.environment = 'longtable')\r
573 @\r
574 \r
575 %%\r
576 %% The column name alignment is off in the following example.\r
577 %% It needs some revision before exposing it. - CR, 7/2/2012\r
578 %%\r
579 %\r
580 %\vspace{12pt}\textbf{\itshape Long tables with the header on each page}\r
581 %\r
582 %The \texttt{add.to.row} argument can be used to display the header\r
583 %for a long table on each page, and to add a "continued" footer\r
584 %on all pages except the last page.\r
585 %\r
586 %<<results=tex>>=\r
587 %library(xtable)\r
588 %x<-matrix(rnorm(1000), ncol = 10)\r
589 %addtorow<-list()\r
590 %addtorow$pos<-list()\r
591 %addtorow$pos[[1]]<-c(0)\r
592 %addtorow$command<-c(paste(\r
593 %    "\\hline \n",\r
594 %    "  \\endhead \n",\r
595 %    "  \\hline \n",\r
596 %    "  {\\footnotesize Continued on next page} \n",\r
597 %    "  \\endfoot \n",\r
598 %    "  \\endlastfoot \n", sep = ""))\r
599 %x.big2 <- xtable(x, label = "tabbig2",\r
600 %    caption = "Example of longtable with the header on each page")\r
601 %print(x.big2, tabular.environment = "longtable",\r
602 %include.rownames = FALSE, add.to.row = addtorow, hline.after = c(-1) )\r
603 %@\r
604 \r
605 \subsection{Sideways tables}\r
606 Remember to insert \verb|\usepackage{rotating}| in your LaTeX\r
607 preamble.  Sideways tables can't be forced in place with the `H'\r
608 specifier, but you can use the \verb|\clearpage| command to get them\r
609 fairly nearby.\r
610 \r
611 <<>>=\r
612 x <- x[1:30, ]\r
613 x.small <- xtable(x, label = 'tabsmall', caption = 'A sideways table')\r
614 @\r
615 \r
616 <<results='asis'>>=\r
617 print(x.small, floating = TRUE, floating.environment = 'sidewaystable')\r
618 @\r
619 \clearpage\r
620 \r
621 \subsection{Rescaled tables}\r
622 Specify a \texttt{scalebox} value to rescale the table.\r
623 \r
624 <<>>=\r
625 x <- x[1:20, ]\r
626 x.rescale <- xtable(x, label = 'tabrescaled', caption = 'A rescaled table')\r
627 @\r
628 \r
629 <<results='asis'>>=\r
630 print(x.rescale, scalebox = 0.7)\r
631 @\r
632 \r
633 \subsection{Table Width}\r
634 The \texttt{tabularx} tabular environment provides more alignment options,\r
635 and has a \texttt{width} argument to specify the table width.\r
636 \r
637 Remember to insert \verb|\usepackage{tabularx}| in your \LaTeX\ preamble.\r
638 \r
639 <<>>=\r
640 df.width <- data.frame(\r
641     "label 1 with much more text than is needed" = c("item 1", "A"),\r
642     "label 2 is also very long" = c("item 2","B"),\r
643     "label 3" = c("item 3","C"),\r
644     "label 4" = c("item 4 but again with too much text","D"),\r
645     check.names = FALSE)\r
646 \r
647 x.width <- xtable(df.width,\r
648                   caption = "Using the 'tabularx' environment")\r
649 align(x.width) <- "|l|X|X|l|X|"\r
650 @\r
651 \r
652 <<results='asis'>>=\r
653 print(x.width, tabular.environment = "tabularx",\r
654       width = "\\textwidth")\r
655 @\r
656 \r
657 \section{Suppressing Printing}\r
658 By default the \texttt{print} method will print the LaTeX or HTML to standard\r
659 output and also return the character strings invisibly.  The printing to\r
660 standard output can be suppressed by specifying \texttt{print.results = FALSE}.\r
661 \r
662 <<>>=\r
663 x.out <- print(tli.table, print.results = FALSE)\r
664 @\r
665 \r
666 Formatted output can also be captured without printing with the\r
667 \texttt{toLatex} method.  This function returns an object of class\r
668 \texttt{"Latex"}.\r
669 \r
670 <<>>=\r
671 x.ltx <- toLatex(tli.table)\r
672 class(x.ltx)\r
673 x.ltx\r
674 @\r
675 \r
676 \section{Acknowledgements}\r
677 Most of the examples in this gallery are taken from the \texttt{xtable}\r
678 documentation.\r
679 \section{R Session information}\r
680 <<results='asis'>>=\r
681 toLatex(sessionInfo())\r
682 @\r
683 \end{document}\r