6 \alias{xtable.data.frame}
12 \alias{xtable.summary.aov}
13 \alias{xtable.summary.aovlist}
14 \alias{xtable.summary.glm}
15 \alias{xtable.summary.lm}
16 \alias{xtable.summary.prcomp}
20 \title{Create Export Tables}
22 Convert an R object to an \code{xtable} object, which can
23 then be printed as a LaTeX or HTML table.
26 xtable(x, caption = NULL, label = NULL, align = NULL, digits = NULL,
30 \item{x}{An R object of class found among \code{methods(xtable)}. See
31 below on how to write additional method functions for \code{xtable}.}
32 \item{caption}{Character vector of length 1 or 2 containing the
33 table's caption or title. If length is 2, the second item is the
34 "short caption" used when LaTeX generates a "List of Tables". Set to
35 \code{NULL} to suppress the caption. Default value is \code{NULL}. }
36 \item{label}{Character vector of length 1 containing the LaTeX label
37 or HTML anchor. Set to \code{NULL} to suppress the label. Default
38 value is \code{NULL}. }
39 \item{align}{Character vector of length equal to the number of columns
40 of the resulting table, indicating the alignment of the corresponding
41 columns. Also, \code{"|"} may be used to produce vertical lines
42 between columns in LaTeX tables, but these are effectively ignored
43 when considering the required length of the supplied vector. If a
44 character vector of length one is supplied, it is split as
45 \code{strsplit(align, "")[[1]]} before processing. Since the row
46 names are printed in the first column, the length of \code{align} is
47 one greater than \code{ncol(x)} if \code{x} is a
48 \code{data.frame}. Use \code{"l"}, \code{"r"}, and \code{"c"} to
49 denote left, right, and center alignment, respectively. Use
50 \code{"p\{3cm\}"} etc. for a LaTeX column of the specified width. For
51 HTML output the \code{"p"} alignment is interpreted as \code{"l"},
52 ignoring the width request. Default depends on the class of
55 Numeric vector of length equal to one (in which case it will be
56 replicated as necessary) or to the number of columns of the
57 resulting table \bold{or} matrix of the same size as the resulting
58 table, indicating the number of digits to display in the
59 corresponding columns. Since the row names are printed in the first
60 column, the length of the vector \code{digits} or the number of
61 columns of the matrix \code{digits} is one greater than
62 \code{ncol(x)} if \code{x} is a \code{data.frame}. Default depends
63 on the class of \code{x}. If values of \code{digits} are negative, the
64 corresponding values of \code{x} are displayed in scientific format
65 with \code{abs(digits)} digits.}
67 Character vector of length equal to the number of columns of the
68 resulting table, indicating the format for the corresponding columns.
69 Since the row names are printed in the first column, the length of
70 \code{display} is one greater than \code{ncol(x)} if \code{x} is a
71 \code{data.frame}. These values are passed to the \code{formatC}
72 function. Use \code{"d"} (for integers), \code{"f"}, \code{"e"},
73 \code{"E"}, \code{"g"}, \code{"G"}, \code{"fg"} (for reals), or
74 \code{"s"} (for strings). \code{"f"} gives numbers in the usual
75 \code{xxx.xxx} format; \code{"e"} and \code{"E"} give
76 \code{n.ddde+nn} or \code{n.dddE+nn} (scientific format); \code{"g"}
77 and \code{"G"} put \code{x[i]} into scientific format only if it
78 saves space to do so. \code{"fg"} uses fixed format as \code{"f"},
79 but \code{digits} as number of \emph{significant} digits. Note that
80 this can lead to quite long result strings. Default depends on the
82 \item{...}{Additional arguments. (Currently ignored.)}
86 This function extracts tabular information from \code{x} and returns
87 an object of class \code{"xtable"}. The nature of the table generated
88 depends on the class of \code{x}. For example, \code{aov} objects
89 produce ANOVA tables while \code{data.frame} objects produce a table
90 of the entire data frame. One can optionally provide a caption
91 or label (called an anchor in HTML), as well
92 as formatting specifications. Default values for \code{align},
93 \code{digits}, and \code{display} are class dependent.
95 The available method functions for \code{xtable} are given by
96 \code{methods(xtable)}. Users can extend the list of available
97 classes by writing methods for the generic function \code{xtable}.
98 These methods functions should have \code{x} as their first argument,
99 with additional arguments to specify \code{caption}, \code{label},
100 \code{align}, \code{digits}, and \code{display}. Optionally, other
101 arguments may be passed to specify how the object \code{x} should be
102 manipulated. All method functions should return an object whose class
103 is \code{c("xtable","data.frame")}. The resulting object can
104 have attributes \code{caption} and \code{label}, but must have
105 attributes \code{align}, \code{digits}, and \code{display}. It is
106 strongly recommened that you set these attributes through the provided
107 replacement functions as they perform validity checks.
109 \value{An object of class \code{"xtable"} which inherits the
110 \code{data.frame} class and contains several additional attributes
111 specifying the table formatting options.
113 \author{David Dahl \email{dahl@stat.byu.edu} with contributions and
114 suggestions from many others (see source code).
117 \code{\link{print.xtable}}, \code{\link{caption}},
118 \code{\link{label}}, \code{\link{align}}, \code{\link{digits}},
119 \code{\link{display}}
121 \code{\link{xalign}}, \code{\link{xdigits}}, \code{\link{xdisplay}}
125 ## Load example dataset
128 ## Demonstrate data.frame
129 tli.table <- xtable(tli[1:20, ])
131 print(tli.table, type = "html")
133 ## Demonstrate data.frame with different digits in cells
134 tli.table <- xtable(tli[1:20, ])
135 display(tli.table)[c(2,6)] <- "f"
136 digits(tli.table) <- matrix(0:4, nrow = 20, ncol = ncol(tli)+1)
138 print(tli.table, type = "html")
140 ## Demonstrate matrix
141 design.matrix <- model.matrix(~ sex*grade, data = tli[1:20, ])
142 design.table <- xtable(design.matrix)
144 print(design.table, type = "html")
147 fm1 <- aov(tlimth ~ sex + ethnicty + grade + disadvg, data = tli)
148 fm1.table <- xtable(fm1)
150 print(fm1.table, type = "html")
153 fm2 <- lm(tlimth ~ sex*ethnicty, data = tli)
154 fm2.table <- xtable(fm2)
156 print(fm2.table, type = "html")
157 print(xtable(anova(fm2)))
158 print(xtable(anova(fm2)), type = "html")
159 fm2b <- lm(tlimth ~ ethnicty, data = tli)
160 print(xtable(anova(fm2b, fm2)))
161 print(xtable(anova(fm2b, fm2)), type = "html")
164 fm3 <- glm(disadvg ~ ethnicty*grade, data = tli, family = binomial())
165 fm3.table <- xtable(fm3)
167 print(fm3.table, type = "html")
168 print(xtable(anova(fm3)))
169 print(xtable(anova(fm3)), type = "html")
172 ## Taken from help(aov) in R 1.1.1
173 ## From Venables and Ripley (1997) p.210.
174 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)
175 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)
176 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)
177 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,
178 62.0,48.8,45.5,44.2,52.0,51.5,49.8,48.8,57.2,59.0,53.2,56.0)
179 npk <- data.frame(block = gl(6,4), N = factor(N), P = factor(P),
180 K = factor(K), yield = yield)
181 npk.aov <- aov(yield ~ block + N*P*K, npk)
182 op <- options(contrasts = c("contr.helmert", "contr.treatment"))
183 npk.aovE <- aov(yield ~ N*P*K + Error(block), npk)
187 print(xtable(npk.aov))
188 print(xtable(anova(npk.aov)))
189 print(xtable(summary(npk.aov)))
192 print(xtable(npk.aovE), type = "html")
193 print(xtable(summary(npk.aovE)), type = "html")
196 ## Taken from help(lm) in R 1.1.1
197 ## Annette Dobson (1990) "An Introduction to Generalized Linear Models".
198 ## Page 9: Plant Weight Data.
199 ctl <- c(4.17,5.58,5.18,6.11,4.50,4.61,5.17,4.53,5.33,5.14)
200 trt <- c(4.81,4.17,4.41,3.59,5.87,3.83,6.03,4.89,4.32,4.69)
201 group <- gl(2,10,20, labels = c("Ctl","Trt"))
202 weight <- c(ctl, trt)
203 lm.D9 <- lm(weight ~ group)
205 print(xtable(anova(lm.D9)))
208 ## Taken from help(glm) in R 1.1.1
209 ## Annette Dobson (1990) "An Introduction to Generalized Linear Models".
210 ## Page 93: Randomized Controlled Trial :
211 counts <- c(18,17,15,20,10,20,25,13,12)
214 d.AD <- data.frame(treatment, outcome, counts)
215 glm.D93 <- glm(counts ~ outcome + treatment, family = poisson())
216 print(xtable(glm.D93, align = "r|llrc"))
217 print(xtable(anova(glm.D93)), hline.after = c(1), size = "small")
219 ## Demonstration of additional formatC() arguments.
220 print(fm1.table, format.args = list(big.mark = "'", decimal.mark = ","))
222 ## Demonstration of "short caption" support.
223 fm1sc <- aov(tlimth ~ sex + ethnicty + grade, data = tli)
224 fm1sc.table <- xtable(fm1sc,
225 caption = c("ANOVA Model with Predictors Sex, Ethnicity, and Grade",
226 "ANOVA: Sex, Ethnicity, Grade"))
229 ## Demonstration of longtable support.
230 ## Remember to insert \usepackage{longtable} on your LaTeX preamble
231 x <- matrix(rnorm(1000), ncol = 10)
232 x.big <- xtable(x, label = 'tabbig',
233 caption = 'Example of longtable spanning several pages')
234 print(x.big, tabular.environment = 'longtable', floating = FALSE)
236 x.small <- xtable(x, label = 'tabsmall', caption = 'regular table env')
237 print(x.small) # default, no longtable
239 ## Demonstration of sidewaystable support.
240 ## Remember to insert \usepackage{rotating} on your LaTeX preamble
241 print(x.small, floating.environment = 'sidewaystable')
243 if(require(stats, quietly = TRUE)) {
244 ## Demonstrate prcomp
245 ## Taken from help(prcomp) in mva package of R 1.1.1
247 pr1 <- prcomp(USArrests)
249 print(xtable(summary(pr1)))
251 # ## Demonstrate princomp
252 # ## Taken from help(princomp) in mva package of R 1.1.1
253 # pr2 <- princomp(USArrests)
257 ## Demonstrate include.rownames, include.colnames,
258 ## only.contents and add.to.row arguments
260 res <- matrix(sample(0:9, size = 6*9, replace = TRUE), ncol = 6, nrow = 9)
262 digits(xres) <- rep(0, 7)
264 addtorow$pos <- list()
265 addtorow$pos[[1]] <- c(0, 2)
266 addtorow$pos[[2]] <- 4
267 addtorow$command <- c('\\vspace{2mm} \n', '\\vspace{10mm} \n')
268 print(xres, add.to.row = addtorow, include.rownames = FALSE,
269 include.colnames = TRUE, only.contents = TRUE,
270 hline.after = c(0, 0, 9, 9))
272 ## Demonstrate include.rownames, include.colnames,
273 ## only.contents and add.to.row arguments in Rweave files
277 \setlongtables % For longtable version 3.x or less
279 <<results = tex, fig = FALSE>>=
280 cat(paste(c('c', rep('cc', 34/2-1), 'c'), collapse = '@{\\hspace{2pt}}'))
287 <<results = tex, fig = FALSE>>=
290 res <- matrix(sample(0:9, size = 34*90, replace = TRUE), ncol = 34, nrow = 90)
292 digits(xres) <- rep(0, 35)
294 addtorow$pos <- list()
295 addtorow$pos[[1]] <- c(seq(4, 40, 5), seq(49, 85, 5))
296 addtorow$pos[[2]] <- 45
297 addtorow$command <- c('\\vspace{2mm} \n', '\\newpage \n')
298 print(xres, add.to.row = addtorow, include.rownames = FALSE,
299 include.colnames = FALSE, only.contents = TRUE, hline.after = NULL)
305 ## Demonstrate sanitization
306 mat <- round(matrix(c(0.9, 0.89, 200, 0.045, 2.0), c(1, 5)), 4)
307 rownames(mat) <- "$y_{t-1}$"
308 colnames(mat) <- c("$R^2$", "$\\\bar{R}^2$", "F-stat", "S.E.E", "DW")
309 print(xtable(mat), type = "latex", sanitize.text.function = function(x){x})
311 ## Demonstrate booktabs
313 print(tli.table, hline.after = c(-1,0))
314 print(tli.table, hline.after = NULL)
316 add.to.row = list(pos = list(2), command = c("\\vspace{2mm} \n")))
318 print(tli.table, booktabs = TRUE)
319 print(tli.table, booktabs = TRUE, hline.after = c(-1,0))
320 print(tli.table, booktabs = TRUE, hline.after = NULL)
321 print(tli.table, booktabs = TRUE,
322 add.to.row = list(pos = list(2), command = c("\\vspace{2mm} \n")))
323 print(tli.table, booktabs = TRUE, add.to.row = list(pos = list(2),
324 command = c("youhou\n")), tabular.environment = "longtable")
327 for(i in c("latex","html")) {
328 outFileName <- paste("xtable.", ifelse(i=="latex", "tex", i), sep = "")
329 print(tli.table, type = i, file = outFileName, append = FALSE)
330 print(design.table, type = i, file = outFileName, append = TRUE)
331 print(fm1.table, type = i, file = outFileName, append = TRUE)
332 print(fm2.table, type = i, file = outFileName, append = TRUE)
333 print(fm2.table, type = i, file = outFileName, append = TRUE,
334 math.style.negative = TRUE)
335 print(xtable(anova(fm2)), type = i, file = outFileName, append = TRUE)
336 print(xtable(anova(fm2b, fm2)), type = i, file = outFileName, append = TRUE)
337 print(fm3.table, type = i, file = outFileName, append = TRUE)
338 print(xtable(anova(fm3)), type = i, file = outFileName, append = TRUE)
339 print(xtable(npk.aov), type = i, file = outFileName, append = TRUE)
340 print(xtable(anova(npk.aov)), type = i, file = outFileName, append = TRUE)
341 print(xtable(summary(npk.aov)), type = i, file = outFileName, append = TRUE)
342 print(xtable(npk.aovE), type = i, file = outFileName, append = TRUE)
343 print(xtable(summary(npk.aovE)),
344 type = i, file = outFileName, append = TRUE)
345 if(i=="latex") cat("\\\clearpage\n", file = outFileName, append = TRUE)
347 type = i, file = outFileName, append = TRUE, latex.environment = NULL)
349 type = i, file = outFileName, append = TRUE, latex.environment = "")
351 type = i, file = outFileName, append = TRUE,
352 latex.environment = "center")
353 print(xtable(anova(lm.D9)), type = i, file = outFileName, append = TRUE)
354 print(xtable(glm.D93), type = i, file = outFileName, append = TRUE)
355 print(xtable(anova(glm.D93, test = "Chisq")),
356 type = i, file = outFileName, append = TRUE)
357 print(xtable(glm.D93, align = "r|llrc"),
358 include.rownames = FALSE, include.colnames = TRUE,
359 type = i, file = outFileName, append = TRUE)
360 print(xtable(glm.D93, align = "r||llrc"),
361 include.rownames = TRUE, include.colnames = FALSE,
362 type = i, file = outFileName, append = TRUE)
363 print(xtable(glm.D93, align = "|r||llrc"),
364 include.rownames = FALSE, include.colnames = FALSE,
365 type = i, file = outFileName, append = TRUE)
366 print(xtable(glm.D93, align = "|r||llrc|"),
367 type = i, file = outFileName, append = TRUE)
368 print(xtable(anova(glm.D93)),
369 hline.after = c(1), size = "small",
370 type = i, file = outFileName, append = TRUE)
371 if(require(stats, quietly = TRUE)) {
372 print(xtable(pr1), type = i, file = outFileName, append = TRUE)
373 print(xtable(summary(pr1)), type = i, file = outFileName, append = TRUE)
374 # print(xtable(pr2), type = i, file = outFileName, append = TRUE)
376 temp.table <- xtable(ts(cumsum(1+round(rnorm(100), 2)),
377 start = c(1954, 7), frequency = 12))
378 caption(temp.table) <- "Time series example"
379 print(temp.table, type = i, file = outFileName,
380 append = TRUE, caption.placement = "top", table.placement = "h")
381 print(temp.table, type = i, file = outFileName,
382 append = TRUE, caption.placement = "bottom", table.placement = "htb")