]> git.donarmstrong.com Git - xtable.git/commitdiff
Proposal: new function autoformat() and xtable argument auto=TRUE/FALSE
authorarnima <arnima@edb9625f-4e0d-4859-8d74-9fd3b1da38cb>
Fri, 10 Oct 2014 11:02:28 +0000 (11:02 +0000)
committerarnima <arnima@edb9625f-4e0d-4859-8d74-9fd3b1da38cb>
Fri, 10 Oct 2014 11:02:28 +0000 (11:02 +0000)
git-svn-id: svn://scm.r-forge.r-project.org/svnroot/xtable@66 edb9625f-4e0d-4859-8d74-9fd3b1da38cb

pkg/DESCRIPTION
pkg/NAMESPACE
pkg/NEWS
pkg/R/autoformat.R [new file with mode: 0644]
pkg/R/formatHelpers.R [deleted file]
pkg/R/xtable.R
pkg/man/autoformat.Rd [new file with mode: 0644]
pkg/man/formatHelpers.Rd [deleted file]
pkg/man/table.attributes.Rd
pkg/man/xtable.Rd
pkg/vignettes/xtableGallery.Rnw

index 902300c711c3a7836686de7adc7c32c8a328f324..429af94bf176f82da9318bf15a8fd991b355eb4f 100644 (file)
@@ -1,6 +1,6 @@
 Package: xtable
-Version: 1.7-5
-Date: 2014/09/11
+Version: 1.8-0
+Date: 2014/10/10
 Title: Export tables to LaTeX or HTML
 Author: David B. Dahl <dahl@stat.byu.edu>
 Maintainer: David Scott <d.scott@auckland.ac.nz>
index 69f92318381fd5405aad1f069c23f84d56763988..b864e2815607668c570cbfe906fb78d8ac3282d3 100644 (file)
@@ -1,15 +1,9 @@
-
-# NAMESPACE work by Robert Gentleman <rgentlem@fhcrc.org>
-# in e-mail on July 30, 2007.
-#
-# Extended by C Roosen, 30/01/2012
-
 importFrom("utils", toLatex)
 
 export("caption<-", "caption", "label", "label<-",
    "align<-", "align", "digits<-", "digits", "display<-",
    "display", "xtable", "print.xtable", "toLatex.xtable",
-   "xalign", "xdigits", "xdisplay")
+   "autoformat", "xalign", "xdigits", "xdisplay")
 
 S3method("print", "xtable")
 S3method("toLatex", "xtable")
index 86e04134561473eea930f5b5551007d038e52529..80f04cb6cf41722164e2b1bac86b4c2a4cab1938 100644 (file)
--- a/pkg/NEWS
+++ b/pkg/NEWS
@@ -1,26 +1,17 @@
-1.7-5 (26-09-2015 NOT YET RELEASED TO CRAN)
-
-  * xalign, xdigits, xdisplay from Arni Magnusson (arnima@hafro.is)
-    added along with help file. Feature request #5686. R code for
-    these functions is in formatHelpers.R and man page is called
-    formatHelpers.Rd.
-  * xtableGallery.snw removed and replaced with
-    xtableGallery.Rnw. Both vignettes now use knitr instead of Sweave.
-  * Section added to xtableGallery showing use of xalign, xdigits, and
-    xdisplay
-
+1.8-0 (NOT YET RELEASED TO CRAN)
+  * autoformat, xalign, xdigits, xdisplay from Arni Magnusson, added
+    along with help file. Feature request #5686.
+  * New argument 'auto' in xtable(), to call xalign, xdigits, and
+    xdisplay at the time when xtable is created.
+  * Updated xtableGallery vignette, now with TOC and revised examples.
 
 1.7-4 (2014-09-11)
