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