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 Function converting 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 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 of 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 (called a title in HTML) 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 present to specify how the object \code{x} should be
102 manipulated. All method functions should return an object whose class
103 if given by \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.tamu.edu} with contributions and
114 suggestions from many others (see source code).
116 \seealso{\code{\link{print.xtable}}, \code{\link{caption}},
117 \code{\link{label}}, \code{\link{align}}, \code{\link{digits}},
118 \code{\link{display}}, \code{\link{formatC}}, \code{\link{methods}}
122 ## Load example dataset
125 ## Demonstrate data.frame
126 tli.table <- xtable(tli[1:20, ])
127 digits(tli.table)[c(2, 6)] <- 0
129 print(tli.table, type = "html")
131 ## Demonstrate data.frame with different digits in cells
132 tli.table <- xtable(tli[1:20, ])
133 digits(tli.table) <- matrix( 0:4, nrow = 20, ncol = ncol(tli)+1 )
135 print(tli.table, type = "html")
137 ## Demonstrate matrix
138 design.matrix <- model.matrix(~ sex*grade, data = tli[1:20, ])
139 design.table <- xtable(design.matrix)
141 print(design.table, type = "html")
144 fm1 <- aov(tlimth ~ sex + ethnicty + grade + disadvg, data = tli)
145 fm1.table <- xtable(fm1)
147 print(fm1.table, type = "html")
150 fm2 <- lm(tlimth ~ sex*ethnicty, data = tli)
151 fm2.table <- xtable(fm2)
153 print(fm2.table, type = "html")
154 print(xtable(anova(fm2)))
155 print(xtable(anova(fm2)), type = "html")
156 fm2b <- lm(tlimth ~ ethnicty, data = tli)
157 print(xtable(anova(fm2b, fm2)))
158 print(xtable(anova(fm2b, fm2)), type = "html")
161 fm3 <- glm(disadvg ~ ethnicty*grade, data = tli, family = binomial())
162 fm3.table <- xtable(fm3)
164 print(fm3.table, type = "html")
165 print(xtable(anova(fm3)))
166 print(xtable(anova(fm3)), type = "html")
169 ## Taken from help(aov) in R 1.1.1
170 ## From Venables and Ripley (1997) p.210.
171 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)
172 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)
173 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)
174 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,
175 62.0,48.8,45.5,44.2,52.0,51.5,49.8,48.8,57.2,59.0,53.2,56.0)
176 npk <- data.frame(block = gl(6,4), N = factor(N), P = factor(P),
177 K = factor(K), yield = yield)
178 npk.aov <- aov(yield ~ block + N*P*K, npk)
179 op <- options(contrasts = c("contr.helmert", "contr.treatment"))
180 npk.aovE <- aov(yield ~ N*P*K + Error(block), npk)
184 print(xtable(npk.aov))
185 print(xtable(anova(npk.aov)))
186 print(xtable(summary(npk.aov)))
189 print(xtable(npk.aovE), type = "html")
190 print(xtable(summary(npk.aovE)), type = "html")
193 ## Taken from help(lm) in R 1.1.1
194 ## Annette Dobson (1990) "An Introduction to Generalized Linear Models".
195 ## Page 9: Plant Weight Data.
196 ctl <- c(4.17,5.58,5.18,6.11,4.50,4.61,5.17,4.53,5.33,5.14)
197 trt <- c(4.81,4.17,4.41,3.59,5.87,3.83,6.03,4.89,4.32,4.69)
198 group <- gl(2,10,20, labels = c("Ctl","Trt"))
199 weight <- c(ctl, trt)
200 lm.D9 <- lm(weight ~ group)
202 print(xtable(anova(lm.D9)))
205 ## Taken from help(glm) in R 1.1.1
206 ## Annette Dobson (1990) "An Introduction to Generalized Linear Models".
207 ## Page 93: Randomized Controlled Trial :
208 counts <- c(18,17,15,20,10,20,25,13,12)
211 d.AD <- data.frame(treatment, outcome, counts)
212 glm.D93 <- glm(counts ~ outcome + treatment, family = poisson())
213 print(xtable(glm.D93, align = "r|llrc"))
214 print(xtable(anova(glm.D93)), hline.after = c(1), size = "small")
216 ## Demonstration of additional formatC() arguments.
217 print(fm1.table, format.args = list(big.mark = "'", decimal.mark = ","))
219 ## Demonstration of "short caption" support.
220 fm1sc <- aov(tlimth ~ sex + ethnicty + grade, data = tli)
221 fm1sc.table <- xtable(fm1sc,
222 caption = c("ANOVA Model with Predictors Sex, Ethnicity, and Grade",
223 "ANOVA: Sex, Ethnicity, Grade"))
226 ## Demonstration of longtable support.
227 ## Remember to insert \usepackage{longtable} on your LaTeX preamble
228 x <- matrix(rnorm(1000), ncol = 10)
229 x.big <- xtable(x, label = 'tabbig',
230 caption = 'Example of longtable spanning several pages')
231 print(x.big, tabular.environment = 'longtable', floating = FALSE)
233 x.small <- xtable(x, label = 'tabsmall', caption = 'regular table env')
234 print(x.small) # default, no longtable
236 ## Demonstration of sidewaystable support.
237 ## Remember to insert \usepackage{rotating} on your LaTeX preamble
238 print(x.small, floating.environment = 'sidewaystable')
240 if(require(stats, quietly = TRUE)) {
241 ## Demonstrate prcomp
242 ## Taken from help(prcomp) in mva package of R 1.1.1
244 pr1 <- prcomp(USArrests)
246 print(xtable(summary(pr1)))
248 # ## Demonstrate princomp
249 # ## Taken from help(princomp) in mva package of R 1.1.1
250 # pr2 <- princomp(USArrests)
254 ## Demonstrate include.rownames, include.colnames,
255 ## only.contents and add.to.row arguments
257 res <- matrix(sample(0:9, size = 6*9, replace = TRUE), ncol = 6, nrow = 9)
259 digits(xres) <- rep(0, 7)
261 addtorow$pos <- list()
262 addtorow$pos[[1]] <- c(0, 2)
263 addtorow$pos[[2]] <- 4
264 addtorow$command <- c('\\vspace{2mm} \n', '\\vspace{10mm} \n')
265 print(xres, add.to.row = addtorow, include.rownames = FALSE,
266 include.colnames = TRUE, only.contents = TRUE,
267 hline.after = c(0, 0, 9, 9))
269 ## Demonstrate include.rownames, include.colnames,
270 ## only.contents and add.to.row arguments in Rweave files
274 \setlongtables % For longtable version 3.x or less
276 <<results = tex, fig = FALSE>>=
277 cat(paste(c('c', rep('cc', 34/2-1), 'c'), collapse = '@{\\hspace{2pt}}'))
284 <<results = tex, fig = FALSE>>=
287 res <- matrix(sample(0:9, size = 34*90, replace = TRUE), ncol = 34, nrow = 90)
289 digits(xres) <- rep(0, 35)
291 addtorow$pos <- list()
292 addtorow$pos[[1]] <- c(seq(4, 40, 5), seq(49, 85, 5))
293 addtorow$pos[[2]] <- 45
294 addtorow$command <- c('\\vspace{2mm} \n', '\\newpage \n')
295 print(xres, add.to.row = addtorow, include.rownames = FALSE,
296 include.colnames = FALSE, only.contents = TRUE, hline.after = NULL)
302 ## Demonstrate sanitization
303 mat <- round(matrix(c(0.9, 0.89, 200, 0.045, 2.0), c(1, 5)), 4)
304 rownames(mat) <- "$y_{t-1}$"
305 colnames(mat) <- c("$R^2$", "$\\\bar{R}^2$", "F-stat", "S.E.E", "DW")
306 print(xtable(mat), type = "latex", sanitize.text.function = function(x){x})
308 ## Demonstrate booktabs
310 print(tli.table, hline.after = c(-1,0))
311 print(tli.table, hline.after = NULL)
313 add.to.row = list(pos = list(2), command = c("\\vspace{2mm} \n")))
315 print(tli.table, booktabs = TRUE)
316 print(tli.table, booktabs = TRUE, hline.after = c(-1,0))
317 print(tli.table, booktabs = TRUE, hline.after = NULL)
318 print(tli.table, booktabs = TRUE,
319 add.to.row = list(pos = list(2), command = c("\\vspace{2mm} \n")))
320 print(tli.table, booktabs = TRUE, add.to.row = list(pos = list(2),
321 command = c("youhou\n")), tabular.environment = "longtable")
324 for(i in c("latex","html")) {
325 outFileName <- paste("xtable.", ifelse(i=="latex", "tex", i), sep = "")
326 print(tli.table, type = i, file = outFileName, append = FALSE)
327 print(design.table, type = i, file = outFileName, append = TRUE)
328 print(fm1.table, type = i, file = outFileName, append = TRUE)
329 print(fm2.table, type = i, file = outFileName, append = TRUE)
330 print(fm2.table, type = i, file = outFileName, append = TRUE,
331 math.style.negative = TRUE)
332 print(xtable(anova(fm2)), type = i, file = outFileName, append = TRUE)
333 print(xtable(anova(fm2b, fm2)), type = i, file = outFileName, append = TRUE)
334 print(fm3.table, type = i, file = outFileName, append = TRUE)
335 print(xtable(anova(fm3)), type = i, file = outFileName, append = TRUE)
336 print(xtable(npk.aov), type = i, file = outFileName, append = TRUE)
337 print(xtable(anova(npk.aov)), type = i, file = outFileName, append = TRUE)
338 print(xtable(summary(npk.aov)), type = i, file = outFileName, append = TRUE)
339 print(xtable(npk.aovE), type = i, file = outFileName, append = TRUE)
340 print(xtable(summary(npk.aovE)),
341 type = i, file = outFileName, append = TRUE)
342 if(i=="latex") cat("\\\clearpage\n", file = outFileName, append = TRUE)
344 type = i, file = outFileName, append = TRUE, latex.environment = NULL)
346 type = i, file = outFileName, append = TRUE, latex.environment = "")
348 type = i, file = outFileName, append = TRUE,
349 latex.environment = "center")
350 print(xtable(anova(lm.D9)), type = i, file = outFileName, append = TRUE)
351 print(xtable(glm.D93), type = i, file = outFileName, append = TRUE)
352 print(xtable(anova(glm.D93, test = "Chisq")),
353 type = i, file = outFileName, append = TRUE)
354 print(xtable(glm.D93, align = "r|llrc"),
355 include.rownames = FALSE, include.colnames = TRUE,
356 type = i, file = outFileName, append = TRUE)
357 print(xtable(glm.D93, align = "r||llrc"),
358 include.rownames = TRUE, include.colnames = FALSE,
359 type = i, file = outFileName, append = TRUE)
360 print(xtable(glm.D93, align = "|r||llrc"),
361 include.rownames = FALSE, include.colnames = FALSE,
362 type = i, file = outFileName, append = TRUE)
363 print(xtable(glm.D93, align = "|r||llrc|"),
364 type = i, file = outFileName, append = TRUE)
365 print(xtable(anova(glm.D93)),
366 hline.after = c(1), size = "small",
367 type = i, file = outFileName, append = TRUE)
368 if(require(stats, quietly = TRUE)) {
369 print(xtable(pr1), type = i, file = outFileName, append = TRUE)
370 print(xtable(summary(pr1)), type = i, file = outFileName, append = TRUE)
371 # print(xtable(pr2), type = i, file = outFileName, append = TRUE)
373 temp.table <- xtable(ts(cumsum(1+round(rnorm(100), 2)),
374 start = c(1954, 7), frequency = 12))
375 caption(temp.table) <- "Time series example"
376 print(temp.table, type = i, file = outFileName,
377 append = TRUE, caption.placement = "top", table.placement = "h")
378 print(temp.table, type = i, file = outFileName,
379 append = TRUE, caption.placement = "bottom", table.placement = "htb")