-  * Released to CRAN
   * Changed tags in HTML to be all lower case, to be compatible with
     HTML5, part of feature request. (#5879)
   * Fixed booktabs bug (#2309), more of an enhancement really. Updated
     xtableGallery.snw to illustrate the change.
-  * Moved vignettes from inst/doc to vignettes as now required by CRAN.
-  * Changed email address of David Dahl to dahl@stat.byu.edu in 9 places
 
 1.7-3 (2014-03-06)
-  * Released to CRAN
   * Dealt with format.args bug (#4770). No code changes, but the
     documentation of print.xtable was changed to warn of the problem
     and to give a workaround as an example.
diff --git a/pkg/R/autoformat.R b/pkg/R/autoformat.R
new file mode 100644 (file)
index 0000000..940ecdb
--- /dev/null
@@ -0,0 +1,53 @@
+autoformat <- function(xtab, zap = getOption("digits")) {
+  align(xtab) <- xalign(xtab)
+  digits(xtab) <- xdigits(xtab, zap = zap)
+  display(xtab) <- xdisplay(xtab)
+  return(xtab)
+}
+
+xalign <- function(x, pad = TRUE) {
+  lr <- function(v) if(is.numeric(v)) "r" else "l"
+
+  is.2d <- length(dim(x)) == 2
+  alignment <- if(is.2d) sapply(as.data.frame(x), lr) else lr(x)
+  output <- if(is.2d && pad) c("l", alignment) else alignment
+
+  return(output)
+}
+
+xdigits <- function(x, pad = TRUE, zap = getOption("digits")) {
+  dig <- function(v) {
+    if(is.numeric(v)) {
+      v <- na.omit(v)
+      v <- zapsmall(abs(v - floor(v)), zap)
+      dec <- if(any(v > 0)) max(nchar(v) - 2L) else 0L
+    } else {
+      dec <- 0L
+    }
+    return(dec)
+  }
+
+  is.2d <- length(dim(x)) == 2
+  decimals <- if(is.2d) sapply(as.data.frame(x), dig) else dig(x)
+  output <- if(is.2d && pad) c(0L, decimals) else decimals
+
+  return(output)
+}
+
+xdisplay <- function(x, pad = TRUE) {
+  type <- function(v) {
+    if(is.numeric(v)) {
+      tp <- if(xdigits(v) == 0) "d" else "f"
+    } else {
+      tp <- "s"
+    }
+    return(tp)
+  }
+
+  is.2d <- length(dim(x)) == 2
+  disp <- if(is.2d) sapply(as.data.frame(x), type) else type(x)
+  output <- if(is.2d && pad) c("s", disp) else disp
+
+  return(output)
+}
+
diff --git a/pkg/R/formatHelpers.R b/pkg/R/formatHelpers.R
deleted file mode 100644 (file)
index 91691e4..0000000
+++ /dev/null
@@ -1,46 +0,0 @@
-xalign <- function(x, pad = TRUE) {
-  lr <- function(v) if(is.numeric(v)) "r" else "l"
-
-  is.2d <- length(dim(x)) == 2
-  alignment <- if(is.2d) sapply(as.data.frame(x), lr) else lr(x)
-  output <- if(is.2d && pad) c("l", alignment) else alignment
-
-  return(output)
-}
-
-xdigits <- function(x, pad = TRUE, zap = getOption("digits")) {
-  dig <- function(v) {
-    if(is.numeric(v)) {
-      v <- na.omit(v)
-      v <- zapsmall(abs(v - floor(v)), zap)
-      dec <- if(any(v > 0)) max(nchar(v) - 2L) else 0L
-    } else {
-      dec <- 0L
-    }
-    return(dec)
-  }
-
-  is.2d <- length(dim(x)) == 2
-  decimals <- if(is.2d) sapply(as.data.frame(x), dig) else dig(x)
-  output <- if(is.2d && pad) c(0L, decimals) else decimals
-
-  return(output)
-}
-
-xdisplay <- function(x, pad = TRUE) {
-  type <- function(v) {
-    if(is.numeric(v)) {
-      tp <- if(xdigits(v) == 0) "d" else "f"
-    } else {
-      tp <- "s"
-    }
-    return(tp)
-  }
-
-  is.2d <- length(dim(x)) == 2
-  disp <- if(is.2d) sapply(as.data.frame(x), type) else type(x)
-  output <- if(is.2d && pad) c("s", disp) else disp
-
-  return(output)
-}
-
index 2afc43c3bcb4ac94167bb411f064d1cccbf6a098..011179d3feaf7cc6412cbb1c13872dde3cdb2fa2 100644 (file)
@@ -21,7 +21,7 @@
 ### MA 02111-1307, USA
 
 xtable <- function(x, caption = NULL, label = NULL, align = NULL,
-                   digits = NULL, display = NULL, ...) {
+                   digits = NULL, display = NULL, auto = FALSE, ...) {
   UseMethod("xtable")
 }
 
@@ -29,7 +29,8 @@ xtable <- function(x, caption = NULL, label = NULL, align = NULL,
 ## data.frame and matrix objects
 
 xtable.data.frame <- function(x, caption = NULL, label = NULL, align = NULL,
-                              digits = NULL, display = NULL, ...) {
+                              digits = NULL, display = NULL, auto = FALSE,
+                              ...) {
   logicals <- unlist(lapply(x, is.logical))
   ##x[, logicals] <- lapply(x[, logicals], as.character)
   ## Patch for logicals bug, no 1911
@@ -41,6 +42,9 @@ xtable.data.frame <- function(x, caption = NULL, label = NULL, align = NULL,
   class(x) <- c("xtable","data.frame")
   caption(x) <- caption
   label(x) <- label
+  if(auto && is.null(align))   align   <- xalign(x)
+  if(auto && is.null(digits))  digits  <- xdigits(x)
+  if(auto && is.null(display)) display <- xdisplay(x)
   align(x) <- switch(1+is.null(align), align,
                      c("r",c("r","l")[(characters|factors)+1]))
   digits(x) <- switch(1+is.null(digits), digits, c(0,rep(2,ncol(x))))
@@ -56,28 +60,28 @@ xtable.data.frame <- function(x, caption = NULL, label = NULL, align = NULL,
 }
 
 xtable.matrix <- function(x, caption = NULL, label = NULL, align = NULL,
-                          digits = NULL, display = NULL, ...) {
+                          digits = NULL, display = NULL, auto = FALSE, ...) {
   return(xtable.data.frame(data.frame(x, check.names = FALSE),
                            caption = caption, label = label, align = align,
-                           digits = digits, display = display))
+                           digits = digits, display = display, auto = auto))
 }
 
 
 ### table objects (of 1 or 2 dimensions) by Guido Gay, 9 Feb 2007
 ### Fixed to pass R checks by DBD, 9 May 2007
 xtable.table <- function(x, caption = NULL, label = NULL, align = NULL,
-                       digits = NULL, display = NULL, ...) {
+                       digits = NULL, display = NULL, auto = FALSE, ...) {
   if (length(dim(x)) == 1) {
     return(xtable.matrix(matrix(x,
                                 dimnames = list(rownames(x),
                                                 names(dimnames(x)))),
-                         caption = caption, label = label,
-                         align = align, digits = digits, display = display))
+                         caption = caption, label = label, align = align,
+                         digits = digits, display = display, auto = auto))
   } else if (length(dim(x))==2) {
     return(xtable.matrix(matrix(x, ncol = dim(x)[2], nrow = dim(x)[1],
                                 dimnames = list(rownames(x), colnames(x))),
-                         caption = caption, label = label,
-                         align = align, digits = digits, display = display))
+                         caption = caption, label = label, align = align,
+                         digits = digits, display = display, auto = auto))
   } else {
     stop("xtable.table is not implemented for tables of > 2 dimensions")
   }
@@ -87,7 +91,7 @@ xtable.table <- function(x, caption = NULL, label = NULL, align = NULL,
 ## anova objects
 
 xtable.anova <- function(x, caption = NULL, label = NULL, align = NULL,
-                         digits = NULL, display = NULL, ...) {
+                         digits = NULL, display = NULL, auto = FALSE, ...) {
   suggested.digits <- c(0,rep(2, ncol(x)))
   suggested.digits[grep("Pr\\(>", names(x))+1] <- 4
   suggested.digits[grep("P\\(>", names(x))+1] <- 4
@@ -96,6 +100,9 @@ xtable.anova <- function(x, caption = NULL, label = NULL, align = NULL,
   class(x) <- c("xtable","data.frame")
   caption(x) <- caption
   label(x) <- label
+  if(auto && is.null(align))   align   <- xalign(x)
+  if(auto && is.null(digits))  digits  <- xdigits(x)
+  if(auto && is.null(display)) display <- xdisplay(x)
   align(x) <- switch(1+is.null(align), align, c("l",rep("r", ncol(x))))
   digits(x) <- switch(1+is.null(digits), digits, suggested.digits)
   display(x) <- switch(1+is.null(display), display, c("s",rep("f", ncol(x))))
@@ -106,41 +113,44 @@ xtable.anova <- function(x, caption = NULL, label = NULL, align = NULL,
 ## aov objects
 
 xtable.aov <- function(x, caption = NULL, label = NULL, align = NULL,
-                       digits = NULL, display = NULL, ...) {
+                       digits = NULL, display = NULL, auto = FALSE, ...) {
   return(xtable.anova(anova(x, ...), caption = caption, label = label,
-                      align = align, digits = digits, display = display))
+                      align = align, digits = digits, display = display,
+                      auto = auto))
 }
 
 xtable.summary.aov <- function(x, caption = NULL, label = NULL, align = NULL,
-                               digits = NULL, display = NULL, ...) {
-  return(xtable.anova(x[[1]], caption = caption, label = label,
-                      align = align, digits = digits, display = display))
+                               digits = NULL, display = NULL, auto = FALSE,
+                               ...) {
+  return(xtable.anova(x[[1]], caption = caption, label = label, align = align,
+                      digits = digits, display = display, auto = auto))
 }
 
 xtable.summary.aovlist <- function(x, caption = NULL, label = NULL,
-                                   align = NULL,
-                                   digits = NULL, display = NULL, ...) {
+                                   align = NULL, digits = NULL, display = NULL,
+                                   auto = FALSE, ...) {
     for (i in 1:length(x)) {
         if (i == 1) {
             result <- xtable.summary.aov(x[[i]], caption = caption,
                                          label = label,
                                          align = align, digits = digits,
-                                         display = display)
+                                         display = display, auto = auto)
         } else {
             result <- rbind(result,
                             xtable.anova(x[[i]][[1]], caption = caption,
                                          label = label, align = align,
-                                         digits = digits, display = display))
+                                         digits = digits, display = display,
+                                         auto = auto))
         }
     }
     return(result)
 }
 
 xtable.aovlist <- function(x, caption = NULL, label = NULL, align = NULL,
-                           digits = NULL, display = NULL, ...) {
+                           digits = NULL, display = NULL, auto = FALSE, ...) {
   return(xtable.summary.aovlist(summary(x), caption = caption, label = label,
                                 align = align, digits = digits,
-                                display = display))
+                                display = display, auto = auto))
 }
 
 
