]> git.donarmstrong.com Git - xtable.git/blob - pkg/R/xtable.R
Proposal: new function autoformat() and xtable argument auto=TRUE/FALSE
[xtable.git] / pkg / R / xtable.R
1 ### xtable package
2 ###
3 ### Produce LaTeX and HTML tables from R objects.
4 ###
5 ### Copyright 2000-2013 David B. Dahl <dahl@stat.byu.edu>
6 ###
7 ### This file is part of the `xtable' library for R and related languages.
8 ### It is made available under the terms of the GNU General Public
9 ### License, version 2, or at your option, any later version,
10 ### incorporated herein by reference.
11 ###
12 ### This program is distributed in the hope that it will be
13 ### useful, but WITHOUT ANY WARRANTY; without even the implied
14 ### warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
15 ### PURPOSE.  See the GNU General Public License for more
16 ### details.
17 ###
18 ### You should have received a copy of the GNU General Public
19 ### License along with this program; if not, write to the Free
20 ### Software Foundation, Inc., 59 Temple Place - Suite 330, Boston,
21 ### MA 02111-1307, USA
22
23 xtable <- function(x, caption = NULL, label = NULL, align = NULL,
24                    digits = NULL, display = NULL, auto = FALSE, ...) {
25   UseMethod("xtable")
26 }
27
28
29 ## data.frame and matrix objects
30
31 xtable.data.frame <- function(x, caption = NULL, label = NULL, align = NULL,
32                               digits = NULL, display = NULL, auto = FALSE,
33                               ...) {
34   logicals <- unlist(lapply(x, is.logical))
35   ##x[, logicals] <- lapply(x[, logicals], as.character)
36   ## Patch for logicals bug, no 1911
37   ## David Scott, <d.scott@auckland.ac.nz>, 2012-08-10
38   x[, logicals] <- lapply(x[, logicals, drop = FALSE], as.character)
39   characters <- unlist(lapply(x, is.character))
40   factors <- unlist(lapply(x, is.factor))
41   ints <- sapply(x, is.integer)
42   class(x) <- c("xtable","data.frame")
43   caption(x) <- caption
44   label(x) <- label
45   if(auto && is.null(align))   align   <- xalign(x)
46   if(auto && is.null(digits))  digits  <- xdigits(x)
47   if(auto && is.null(display)) display <- xdisplay(x)
48   align(x) <- switch(1+is.null(align), align,
49                      c("r",c("r","l")[(characters|factors)+1]))
50   digits(x) <- switch(1+is.null(digits), digits, c(0,rep(2,ncol(x))))
51   ## Patch from Seth Falcon <sfalcon@fhcrc.org>, 18-May-2007
52   if (is.null(display)) {
53       display <- rep("f", ncol(x))
54       display[ints] <- "d"
55       display[characters | factors] <- "s"
56       display <- c("s", display)
57   }
58   display(x) <- display
59   return(x)
60 }
61
62 xtable.matrix <- function(x, caption = NULL, label = NULL, align = NULL,
63                           digits = NULL, display = NULL, auto = FALSE, ...) {
64   return(xtable.data.frame(data.frame(x, check.names = FALSE),
65                            caption = caption, label = label, align = align,
66                            digits = digits, display = display, auto = auto))
67 }
68
69
70 ### table objects (of 1 or 2 dimensions) by Guido Gay, 9 Feb 2007
71 ### Fixed to pass R checks by DBD, 9 May 2007
72 xtable.table <- function(x, caption = NULL, label = NULL, align = NULL,
73                        digits = NULL, display = NULL, auto = FALSE, ...) {
74   if (length(dim(x)) == 1) {
75     return(xtable.matrix(matrix(x,
76                                 dimnames = list(rownames(x),
77                                                 names(dimnames(x)))),
78                          caption = caption, label = label, align = align,
79                          digits = digits, display = display, auto = auto))
80   } else if (length(dim(x))==2) {
81     return(xtable.matrix(matrix(x, ncol = dim(x)[2], nrow = dim(x)[1],
82                                 dimnames = list(rownames(x), colnames(x))),
83                          caption = caption, label = label, align = align,
84                          digits = digits, display = display, auto = auto))
85   } else {
86     stop("xtable.table is not implemented for tables of > 2 dimensions")
87   }
88 }
89
90
91 ## anova objects
92
93 xtable.anova <- function(x, caption = NULL, label = NULL, align = NULL,
94                          digits = NULL, display = NULL, auto = FALSE, ...) {
95   suggested.digits <- c(0,rep(2, ncol(x)))
96   suggested.digits[grep("Pr\\(>", names(x))+1] <- 4
97   suggested.digits[grep("P\\(>", names(x))+1] <- 4
98   suggested.digits[grep("Df", names(x))+1] <- 0
99
100   class(x) <- c("xtable","data.frame")
101   caption(x) <- caption
102   label(x) <- label
103   if(auto && is.null(align))   align   <- xalign(x)
104   if(auto && is.null(digits))  digits  <- xdigits(x)
105   if(auto && is.null(display)) display <- xdisplay(x)
106   align(x) <- switch(1+is.null(align), align, c("l",rep("r", ncol(x))))
107   digits(x) <- switch(1+is.null(digits), digits, suggested.digits)
108   display(x) <- switch(1+is.null(display), display, c("s",rep("f", ncol(x))))
109   return(x)
110 }
111
112
113 ## aov objects
114
115 xtable.aov <- function(x, caption = NULL, label = NULL, align = NULL,
116                        digits = NULL, display = NULL, auto = FALSE, ...) {
117   return(xtable.anova(anova(x, ...), caption = caption, label = label,
118                       align = align, digits = digits, display = display,
119                       auto = auto))
120 }
121
122 xtable.summary.aov <- function(x, caption = NULL, label = NULL, align = NULL,
123                                digits = NULL, display = NULL, auto = FALSE,
124                                ...) {
125   return(xtable.anova(x[[1]], caption = caption, label = label, align = align,
126                       digits = digits, display = display, auto = auto))
127 }
128
129 xtable.summary.aovlist <- function(x, caption = NULL, label = NULL,
130                                    align = NULL, digits = NULL, display = NULL,
131                                    auto = FALSE, ...) {
132     for (i in 1:length(x)) {
133         if (i == 1) {
134             result <- xtable.summary.aov(x[[i]], caption = caption,
135                                          label = label,
136                                          align = align, digits = digits,
137                                          display = display, auto = auto)
138         } else {
139             result <- rbind(result,
140                             xtable.anova(x[[i]][[1]], caption = caption,
141                                          label = label, align = align,
142                                          digits = digits, display = display,
143                                          auto = auto))
144         }
145     }
146     return(result)
147 }
148
149 xtable.aovlist <- function(x, caption = NULL, label = NULL, align = NULL,
150                            digits = NULL, display = NULL, auto = FALSE, ...) {
151   return(xtable.summary.aovlist(summary(x), caption = caption, label = label,
152                                 align = align, digits = digits,
153                                 display = display, auto = auto))
154 }
155
156
157
158 ## lm objects
159
160 xtable.lm <- function(x, caption = NULL, label = NULL, align = NULL,
161                       digits = NULL, display = NULL, auto = FALSE, ...) {
162   return(xtable.summary.lm(summary(x), caption = caption, label = label,
163                            align = align, digits = digits, display = display,
164                            auto = auto))
165 }
166
167 xtable.summary.lm <- function(x, caption = NULL, label = NULL, align = NULL,
168                               digits = NULL, display = NULL, auto = FALSE,
169                               ...) {
170   x <- data.frame(x$coef, check.names = FALSE)
171
172   class(x) <- c("xtable","data.frame")
173   caption(x) <- caption
174   label(x) <- label
175   if(auto && is.null(align))   align   <- xalign(x)
176   if(auto && is.null(digits))  digits  <- xdigits(x)
177   if(auto && is.null(display)) display <- xdisplay(x)
178   align(x) <- switch(1+is.null(align), align, c("r","r","r","r","r"))
179   digits(x) <- switch(1+is.null(digits), digits, c(0,4,4,2,4))
180   display(x) <- switch(1+is.null(display), display, c("s","f","f","f","f"))
181   return(x)
182 }
183
184
185 ## glm objects
186
187 xtable.glm <- function(x, caption = NULL, label = NULL, align = NULL,
188                        digits = NULL, display = NULL, auto = FALSE, ...) {
189   return(xtable.summary.glm(summary(x), caption = caption,
190                             label = label, align = align,
191                             digits = digits, display = display, auto = auto))
192 }
193
194 xtable.summary.glm <- function(x, caption = NULL, label = NULL, align = NULL,
195                                digits = NULL, display = NULL, auto = FALSE,
196                                ...) {
197   return(xtable.summary.lm(x, caption = caption, label = label, align = align,
198                            digits = digits, display = display, auto = auto))
199 }
200
201
202 ## prcomp objects
203
204 xtable.prcomp <- function(x, caption = NULL, label = NULL, align = NULL,
205                           digits = NULL, display = NULL, auto = FALSE, ...) {
206   x <- data.frame(x$rotation, check.names = FALSE)
207
208   class(x) <- c("xtable","data.frame")
209   caption(x) <- caption
210   label(x) <- label
211   if(auto && is.null(align))   align   <- xalign(x)
212   if(auto && is.null(digits))  digits  <- xdigits(x)
213   if(auto && is.null(display)) display <- xdisplay(x)
214   align(x) <- switch(1+is.null(align), align, c("r",rep("r", ncol(x))))
215   digits(x) <- switch(1+is.null(digits), digits, c(0,rep(4, ncol(x))))
216   display(x) <- switch(1+is.null(display), display, c("s",rep("f", ncol(x))))
217   return(x)
218 }
219
220 xtable.summary.prcomp <- function(x, caption = NULL, label = NULL, align = NULL,
221                                   digits = NULL, display = NULL, auto = FALSE,
222                                   ...) {
223   x <- data.frame(x$importance, check.names = FALSE)
224
225   class(x) <- c("xtable","data.frame")
226   caption(x) <- caption
227   label(x) <- label
228   if(auto && is.null(align))   align   <- xalign(x)
229   if(auto && is.null(digits))  digits  <- xdigits(x)
230   if(auto && is.null(display)) display <- xdisplay(x)
231   align(x) <- switch(1+is.null(align), align, c("r",rep("r", ncol(x))))
232   digits(x) <- switch(1+is.null(digits), digits, c(0,rep(4, ncol(x))))
233   display(x) <- switch(1+is.null(display), display, c("s",rep("f", ncol(x))))
234   return(x)
235 }
236
237
238 # Slightly modified version of xtable.coxph contributed on r-help by
239 #   Date: Wed, 2 Oct 2002 17:47:56 -0500 (CDT)
240 #   From: Jun Yan <jyan@stat.wisc.edu>
241 #   Subject: Re: [R] xtable for Cox model output
242 xtable.coxph <- function (x, caption = NULL, label = NULL, align = NULL,
243                           digits = NULL, display = NULL, auto = FALSE, ...)
244 {
245   cox <- x
246   beta <- cox$coef
247   se <- sqrt(diag(cox$var))
248   if (is.null(cox$naive.var)) {
249     tmp <- cbind(beta, exp(beta), se, beta/se, 1 - pchisq((beta/se)^2, 1))
250     dimnames(tmp) <- list(names(beta),
251       c("coef", "exp(coef)", "se(coef)", "z", "p"))
252   }
253   else {
254     tmp <- cbind( beta, exp(beta), se, beta/se,
255       signif(1 - pchisq((beta/se)^2, 1), digits - 1))
256     dimnames(tmp) <- list(names(beta),
257       c("coef", "exp(coef)", "robust se", "z", "p"))
258   }
259   return(xtable(tmp, caption = caption, label = label, align = align,
260                 digits = digits, display = display, auto = auto))
261 }
262
263 # Additional method: xtable.ts
264 # Contributed by David Mitchell (davidm@netspeed.com.au)
265 # Date: July 2003
266 xtable.ts <- function(x, caption = NULL, label = NULL, align = NULL,
267                       digits = NULL, display = NULL, auto = FALSE, ...) {
268   if (inherits(x, "ts") && !is.null(ncol(x))) {
269     # COLNAMES <- paste(colnames(x));
270     tp.1 <- trunc(time(x))
271     tp.2 <- trunc(cycle(x))
272     day.abb <- c("Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat")
273     ROWNAMES <- switch(frequency(x),
274                        tp.1,
275                        "Arg2", "Arg3",              # Dummy arguments
276                        paste(tp.1, c("Q1", "Q2", "Q3", "Q4")[tp.2], sep = " "),
277                        "Arg5", "Arg6",
278                        paste("Wk.", tp.1, " ", day.abb[tp.2], sep = ""),
279                        "Arg8", "Arg9", "Arg10", "Arg11",
280                        paste(tp.1, month.abb[tp.2], sep = " "))
281     tmp <- data.frame(x, row.names = ROWNAMES);
282   }
283   else if (inherits(x, "ts") && is.null(ncol(x))) {
284     COLNAMES <- switch(frequency(x),
285                        "Value",
286                        "Arg2", "Arg3",              # Dummy arguments
287                        c("Q1", "Q2", "Q3", "Q4"),
288                        "Arg5", "Arg6",
289                        day.abb,
290                        "Arg8", "Arg9", "Arg10", "Arg11",
291                        month.abb)
292     ROWNAMES <- seq(from = start(x)[1], to = end(x)[1])
293     tmp <- data.frame(matrix(c(rep(NA, start(x)[2] - 1), x,
294                                rep(NA, frequency(x) - end(x)[2])),
295                              ncol = frequency(x), byrow = TRUE),
296                       row.names = ROWNAMES)
297     names(tmp) <- COLNAMES
298   }
299   return(xtable(tmp, caption = caption, label = label, align = align,
300                 digits = digits, display = display, auto = auto))
301 }
302
303 # Suggested by Ajay Narottam Shah <ajayshah@mayin.org> in e-mail 2006/07/22
304 xtable.zoo <- function(x, ...) {
305   return(xtable(as.ts(x), ...))
306 }
307