From fee9ba6015487fae4cf916908c7691a098ca8f93 Mon Sep 17 00:00:00 2001 From: roosen Date: Mon, 30 Jan 2012 11:29:39 +0000 Subject: [PATCH 1/1] Added "format.args" argument to "print.xtable" that can be used to pass arguments to "formatC()". git-svn-id: svn://scm.r-forge.r-project.org/svnroot/xtable@21 edb9625f-4e0d-4859-8d74-9fd3b1da38cb --- pkg/NEWS | 2 ++ pkg/R/print.xtable.R | 38 +++++++++++++++++++++++++------------- pkg/man/print.xtable.Rd | 4 +++- pkg/man/xtable.Rd | 3 +++ 4 files changed, 33 insertions(+), 14 deletions(-) diff --git a/pkg/NEWS b/pkg/NEWS index 97aa43f..25520a5 100644 --- a/pkg/NEWS +++ b/pkg/NEWS @@ -8,6 +8,8 @@ * Added an "is.null()" check on the "table.placement" argument. * Added an example of header and footer specification with longtable to the vignette. + * Added "format.args" argument to "print" that can be used to + pass additional arguments such as "big.marks" to "formatC()". 1.6-0 (2011-10-07) * Allow "table*" as a value for "floating.environment" in print.xtable(). diff --git a/pkg/R/print.xtable.R b/pkg/R/print.xtable.R index 2d74267..aadbc62 100644 --- a/pkg/R/print.xtable.R +++ b/pkg/R/print.xtable.R @@ -45,6 +45,7 @@ print.xtable <- function( math.style.negative=FALSE, html.table.attributes="border=1", print.results=TRUE, + format.args=NULL, ...) { # Claudio Agostinelli dated 2006-07-28 hline.after # By default it print an \hline before and after the columns names independently they are printed or not and at the end of the table @@ -354,23 +355,34 @@ print.xtable <- function( ina <- is.na(xcol) is.numeric.column <- is.numeric(xcol) - if(is.character(xcol)) + if(is.character(xcol)) { cols[,i+pos] <- xcol - else if(!varying.digits){ - cols[,i+pos] <- - formatC( xcol, + } else { + if (is.null(format.args)){ + format.args <- list() + } + if (is.null(format.args$decimal.mark)){ + format.args$decimal.mark <- options()$OutDec + } + if(!varying.digits){ + curFormatArgs <- c(list( + x = xcol, format = ifelse( attr( x, "digits",exact=TRUE )[i+1] < 0, "E", - attr( x, "display",exact=TRUE )[i+1] ), - digits = abs( attr( x, "digits",exact=TRUE )[i+1] ), - decimal.mark=options()$OutDec) - }else{ + attr( x, "display",exact=TRUE )[i+1] ), + digits = abs( attr( x, "digits",exact=TRUE )[i+1] )), + format.args) + cols[,i+pos] <- do.call("formatC", curFormatArgs) + }else{ for( j in 1:nrow( cols ) ) { - ### modified Claudio Agostinelli dated 2009-09-14 - ### add decimal.mark=options()$OutDec - cols[j,i+pos] <- - formatC( xcol[j], - format = ifelse( attr( x, "digits",exact=TRUE )[j,i+1] < 0, "E", attr( x, "display",exact=TRUE )[i+1] ), digits = abs( attr( x, "digits",exact=TRUE )[j,i+1] ), decimal.mark=options()$OutDec) + curFormatArgs <- c(list( + x = xcol[j], + format = ifelse( attr( x, "digits",exact=TRUE )[j,i+1] < 0, "E", + attr( x, "display",exact=TRUE )[i+1] ), + digits = abs( attr( x, "digits",exact=TRUE )[j,i+1] )), + format.args) + cols[j,i+pos] <- do.call("formatC", curFormatArgs) } + } } ## End Ian Fellows changes diff --git a/pkg/man/print.xtable.Rd b/pkg/man/print.xtable.Rd index f11aa7e..5d1ffd5 100644 --- a/pkg/man/print.xtable.Rd +++ b/pkg/man/print.xtable.Rd @@ -11,7 +11,8 @@ sanitize.text.function=NULL, sanitize.rownames.function=sanitize.text.function, sanitize.colnames.function=sanitize.text.function, math.style.negative=FALSE, html.table.attributes="border=1", - print.results=TRUE, ...)} + print.results=TRUE, + format.args=NULL, ...)} \arguments{ \item{x}{An object of class \code{"xtable"}.} \item{type}{Type of table to produce. Possible values for \code{type} are \code{"latex"} or \code{"html"}. @@ -45,6 +46,7 @@ \item{math.style.negative}{In a LaTeX table, if \code{TRUE}, then use $-$ for the negative sign (as was the behavior prior to version 1.5-3). Default value is \code{FALSE}.} \item{html.table.attributes}{In an HTML table, attributes associated with the \code{} tag. Default value is \code{border=1}.} \item{print.results}{If \code{TRUE}, the generated table is printed to standard output. Set this to \code{FALSE} if you will just be using the character vector that is returned invisibly.} + \item{format.args}{List of arguments for the \code{formatC} function. For example, standard German number separators can be specified as \code{format.args=list(big.mark = "'", decimal.mark = ","))}.} \item{...}{Additional arguments. (Currently ignored.)} } \details{ diff --git a/pkg/man/xtable.Rd b/pkg/man/xtable.Rd index 4122518..60736f8 100644 --- a/pkg/man/xtable.Rd +++ b/pkg/man/xtable.Rd @@ -193,6 +193,9 @@ glm.D93 <- glm(counts ~ outcome + treatment, family=poisson()) print(xtable(glm.D93,align="r|llrc")) print(xtable(anova(glm.D93)),hline.after=c(1),size="small") +## Demonstration of additional formatC() arguments. +print(fm1.table, format.args=list(big.mark = "'", decimal.mark = ",")) + ## Demonstration of longtable support. ## Remember to insert \usepackage{longtable} on your LaTeX preamble x <- matrix(rnorm(1000), ncol = 10) -- 2.39.2