@@ -148,18 +158,23 @@ xtable.aovlist <- function(x, caption = NULL, label = NULL, align = NULL,
 ## lm objects
 
 xtable.lm <- function(x, caption = NULL, label = NULL, align = NULL,
-                      digits = NULL, display = NULL, ...) {
+                      digits = NULL, display = NULL, auto = FALSE, ...) {
   return(xtable.summary.lm(summary(x), caption = caption, label = label,
-                           align = align, digits = digits, display = display))
+                           align = align, digits = digits, display = display,
+                           auto = auto))
 }
 
 xtable.summary.lm <- function(x, caption = NULL, label = NULL, align = NULL,
-                              digits = NULL, display = NULL, ...) {
+                              digits = NULL, display = NULL, auto = FALSE,
+                              ...) {
   x <- data.frame(x$coef, check.names = FALSE)
 
   class(x) <- c("xtable","data.frame")
   caption(x) <- caption
   label(x) <- label
+  if(auto && is.null(align))   align   <- xalign(x)
+  if(auto && is.null(digits))  digits  <- xdigits(x)
+  if(auto && is.null(display)) display <- xdisplay(x)
   align(x) <- switch(1+is.null(align), align, c("r","r","r","r","r"))
   digits(x) <- switch(1+is.null(digits), digits, c(0,4,4,2,4))
   display(x) <- switch(1+is.null(display), display, c("s","f","f","f","f"))
@@ -170,28 +185,32 @@ xtable.summary.lm <- function(x, caption = NULL, label = NULL, align = NULL,
 ## glm objects
 
 xtable.glm <- function(x, caption = NULL, label = NULL, align = NULL,
-                       digits = NULL, display = NULL, ...) {
+                       digits = NULL, display = NULL, auto = FALSE, ...) {
   return(xtable.summary.glm(summary(x), caption = caption,
                             label = label, align = align,
-                            digits = digits, display = display))
+                            digits = digits, display = display, auto = auto))
 }
 
 xtable.summary.glm <- function(x, caption = NULL, label = NULL, align = NULL,
-                               digits = NULL, display = NULL, ...) {
-  return(xtable.summary.lm(x, caption = caption, label = label,
-                           align = align, digits = digits, display = display))
+                               digits = NULL, display = NULL, auto = FALSE,
+                               ...) {
+  return(xtable.summary.lm(x, caption = caption, label = label, align = align,
+                           digits = digits, display = display, auto = auto))
 }
 
 
 ## prcomp objects
 
 xtable.prcomp <- function(x, caption = NULL, label = NULL, align = NULL,
-                          digits = NULL, display = NULL, ...) {
+                          digits = NULL, display = NULL, auto = FALSE, ...) {
   x <- data.frame(x$rotation, check.names = FALSE)
 
   class(x) <- c("xtable","data.frame")
   caption(x) <- caption
   label(x) <- label
+  if(auto && is.null(align))   align   <- xalign(x)
+  if(auto && is.null(digits))  digits  <- xdigits(x)
+  if(auto && is.null(display)) display <- xdisplay(x)
   align(x) <- switch(1+is.null(align), align, c("r",rep("r", ncol(x))))
   digits(x) <- switch(1+is.null(digits), digits, c(0,rep(4, ncol(x))))
   display(x) <- switch(1+is.null(display), display, c("s",rep("f", ncol(x))))
@@ -199,12 +218,16 @@ xtable.prcomp <- function(x, caption = NULL, label = NULL, align = NULL,
 }
 
 xtable.summary.prcomp <- function(x, caption = NULL, label = NULL, align = NULL,
-                                  digits = NULL, display = NULL, ...) {
+                                  digits = NULL, display = NULL, auto = FALSE,
+                                  ...) {
   x <- data.frame(x$importance, check.names = FALSE)
 
   class(x) <- c("xtable","data.frame")
   caption(x) <- caption
   label(x) <- label
+  if(auto && is.null(align))   align   <- xalign(x)
+  if(auto && is.null(digits))  digits  <- xdigits(x)
+  if(auto && is.null(display)) display <- xdisplay(x)
   align(x) <- switch(1+is.null(align), align, c("r",rep("r", ncol(x))))
   digits(x) <- switch(1+is.null(digits), digits, c(0,rep(4, ncol(x))))
   display(x) <- switch(1+is.null(display), display, c("s",rep("f", ncol(x))))
@@ -217,7 +240,7 @@ xtable.summary.prcomp <- function(x, caption = NULL, label = NULL, align = NULL,
 #   From: Jun Yan <jyan@stat.wisc.edu>
 #   Subject: Re: [R] xtable for Cox model output
 xtable.coxph <- function (x, caption = NULL, label = NULL, align = NULL,
-                          digits = NULL, display = NULL, ...)
+                          digits = NULL, display = NULL, auto = FALSE, ...)
 {
   cox <- x
   beta <- cox$coef
@@ -234,14 +257,14 @@ xtable.coxph <- function (x, caption = NULL, label = NULL, align = NULL,
       c("coef", "exp(coef)", "robust se", "z", "p"))
   }
   return(xtable(tmp, caption = caption, label = label, align = align,
-                digits = digits, display = display))
+                digits = digits, display = display, auto = auto))
 }
 
 # Additional method: xtable.ts
 # Contributed by David Mitchell (davidm@netspeed.com.au)
 # Date: July 2003
 xtable.ts <- function(x, caption = NULL, label = NULL, align = NULL,
-                      digits = NULL, display = NULL, ...) {
+                      digits = NULL, display = NULL, auto = FALSE, ...) {
   if (inherits(x, "ts") && !is.null(ncol(x))) {
     # COLNAMES <- paste(colnames(x));
     tp.1 <- trunc(time(x))
@@ -274,7 +297,7 @@ xtable.ts <- function(x, caption = NULL, label = NULL, align = NULL,
     names(tmp) <- COLNAMES
   }
   return(xtable(tmp, caption = caption, label = label, align = align,
-                digits = digits, display = display))
+                digits = digits, display = display, auto = auto))
 }
 
 # Suggested by Ajay Narottam Shah <ajayshah@mayin.org> in e-mail 2006/07/22
diff --git a/pkg/man/autoformat.Rd b/pkg/man/autoformat.Rd
new file mode 100644 (file)
index 0000000..1e52242
--- /dev/null
@@ -0,0 +1,75 @@
+\name{autoformat}
+\alias{autoformat}
+\alias{xalign}
+\alias{xdigits}
+\alias{xdisplay}
+\title{Automatically Format Export Tables}
+\description{
+  Suggest an appropriate alignment, number of digits, and display type
+  for \code{xtable}.
+}
+\usage{
+autoformat(xtab, zap = getOption("digits"))
+
+xalign(x, pad = TRUE)
+xdigits(x, pad = TRUE, zap = getOption("digits"))
+xdisplay(x, pad = TRUE)
+}
+\arguments{
+  \item{xtab}{an object of class \code{xtable}.}
+  \item{x}{a vector, matrix, or data frame.}
+  \item{pad}{whether to format row names, when \code{x} is
+    two-dimensional.}
+  \item{zap}{the number of digits passed to \code{zapsmall}.}
+}
+\value{
+  \code{autoformat} returns a copy of \code{xtab}, after applying
+  \code{xalign}, \code{xdigits}, and \code{xdisplay}.
+
+  \code{xalign} returns a character vector consisting of \code{"l"} and
+  \code{"r"} elements, for left/right alignment.
+
+  \code{xdigits} returns an integer vector.
+
+  \code{xdisplay} returns a character vector of \code{"d"}, \code{"f"},
+  and \code{"s"} elements, for integer/double/string display.
+}
+\author{Arni Magnusson.}
+\seealso{
+  \code{\link{xtable}}, \code{\link{align}}, \code{\link{digits}},
+  \code{\link{display}}
+}
+\examples{
+## 1  Vector
+xalign(precip)
+xdigits(precip)
+xdisplay(precip)
+
+
+## 2  Data frame
+head(mtcars)
+xdigits(mtcars, pad = FALSE)
+xdigits(mtcars, pad = TRUE)
+xalign(mtcars)
+xdisplay(mtcars)
+
+
+## 3  Autoformat when xtable is created
+xtable(mtcars, align = xalign(mtcars), digits = xdigits(mtcars),
+       display = xdisplay(mtcars))
+
+## equivalent shortcut
+xtable(mtcars, auto = TRUE)
+
+
+## 4  Autoformat existing xtable
+mt <- xtable(mtcars)
+align(mt) <- xalign(mt)
+digits(mt) <- xdigits(mt)
+display(mt) <- xdisplay(mt)
+
+## equivalent shortcut
+mt <- autoformat(mt)
+}
+\keyword{array}
+\keyword{print}
diff --git a/pkg/man/formatHelpers.Rd b/pkg/man/formatHelpers.Rd
deleted file mode 100644 (file)
index 20480d1..0000000
+++ /dev/null
@@ -1,59 +0,0 @@
-\name{xalign}
-\alias{xalign}
-\alias{xdigits}
-\alias{xdisplay}
-\title{Suggest Appropriate Formatting}
-\description{
-  Suggest an appropriate alignment, number of digits, and display type
-  for \code{xtable}.
-}
-\usage{
-xalign(x, pad = TRUE)
-xdigits(x, pad = TRUE, zap = getOption("digits"))
-xdisplay(x, pad = TRUE)
-}
-\arguments{
-  \item{x}{a vector, matrix, or data frame.}
-  \item{pad}{whether to format row names, when \code{x} is
-    two-dimensional.}
-  \item{zap}{the number of digits passed to \code{zapsmall}.}
-}
-\value{
-  \code{xalign} returns a character vector consisting of \code{"l"} and
-  \code{"r"} elements, for left/right alignment.
-
-  \code{xdigits} returns an integer vector.
-
-  \code{xdisplay} returns a character vector of \code{"d"}, \code{"f"},
-  and \code{"s"} elements, for integer/double/string display.
-}
-\author{Arni Magnusson.}
-\seealso{
-  \code{\link{xtable}}, \code{\link{align}}, \code{\link{digits}},
-  \code{\link{display}}
-}
-\examples{
-## Vector
-xalign(precip)
-xdigits(precip)
-xdisplay(precip)
-
-## Data frame
-head(mtcars)
-xdigits(mtcars, pad = FALSE)
-xdigits(mtcars, pad = TRUE)
-xalign(mtcars)
-xdisplay(mtcars)
-
-## Autoformat when xtable is created
-xtable(mtcars, align = xalign(mtcars), digits = xdigits(mtcars),
-       display = xdisplay(mtcars))
-
-## Autoformat existing xtable
-mt <- xtable(mtcars)
-align(mt) <- xalign(mt)
-digits(mt) <- xdigits(mt)
-display(mt) <- xdisplay(mt)
-}
-\keyword{array}
-\keyword{print}
index ff2be410f8c08dde4295132754ceb16880b27b9f..7d0b6e3870095f70a4792c01aaffab3c09fa7d03 100644 (file)
@@ -44,7 +44,9 @@
 }
 \author{David Dahl \email{dahl@stat.byu.edu} with contributions and suggestions from many others (see source code).}
 \seealso{
-  \code{\link{xtable}}, \code{\link{print.xtable}},
-  \code{\link{xalign}}, \code{\link{xdigits}}, \code{\link{xdisplay}}
+  \code{\link{xtable}}, \code{\link{print.xtable}}
+
+  \code{\link{autoformat}}, \code{\link{xalign}}, \code{\link{xdigits}},
+  \code{\link{xdisplay}}
 }
 \keyword{print}
index dacc66af3de6baf4a420aae0ef34fd098fd9aa6d..ca29021bd6dcbaafe32a39ce270d9edf1cef0cb1 100644 (file)
@@ -24,7 +24,7 @@
 }
 \usage{
 xtable(x, caption = NULL, label = NULL, align = NULL, digits = NULL,
-       display = NULL, ...)
+       display = NULL, auto = FALSE, ...)
 }
 \arguments{
   \item{x}{An R object of class found among \code{methods(xtable)}.  See
@@ -79,6 +79,12 @@ xtable(x, caption = NULL, label = NULL, align = NULL, digits = NULL,
     but \code{digits} as number of \emph{significant} digits.  Note that
     this can lead to quite long result strings.  Default depends on the
     class of \code{x}.}
+  \item{auto}{
+    Logical, indicating whether to apply automatic format when no value
+    is passed to \code{align}, \code{digits}, or \code{display}. This
+    \sQuote{autoformat} (based on \code{xalign}, \code{xdigits}, and
+    \code{xdisplay}) can be useful to quickly format a typical
+    \code{matrix} or \code{data.frame}. Default value is \code{FALSE}.}
   \item{...}{Additional arguments.  (Currently ignored.)}
 }
 \details{
@@ -102,9 +108,7 @@ xtable(x, caption = NULL, label = NULL, align = NULL, digits = NULL,
   manipulated.  All method functions should return an object whose class
   is \code{c("xtable","data.frame")}.  The resulting object can
   have attributes \code{caption} and \code{label}, but must have
-  attributes \code{align}, \code{digits}, and \code{display}.  It is
-  strongly recommened that you set these attributes through the provided
-  replacement functions as they perform validity checks.
+  attributes \code{align}, \code{digits}, and \code{display}.
 }
 \value{An object of class \code{"xtable"} which inherits the
   \code{data.frame} class and contains several additional attributes
@@ -118,7 +122,8 @@ xtable(x, caption = NULL, label = NULL, align = NULL, digits = NULL,
   \code{\link{label}}, \code{\link{align}}, \code{\link{digits}},
   \code{\link{display}}
 
-  \code{\link{xalign}}, \code{\link{xdigits}}, \code{\link{xdisplay}}
+  \code{\link{autoformat}}, \code{\link{xalign}}, \code{\link{xdigits}},
+  \code{\link{xdisplay}}
 }
 \examples{
 
@@ -129,6 +134,8 @@ data(tli)
 tli.table <- xtable(tli[1:20, ])
 print(tli.table)
 print(tli.table, type = "html")
+xtable(mtcars)
+xtable(mtcars, auto = TRUE)
 
 ## Demonstrate data.frame with different digits in cells
 tli.table <- xtable(tli[1:20, ])
@@ -139,7 +146,7 @@ print(tli.table, type = "html")
 
 ## Demonstrate matrix
 design.matrix <- model.matrix(~ sex*grade, data = tli[1:20, ])
-design.table <- xtable(design.matrix)
+design.table <- xtable(design.matrix, auto = TRUE)
 print(design.table)
 print(design.table, type = "html")
 
index cbab0d94950ae075b032adb895579092f0158587..8491e129fb493da37ecfeccc5c2f894f2ae17561 100644 (file)
@@ -143,7 +143,8 @@ temp.table
 # }\r
 @\r
 \r
-\section{Helper functions for formatting}\r
+\section{Automatic formatting}\r
+\subsection{Suggest alignment, digits, and display}\r
 The functions \code{xalign}, \code{xdigits}, and \code{xdisplay} are useful for\r
 formatting tables in a sensible way. Consider the output produced by the default\r
 formatting.\r
@@ -166,6 +167,24 @@ display(x) <- xdisplay(x)
 x\r
 @\r
 \r
+\subsection{Shorthand notation}\r
+For convenience, the three `autoformat' functions (\code{xalign},\r
+\code{xdigits}, and \code{xdisplay}) can be applied together when an\r
+\code{xtable} is created, using the \code{auto} argument:\r
+\r
+<<results='asis'>>=\r
+xtable(dat, auto = TRUE)\r
+@\r
+\r
+\p\r
+Similarly, the \code{autoformat} function can be used to postprocess an\r
+existing \code{xtable}:\r
+\r
+<<results='asis'>>=\r
+x <- xtable(dat)\r
+autoformat(x)\r
+@\r
+\r
 \newpage\r
 \r
 \section{Sanitization}\r