From ffc6a9619748f977361d207be6c8a6cf43f279f3 Mon Sep 17 00:00:00 2001 From: dscott Date: Thu, 16 Aug 2012 02:59:02 +0000 Subject: [PATCH] Reformatted print.xtable.Rd, xtable.Rd, and print.xtable.R for readability. Lines kept to < 80 characters if possible, uniform indentation of code to 4 characters in .R file. git-svn-id: svn://scm.r-forge.r-project.org/svnroot/xtable@34 edb9625f-4e0d-4859-8d74-9fd3b1da38cb --- pkg/R/print.xtable.R | 1014 +++++++++++++++++++++------------------ pkg/man/print.xtable.Rd | 256 +++++++--- pkg/man/xtable.Rd | 368 +++++++------- 3 files changed, 921 insertions(+), 717 deletions(-) diff --git a/pkg/R/print.xtable.R b/pkg/R/print.xtable.R index 94cb18d..3145514 100644 --- a/pkg/R/print.xtable.R +++ b/pkg/R/print.xtable.R @@ -21,538 +21,602 @@ ### License along with this program; if not, write to the Free ### Software Foundation, Inc., 59 Temple Place - Suite 330, Boston, ### MA 02111-1307, USA -print.xtable <- function( - x, - type=getOption("xtable.type", "latex"), - file=getOption("xtable.file", ""), - append=getOption("xtable.append", FALSE), - floating=getOption("xtable.floating", TRUE), - floating.environment=getOption("xtable.floating.environment", "table"), - table.placement=getOption("xtable.table.placement", "ht"), - caption.placement=getOption("xtable.caption.placement", "bottom"), - latex.environments=getOption("xtable.latex.environments", c("center")), - tabular.environment=getOption("xtable.tabular.environment", "tabular"), - size=getOption("xtable.size", NULL), - hline.after=getOption("xtable.hline.after", c(-1,0,nrow(x))), - NA.string=getOption("xtable.NA.string", ""), - include.rownames=getOption("xtable.include.rownames", TRUE), - include.colnames=getOption("xtable.include.colnames", TRUE), - only.contents=getOption("xtable.only.contents", FALSE), - add.to.row=getOption("xtable.add.to.row", NULL), - sanitize.text.function=getOption("xtable.sanitize.text.function", NULL), - sanitize.rownames.function=getOption("xtable.sanitize.rownames.function", - sanitize.text.function), - sanitize.colnames.function=getOption("xtable.sanitize.colnames.function", - sanitize.text.function), - math.style.negative=getOption("xtable.math.style.negative", FALSE), - html.table.attributes=getOption("xtable.html.table.attributes", "border=1"), - print.results=getOption("xtable.print.results", TRUE), - format.args=getOption("xtable.format.args", NULL), - rotate.rownames=getOption("xtable.rotate.rownames", FALSE), - rotate.colnames=getOption("xtable.rotate.colnames", FALSE), +print.xtable <- function(x, + type = getOption("xtable.type", "latex"), + file = getOption("xtable.file", ""), + append = getOption("xtable.append", FALSE), + floating = getOption("xtable.floating", TRUE), + floating.environment = getOption("xtable.floating.environment", "table"), + table.placement = getOption("xtable.table.placement", "ht"), + caption.placement = getOption("xtable.caption.placement", "bottom"), + latex.environments = getOption("xtable.latex.environments", c("center")), + tabular.environment = getOption("xtable.tabular.environment", "tabular"), + size = getOption("xtable.size", NULL), + hline.after = getOption("xtable.hline.after", c(-1,0,nrow(x))), + NA.string = getOption("xtable.NA.string", ""), + include.rownames = getOption("xtable.include.rownames", TRUE), + include.colnames = getOption("xtable.include.colnames", TRUE), + only.contents = getOption("xtable.only.contents", FALSE), + add.to.row = getOption("xtable.add.to.row", NULL), + sanitize.text.function = getOption("xtable.sanitize.text.function", NULL), + sanitize.rownames.function = getOption("xtable.sanitize.rownames.function", + sanitize.text.function), + sanitize.colnames.function = getOption("xtable.sanitize.colnames.function", + sanitize.text.function), + math.style.negative = getOption("xtable.math.style.negative", FALSE), + html.table.attributes = getOption("xtable.html.table.attributes", "border=1"), + print.results = getOption("xtable.print.results", TRUE), + format.args = getOption("xtable.format.args", NULL), + rotate.rownames = getOption("xtable.rotate.rownames", FALSE), + rotate.colnames = getOption("xtable.rotate.colnames", FALSE), booktabs = getOption("xtable.booktabs", FALSE), scalebox = getOption("xtable.scalebox", NULL), width = getOption("xtable.width", NULL), - ...) { - # If caption is length 2, treat the second value as the "short caption" - caption <- attr(x,"caption",exact=TRUE) - short.caption <- NULL - if (!is.null(caption) && length(caption) > 1){ - short.caption <- caption[2] + ...) +{ + ## If caption is length 2, treat the second value as the "short caption" + caption <- attr(x,"caption",exact = TRUE) + short.caption <- NULL + if (!is.null(caption) && length(caption) > 1){ + short.caption <- caption[2] caption <- caption[1] - } - - # 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 - # Old code that set hline.after should include c(-1, 0, nrow(x)) in the hline.after vector - # If you do not want any \hline inside the data, set hline.after to NULL - # PHEADER instead the string '\\hline\n' is used in the code - # Now hline.after counts how many time a position appear - # I left an automatic PHEADER in the longtable is this correct? - - # Claudio Agostinelli dated 2006-07-28 include.rownames, include.colnames - pos <- 0 - if (include.rownames) pos <- 1 - - # Claudio Agostinelli dated 2006-07-28 hline.after checks - if (any(hline.after < -1) | any(hline.after > nrow(x))) stop("'hline.after' must be inside [-1, nrow(x)]") - - # Claudio Agostinelli dated 2006-07-28 add.to.row checks - if (!is.null(add.to.row)) { - if (is.list(add.to.row) && length(add.to.row)==2) { - if (is.null(names(add.to.row))) { - names(add.to.row) <- c('pos', 'command') - } else if (any(sort(names(add.to.row))!=c('command', 'pos'))) { - stop("the names of the elements of 'add.to.row' must be 'pos' and 'command'") - } - if (is.list(add.to.row$pos) && is.vector(add.to.row$command, mode='character')) { - if ((npos <- length(add.to.row$pos)) != length(add.to.row$command)) { - stop("the length of 'add.to.row$pos' must be equal to the length of 'add.to.row$command'") - } - if (any(unlist(add.to.row$pos) < -1) | any(unlist(add.to.row$pos) > nrow(x))) { - stop("the values in add.to.row$pos must be inside the interval [-1, nrow(x)]") + } + + ## 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 + ## Old code that set hline.after should include c(-1, 0, nrow(x)) in the hline.after vector + ## If you do not want any \hline inside the data, set hline.after to NULL + ## PHEADER instead the string '\\hline\n' is used in the code + ## Now hline.after counts how many time a position appear + ## I left an automatic PHEADER in the longtable is this correct? + + ## Claudio Agostinelli dated 2006-07-28 include.rownames, include.colnames + pos <- 0 + if (include.rownames) pos <- 1 + + ## Claudio Agostinelli dated 2006-07-28 hline.after checks + if (any(hline.after < -1) | any(hline.after > nrow(x))) { + stop("'hline.after' must be inside [-1, nrow(x)]") + } + + ## Claudio Agostinelli dated 2006-07-28 add.to.row checks + if (!is.null(add.to.row)) { + if (is.list(add.to.row) && length(add.to.row) == 2) { + if (is.null(names(add.to.row))) { + names(add.to.row) <- c('pos', 'command') + } else if (any(sort(names(add.to.row))!= c('command', 'pos'))) { + stop("the names of the elements of 'add.to.row' must be 'pos' and 'command'") + } + if (is.list(add.to.row$pos) && is.vector(add.to.row$command, + mode = 'character')) { + if ((npos <- length(add.to.row$pos)) != + length(add.to.row$command)) { + stop("the length of 'add.to.row$pos' must be equal to the length of 'add.to.row$command'") + } + if (any(unlist(add.to.row$pos) < -1) | + any(unlist(add.to.row$pos) > nrow(x))) { + stop("the values in add.to.row$pos must be inside the interval [-1, nrow(x)]") + } + } else { + stop("the first argument ('pos') of 'add.to.row' must be a list, the second argument ('command') must be a vector of mode character") + } + } else { + stop("'add.to.row' argument must be a list of length 2") } - } else { - stop("the first argument ('pos') of 'add.to.row' must be a list, the second argument ('command') must be a vector of mode character") - } } else { - stop("'add.to.row' argument must be a list of length 2") + add.to.row <- list(pos = list(), + command = vector(length = 0, mode = "character")) + npos <- 0 } - } else { - add.to.row <- list(pos=list(), command=vector(length=0, mode="character")) - npos <- 0 - } - - # Claudio Agostinelli dated 2006-07-28 add.to.row - # Add further commands at the end of rows - if (type=="latex") { - ## Original code before changes in version 1.6-1 - # PHEADER <- "\\hline\n" - - # booktabs code from Matthieu Stigler , 1 Feb 2012 - if(!booktabs){ - PHEADER <- "\\hline\n" + + ## Claudio Agostinelli dated 2006-07-28 add.to.row + ## Add further commands at the end of rows + if (type == "latex") { + ## Original code before changes in version 1.6-1 + ## PHEADER <- "\\hline\n" + + ## booktabs code from Matthieu Stigler , 1 Feb 2012 + if(!booktabs){ + PHEADER <- "\\hline\n" } else { - PHEADER <- ifelse(-1%in%hline.after, "\\toprule\n", "") - if(0%in%hline.after) { - PHEADER <- c(PHEADER, "\\midrule\n") - } - if(nrow(x)%in%hline.after) { - PHEADER <- c(PHEADER, "\\bottomrule\n") - } + PHEADER <- ifelse(-1%in%hline.after, "\\toprule\n", "") + if(0%in%hline.after) { + PHEADER <- c(PHEADER, "\\midrule\n") + } + if(nrow(x)%in%hline.after) { + PHEADER <- c(PHEADER, "\\bottomrule\n") + } + } + } else { + PHEADER <- "" } - } else { - PHEADER <- "" - } - - lastcol <- rep(" ", nrow(x)+2) - if (!is.null(hline.after)) { - # booktabs change - Matthieu Stigler: fill the hline arguments separately, 1 Feb 2012 - # - # Code before booktabs change was: - # add.to.row$pos[[npos+1]] <- hline.after - - if (!booktabs){ - add.to.row$pos[[npos+1]] <- hline.after + + lastcol <- rep(" ", nrow(x)+2) + if (!is.null(hline.after)) { + ## booktabs change - Matthieu Stigler: fill the hline arguments separately, 1 Feb 2012 + ## + ## Code before booktabs change was: + ## add.to.row$pos[[npos+1]] <- hline.after + + if (!booktabs){ + add.to.row$pos[[npos+1]] <- hline.after } else { - for(i in 1:length(hline.after)) { - add.to.row$pos[[npos+i]] <- hline.after[i] - } - } - add.to.row$command <- c(add.to.row$command, PHEADER) - } - - if ( length(add.to.row$command) > 0 ) { - for (i in 1:length(add.to.row$command)) { - addpos <- add.to.row$pos[[i]] - freq <- table(addpos) - addpos <- unique(addpos) - for (j in 1:length(addpos)) { - lastcol[addpos[j]+2] <- paste(lastcol[addpos[j]+2], paste(rep(add.to.row$command[i], freq[j]), sep="", collapse=""), sep=" ") - } - } - } - - if (length(type)>1) stop("\"type\" must have length 1") - type <- tolower(type) - if (!all(!is.na(match(type,c("latex","html"))))) stop("\"type\" must be in {\"latex\", \"html\"}") - if (!all(!is.na(match(floating.environment,c("table","table*","sidewaystable"))))) stop("\"type\" must be in {\"table\", \"table*\", \"sidewaystable\"}") - if (!is.null(table.placement) && !all(!is.na(match(unlist(strsplit(table.placement, split="")),c("H","h","t","b","p","!"))))) { - stop("\"table.placement\" must contain only elements of {\"h\",\"t\",\"b\",\"p\",\"!\"}") - } - if (!all(!is.na(match(caption.placement,c("bottom","top"))))) stop("\"caption.placement\" must be either {\"bottom\",\"top\"}") - - if (type=="latex") { - BCOMMENT <- "% " - ECOMMENT <- "\n" - # See e-mail from "John S. Walker " dated 5-19-2003 regarding "texfloat" - # See e-mail form "Fernando Henrique Ferraz P. da Rosa" " dated 10-28-2005 regarding "longtable" - if ( tabular.environment == "longtable" & floating == TRUE ) { - warning("Attempt to use \"longtable\" with floating=TRUE. Changing to FALSE.") - floating <- FALSE - } - if ( floating == TRUE ) { - # See e-mail from "Pfaff, Bernhard " dated 7-09-2003 regarding "suggestion for an amendment of the source" - # See e-mail from "Mitchell, David" " dated 2003-07-09 regarding "Additions to R xtable package" - # See e-mail from "Garbade, Sven" dated 2006-05-22 regarding the floating environment. - BTABLE <- paste("\\begin{", floating.environment, "}",ifelse(!is.null(table.placement), - paste("[",table.placement,"]",sep=""),""),"\n",sep="") - if ( is.null(latex.environments) || (length(latex.environments)==0) ) { - BENVIRONMENT <- "" - EENVIRONMENT <- "" - } - else { - BENVIRONMENT <- "" - EENVIRONMENT <- "" - for ( i in 1:length(latex.environments) ) { - if ( latex.environments[i] == "" ) next - BENVIRONMENT <- paste(BENVIRONMENT, "\\begin{",latex.environments[i],"}\n",sep="") - EENVIRONMENT <- paste("\\end{",latex.environments[i],"}\n",EENVIRONMENT,sep="") + for(i in 1:length(hline.after)) { + add.to.row$pos[[npos+i]] <- hline.after[i] + } } - } - ETABLE <- paste("\\end{", floating.environment, "}\n", sep="") + add.to.row$command <- c(add.to.row$command, PHEADER) } - else { - BTABLE <- "" - ETABLE <- "" - BENVIRONMENT <- "" - EENVIRONMENT <- "" + + if ( length(add.to.row$command) > 0 ) { + for (i in 1:length(add.to.row$command)) { + addpos <- add.to.row$pos[[i]] + freq <- table(addpos) + addpos <- unique(addpos) + for (j in 1:length(addpos)) { + lastcol[addpos[j]+2] <- paste(lastcol[addpos[j]+2], + paste(rep(add.to.row$command[i], + freq[j]), + sep = "", collapse = ""), + sep = " ") + } + } } - tmp.index.start <- 1 - if ( ! include.rownames ) { - while ( attr(x,"align",exact=TRUE)[tmp.index.start] == '|' ) tmp.index.start <- tmp.index.start + 1 - tmp.index.start <- tmp.index.start + 1 + if (length(type)>1) stop("\"type\" must have length 1") + type <- tolower(type) + if (!all(!is.na(match(type, c("latex","html"))))) { + stop("\"type\" must be in {\"latex\", \"html\"}") + } + if (!all(!is.na(match(floating.environment, + c("table","table*","sidewaystable"))))) { + stop("\"type\" must be in {\"table\", \"table*\", \"sidewaystable\"}") + } + if (!is.null(table.placement) && + !all(!is.na(match(unlist(strsplit(table.placement, split = "")), + c("H","h","t","b","p","!"))))) { + stop("\"table.placement\" must contain only elements of {\"h\",\"t\",\"b\",\"p\",\"!\"}") + } + if (!all(!is.na(match(caption.placement, c("bottom","top"))))) { + stop("\"caption.placement\" must be either {\"bottom\",\"top\"}") } - # Added "width" argument for use with "tabular*" or "tabularx" environments - CR, 7/2/12 + + if (type == "latex") { + BCOMMENT <- "% " + ECOMMENT <- "\n" + ## See e-mail from "John S. Walker " dated 5-19-2003 regarding "texfloat" + ## See e-mail form "Fernando Henrique Ferraz P. da Rosa" " dated 10-28-2005 regarding "longtable" + if ( tabular.environment == "longtable" & floating == TRUE ) { + warning("Attempt to use \"longtable\" with floating = TRUE. Changing to FALSE.") + floating <- FALSE + } + if ( floating == TRUE ) { + ## See e-mail from "Pfaff, Bernhard " dated 7-09-2003 regarding "suggestion for an amendment of the source" + ## See e-mail from "Mitchell, David" " dated 2003-07-09 regarding "Additions to R xtable package" + ## See e-mail from "Garbade, Sven" dated 2006-05-22 regarding the floating environment. + BTABLE <- paste("\\begin{", floating.environment, "}", + ifelse(!is.null(table.placement), + paste("[", table.placement, "]", sep = ""), + ""), "\n", sep = "") + if ( is.null(latex.environments) || + (length(latex.environments) == 0) ) { + BENVIRONMENT <- "" + EENVIRONMENT <- "" + } else { + BENVIRONMENT <- "" + EENVIRONMENT <- "" + for ( i in 1:length(latex.environments) ) { + if ( latex.environments[i] == "" ) next + BENVIRONMENT <- paste(BENVIRONMENT, + "\\begin{", latex.environments[i], + "}\n", sep = "") + EENVIRONMENT <- paste("\\end{", latex.environments[i], + "}\n", EENVIRONMENT, sep = "") + } + } + ETABLE <- paste("\\end{", floating.environment, "}\n", sep = "") + } else { + BTABLE <- "" + ETABLE <- "" + BENVIRONMENT <- "" + EENVIRONMENT <- "" + } + + tmp.index.start <- 1 + if ( ! include.rownames ) { + while ( attr(x, "align", exact = TRUE)[tmp.index.start] == '|' ) + tmp.index.start <- tmp.index.start + 1 + tmp.index.start <- tmp.index.start + 1 + } + ## Added "width" argument for use with "tabular*" or "tabularx" environments - CR, 7/2/12 if (is.null(width)){ - WIDTH <-"" - } else if (is.element(tabular.environment, c("tabular", "longtable"))){ - warning("Ignoring 'width' argument. The 'tabular' and 'longtable' environments do not support a width specification. Use another environment such as 'tabular*' or 'tabularx' to specify the width.") - WIDTH <- "" + WIDTH <-"" + } else if (is.element(tabular.environment, + c("tabular", "longtable"))){ + warning("Ignoring 'width' argument. The 'tabular' and 'longtable' environments do not support a width specification. Use another environment such as 'tabular*' or 'tabularx' to specify the width.") + WIDTH <- "" } else { - WIDTH <- paste("{", width, "}", sep="") + WIDTH <- paste("{", width, "}", sep = "") } - - BTABULAR <- paste("\\begin{",tabular.environment,"}", WIDTH, "{", - paste(c(attr(x, "align",exact=TRUE)[tmp.index.start:length(attr(x,"align", - exact=TRUE))], "}\n"), sep="", collapse=""), - sep="") - - ## fix 10-26-09 (robert.castelo@upf.edu) the following 'if' condition is added here to support - ## a caption on the top of a longtable - if (tabular.environment == "longtable" && caption.placement=="top") { - if (is.null(short.caption)){ - BCAPTION <- "\\caption{" - } else { - BCAPTION <- paste("\\caption[", short.caption, "]{", sep="") - } - ECAPTION <- "} \\\\ \n" - if ((!is.null(caption)) && (type=="latex")) BTABULAR <- paste(BTABULAR, BCAPTION, caption, ECAPTION, sep="") - } - # Claudio Agostinelli dated 2006-07-28 add.to.row position -1 - BTABULAR <- paste(BTABULAR,lastcol[1], sep="") - # the \hline at the end, if present, is set in full matrix - ETABULAR <- paste("\\end{",tabular.environment,"}\n",sep="") + + BTABULAR <- + paste("\\begin{", tabular.environment, "}", + WIDTH, "{", + paste(c(attr(x, "align", + exact = TRUE)[ + tmp.index.start:length(attr(x, "align", + exact = TRUE))], + "}\n"), + sep = "", collapse = ""), + sep = "") + + ## fix 10-26-09 (robert.castelo@upf.edu) the following 'if' condition is added here to support + ## a caption on the top of a longtable + if (tabular.environment == "longtable" && caption.placement == "top") { + if (is.null(short.caption)){ + BCAPTION <- "\\caption{" + } else { + BCAPTION <- paste("\\caption[", short.caption, "]{", sep = "") + } + ECAPTION <- "} \\\\ \n" + if ((!is.null(caption)) && (type == "latex")) { + BTABULAR <- paste(BTABULAR, BCAPTION, caption, ECAPTION, + sep = "") + } + } + ## Claudio Agostinelli dated 2006-07-28 add.to.row position -1 + BTABULAR <- paste(BTABULAR, lastcol[1], sep = "") + ## the \hline at the end, if present, is set in full matrix + ETABULAR <- paste("\\end{", tabular.environment, "}\n", sep = "") ## Add scalebox - CR, 7/2/12 if (!is.null(scalebox)){ - BTABULAR <- paste("\\scalebox{", scalebox, "}{\n", BTABULAR, sep="") - ETABULAR <- paste(ETABULAR, "}\n", sep="") + BTABULAR <- paste("\\scalebox{", scalebox, "}{\n", BTABULAR, + sep = "") + ETABULAR <- paste(ETABULAR, "}\n", sep = "") } - - # BSIZE contributed by Benno in e-mail dated Wednesday, December 01, 2004 - if (is.null(size) || !is.character(size)) { - BSIZE <- "" - ESIZE <- "" + + ## BSIZE contributed by Benno in e-mail dated Wednesday, December 01, 2004 + if (is.null(size) || !is.character(size)) { + BSIZE <- "" + ESIZE <- "" + } else { + if(length(grep("^\\\\", size)) == 0){ + size <- paste("\\", size, sep = "") + } + BSIZE <- paste("{", size, "\n", sep = "") + ESIZE <- "}\n" + } + BLABEL <- "\\label{" + ELABEL <- "}\n" + if (is.null(short.caption)){ + BCAPTION <- "\\caption{" } else { - if(length(grep("^\\\\",size))==0){ - size <- paste("\\",size,sep="") - } - BSIZE <- paste("{",size,"\n",sep="") - ESIZE <- "}\n" - } - BLABEL <- "\\label{" - ELABEL <- "}\n" - if (is.null(short.caption)){ - BCAPTION <- "\\caption{" - } else { - BCAPTION <- paste("\\caption[", short.caption, "]{", sep="") - } - ECAPTION <- "}\n" - BROW <- "" - EROW <- " \\\\ \n" - BTH <- "" - ETH <- "" - STH <- " & " - BTD1 <- " & " - BTD2 <- "" - BTD3 <- "" - ETD <- "" - # Based on contribution from Jonathan Swinton in e-mail dated Wednesday, January 17, 2007 - sanitize <- function(str) { - result <- str - result <- gsub("\\\\","SANITIZE.BACKSLASH",result) - result <- gsub("$","\\$",result,fixed=TRUE) - result <- gsub(">","$>$",result,fixed=TRUE) - result <- gsub("<","$<$",result,fixed=TRUE) - result <- gsub("|","$|$",result,fixed=TRUE) - result <- gsub("{","\\{",result,fixed=TRUE) - result <- gsub("}","\\}",result,fixed=TRUE) - result <- gsub("%","\\%",result,fixed=TRUE) - result <- gsub("&","\\&",result,fixed=TRUE) - result <- gsub("_","\\_",result,fixed=TRUE) - result <- gsub("#","\\#",result,fixed=TRUE) - result <- gsub("^","\\verb|^|",result,fixed=TRUE) - result <- gsub("~","\\~{}",result,fixed=TRUE) - result <- gsub("SANITIZE.BACKSLASH","$\\backslash$",result,fixed=TRUE) - return(result) + BCAPTION <- paste("\\caption[", short.caption, "]{", sep = "") } - sanitize.numbers <- function(x) { - result <- x - if ( math.style.negative ) { - # Jake Bowers in e-mail from 2008-08-20 suggested - # disabling this feature to avoid problems with LaTeX's dcolumn package. - # by Florian Wickelmaier in e-mail - # from 2008-10-03 requested the ability to use the old behavior. - for(i in 1:length(x)) { - result[i] <- gsub("-","$-$",result[i],fixed=TRUE) + ECAPTION <- "}\n" + BROW <- "" + EROW <- " \\\\ \n" + BTH <- "" + ETH <- "" + STH <- " & " + BTD1 <- " & " + BTD2 <- "" + BTD3 <- "" + ETD <- "" + ## Based on contribution from Jonathan Swinton in e-mail dated Wednesday, January 17, 2007 + sanitize <- function(str) { + result <- str + result <- gsub("\\\\", "SANITIZE.BACKSLASH", result) + result <- gsub("$", "\\$", result, fixed = TRUE) + result <- gsub(">", "$>$", result, fixed = TRUE) + result <- gsub("<", "$<$", result, fixed = TRUE) + result <- gsub("|", "$|$", result, fixed = TRUE) + result <- gsub("{", "\\{", result, fixed = TRUE) + result <- gsub("}", "\\}", result, fixed = TRUE) + result <- gsub("%", "\\%", result, fixed = TRUE) + result <- gsub("&", "\\&", result, fixed = TRUE) + result <- gsub("_", "\\_", result, fixed = TRUE) + result <- gsub("#", "\\#", result, fixed = TRUE) + result <- gsub("^", "\\verb|^|", result, fixed = TRUE) + result <- gsub("~", "\\~{}", result, fixed = TRUE) + result <- gsub("SANITIZE.BACKSLASH", "$\\backslash$", + result, fixed = TRUE) + return(result) + } + sanitize.numbers <- function(x) { + result <- x + if ( math.style.negative ) { + ## Jake Bowers in e-mail from 2008-08-20 suggested + ## disabling this feature to avoid problems with LaTeX's dcolumn package. + ## by Florian Wickelmaier in e-mail + ## from 2008-10-03 requested the ability to use the old behavior. + for(i in 1:length(x)) { + result[i] <- gsub("-", "$-$", result[i], fixed = TRUE) + } + } + return(result) + } + sanitize.final <- function(result) { + return(result) + } + } else { + BCOMMENT <- "\n" + BTABLE <- paste("\n", sep = "") + ETABLE <- "
\n" + BENVIRONMENT <- "" + EENVIRONMENT <- "" + BTABULAR <- "" + ETABULAR <- "" + BSIZE <- "" + ESIZE <- "" + BLABEL <- "\n" + BCAPTION <- paste(" ", + sep = "") + ECAPTION <- " \n" + BROW <- "" + EROW <- " \n" + BTH <- " " + ETH <- " " + STH <- " " + BTD1 <- " in e-mail dated Wednesday, January 17, 2007 + BTD2[regexpr("^p", BTD2)>0] <- "left" + BTD2[BTD2 == "r"] <- "right" + BTD2[BTD2 == "l"] <- "left" + BTD2[BTD2 == "c"] <- "center" + BTD3 <- "\"> " + ETD <- " " + sanitize <- function(str) { + result <- str + result <- gsub("&", "& ", result, fixed = TRUE) + result <- gsub(">", "> ", result, fixed = TRUE) + result <- gsub("<", "< ", result, fixed = TRUE) + ## Kurt Hornik on 2006/10/05 recommended not escaping underscores. + ## result <- gsub("_", "\\_", result, fixed=TRUE) + return(result) + } + sanitize.numbers <- function(x) { + return(x) + } + sanitize.final <- function(result) { + ## Suggested by Uwe Ligges in e-mail dated 2005-07-30. + result$text <- gsub(" *", " ", result$text, fixed = TRUE) + result$text <- gsub(' align="left"', "", result$text, + fixed = TRUE) + return(result) } - } - return(result) - } - sanitize.final <- function(result) { - return(result) - } - } else { - BCOMMENT <- "\n" - BTABLE <- paste("\n",sep="") - ETABLE <- "
\n" - BENVIRONMENT <- "" - EENVIRONMENT <- "" - BTABULAR <- "" - ETABULAR <- "" - BSIZE <- "" - ESIZE <- "" - BLABEL <- "\n" - BCAPTION <- paste(" ",sep="") - ECAPTION <- " \n" - BROW <- "" - EROW <- " \n" - BTH <- " " - ETH <- " " - STH <- " " - BTD1 <- " in e-mail dated Wednesday, January 17, 2007 - BTD2[regexpr("^p",BTD2)>0] <- "left" - BTD2[BTD2=="r"] <- "right" - BTD2[BTD2=="l"] <- "left" - BTD2[BTD2=="c"] <- "center" - BTD3 <- "\"> " - ETD <- " " - sanitize <- function(str) { - result <- str - result <- gsub("&","& ",result,fixed=TRUE) - result <- gsub(">","> ",result,fixed=TRUE) - result <- gsub("<","< ",result,fixed=TRUE) - # Kurt Hornik on 2006/10/05 recommended not escaping underscores. - # result <- gsub("_", "\\_", result, fixed=TRUE) - return(result) - } - sanitize.numbers <- function(x) { - return(x) } - sanitize.final <- function(result) { - # Suggested by Uwe Ligges in e-mail dated 2005-07-30. - result$text <- gsub(" *"," ", result$text,fixed=TRUE) - result$text <- gsub(' align="left"', "", result$text,fixed=TRUE) - return(result) + + result <- string("", file = file, append = append) + info <- R.Version() + ## modified Claudio Agostinelli dated 2006-07-28 to set automatically the package version + result <- result + BCOMMENT + type + " table generated in " + + info$language + " " + info$major + "." + info$minor + " by xtable " + packageDescription('xtable')$Version + " package" + ECOMMENT + result <- result + BCOMMENT + date() + ECOMMENT + ## Claudio Agostinelli dated 2006-07-28 only.contents + if (!only.contents) { + result <- result + BTABLE + result <- result + BENVIRONMENT + if ( floating == TRUE ) { + if ((!is.null(caption)) && + (type == "html" ||caption.placement == "top")) { + result <- result + BCAPTION + caption + ECAPTION + } + if (!is.null(attr(x, "label", exact = TRUE)) && + (type == "latex" && caption.placement == "top")) { + result <- result + BLABEL + + attr(x, "label", exact = TRUE) + ELABEL + } + } + result <- result + BSIZE + result <- result + BTABULAR } - } - - result <- string("",file=file,append=append) - info <- R.Version() - # modified Claudio Agostinelli dated 2006-07-28 to set automatically the package version - result <- result + BCOMMENT + type + " table generated in " + - info$language + " " + info$major + "." + info$minor + " by xtable " + packageDescription('xtable')$Version + " package" + ECOMMENT - result <- result + BCOMMENT + date() + ECOMMENT - # Claudio Agostinelli dated 2006-07-28 only.contents - if (!only.contents) { - result <- result + BTABLE - result <- result + BENVIRONMENT - if ( floating == TRUE ) { - if ((!is.null(caption)) && (type=="html" || caption.placement=="top")) result <- result + BCAPTION + caption + ECAPTION - if (!is.null(attr(x,"label",exact=TRUE)) && (type=="latex" && caption.placement=="top")) result <- result + BLABEL + attr(x,"label",exact=TRUE) + ELABEL + ## Claudio Agostinelli dated 2006-07-28 include.colnames, include.rownames + if (include.colnames) { + result <- result + BROW + BTH + if (include.rownames) { + result <- result + STH + } + ## David G. Whiting in e-mail 2007-10-09 + if (is.null(sanitize.colnames.function)) { + CNAMES <- sanitize(names(x)) + } else { + CNAMES <- sanitize.colnames.function(names(x)) + } + if (rotate.colnames) { + ##added by Markus Loecher, 2009-11-16 + CNAMES <- paste("\\begin{sideways}", CNAMES, "\\end{sideways}") + } + result <- result + paste(CNAMES, collapse = STH) + + result <- result + ETH + EROW } - result <- result + BSIZE - result <- result + BTABULAR - } - # Claudio Agostinelli dated 2006-07-28 include.colnames, include.rownames - if (include.colnames) { - result <- result + BROW + BTH + + cols <- matrix("", nrow = nrow(x), ncol = ncol(x)+pos) if (include.rownames) { - result <- result + STH - } - # David G. Whiting in e-mail 2007-10-09 - if (is.null(sanitize.colnames.function)) { - CNAMES <- sanitize(names(x)) - } else { - CNAMES <- sanitize.colnames.function(names(x)) + ## David G. Whiting in e-mail 2007-10-09 + if (is.null(sanitize.rownames.function)) { + RNAMES <- sanitize(row.names(x)) + } else { + RNAMES <- sanitize.rownames.function(row.names(x)) + } + if (rotate.rownames) { + ##added by Markus Loecher, 2009-11-16 + RNAMES <- paste("\\begin{sideways}", RNAMES, "\\end{sideways}") } - if (rotate.colnames) { - #added by Markus Loecher, 2009-11-16 - CNAMES <- paste("\\begin{sideways}", CNAMES, "\\end{sideways}") - } - result <- result + paste(CNAMES, collapse=STH) - - result <- result + ETH + EROW - } - - cols <- matrix("",nrow=nrow(x),ncol=ncol(x)+pos) - if (include.rownames) { - # David G. Whiting in e-mail 2007-10-09 - if (is.null(sanitize.rownames.function)) { - RNAMES <- sanitize(row.names(x)) - } else { - RNAMES <- sanitize.rownames.function(row.names(x)) + cols[, 1] <- RNAMES } - if (rotate.rownames) { - #added by Markus Loecher, 2009-11-16 - RNAMES <- paste("\\begin{sideways}", RNAMES, "\\end{sideways}") - } - cols[,1] <- RNAMES - } ## Begin vectorizing the formatting code by Ian Fellows [ian@fellstat.com] ## 06 Dec 2011 ## -# disp <- function(y) { -# if (is.factor(y)) { -# y <- levels(y)[y] -# } -# if (is.list(y)) { -# y <- unlist(y) -# } -# return(y) -# } - varying.digits <- is.matrix( attr( x, "digits",exact=TRUE ) ) - # Code for letting "digits" be a matrix was provided by Arne Henningsen in e-mail dated 2005-06-04. - #if( !varying.digits ) { - # modified Claudio Agostinelli dated 2006-07-28 - # attr(x,"digits") <- matrix( attr( x, "digits",exact=TRUE ), nrow = nrow(x), ncol = ncol(x)+1, byrow = TRUE ) - #} - for(i in 1:ncol(x)) { - xcol <- x[,i] +## disp <- function(y) { +## if (is.factor(y)) { +## y <- levels(y)[y] +## } +## if (is.list(y)) { +## y <- unlist(y) +## } +## return(y) +## } + varying.digits <- is.matrix( attr( x, "digits", exact = TRUE ) ) + ## Code for letting "digits" be a matrix was provided by Arne Henningsen in e-mail dated 2005-06-04. + ##if( !varying.digits ) { + ## modified Claudio Agostinelli dated 2006-07-28 + ## attr(x,"digits") <- matrix( attr( x, "digits",exact=TRUE ), nrow = nrow(x), ncol = ncol(x)+1, byrow = TRUE ) + ##} + for(i in 1:ncol(x)) { + xcol <- x[, i] if(is.factor(xcol)) - xcol <- as.character(xcol) + xcol <- as.character(xcol) if(is.list(xcol)) - xcol <- sapply(xcol,unlist) - ina <- is.na(xcol) - is.numeric.column <- is.numeric(xcol) + xcol <- sapply(xcol, unlist) + ina <- is.na(xcol) + is.numeric.column <- is.numeric(xcol) if(is.character(xcol)) { - cols[,i+pos] <- xcol + cols[, i+pos] <- 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] )), - format.args) - cols[,i+pos] <- do.call("formatC", curFormatArgs) - }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] )), + format.args) + cols[, i+pos] <- do.call("formatC", curFormatArgs) + }else{ for( j in 1:nrow( cols ) ) { - 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) + 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 - - if ( any(ina) ) cols[ina,i+pos] <- NA.string - # Based on contribution from Jonathan Swinton in e-mail dated Wednesday, January 17, 2007 - if ( is.numeric.column ) { - cols[,i+pos] <- sanitize.numbers(cols[,i+pos]) - } else { - if (is.null(sanitize.text.function)) { - cols[,i+pos] <- sanitize(cols[,i+pos]) - } else { - cols[,i+pos] <- sanitize.text.function(cols[,i+pos]) - } - } - } - - multiplier <- 5 - full <- matrix("",nrow=nrow(x),ncol=multiplier*(ncol(x)+pos)+2) - full[,1] <- BROW - full[,multiplier*(0:(ncol(x)+pos-1))+2] <- BTD1 - full[,multiplier*(0:(ncol(x)+pos-1))+3] <- BTD2 - full[,multiplier*(0:(ncol(x)+pos-1))+4] <- BTD3 - full[,multiplier*(0:(ncol(x)+pos-1))+5] <- cols - full[,multiplier*(0:(ncol(x)+pos-1))+6] <- ETD - - full[,multiplier*(ncol(x)+pos)+2] <- paste(EROW, lastcol[-(1:2)], sep=" ") - - if (type=="latex") full[,2] <- "" - result <- result + lastcol[2] + paste(t(full),collapse="") - if (!only.contents) { - if (tabular.environment == "longtable") { - # booktabs change added the if() - 1 Feb 2012 - if(!booktabs) { - result <- result + PHEADER - } - - ## fix 10-27-09 Liviu Andronic (landronimirc@gmail.com) the following 'if' condition is inserted in order to avoid - ## that bottom caption interferes with a top caption of a longtable - if(caption.placement=="bottom"){ - if ((!is.null(caption)) && (type=="latex")) result <- result + BCAPTION + caption + ECAPTION - } - if (!is.null(attr(x,"label",exact=TRUE))) result <- result + BLABEL + attr(x,"label",exact=TRUE) + ELABEL - ETABULAR <- "\\end{longtable}\n" + + if ( any(ina) ) cols[ina, i+pos] <- NA.string + ## Based on contribution from Jonathan Swinton in e-mail dated Wednesday, January 17, 2007 + if ( is.numeric.column ) { + cols[, i+pos] <- sanitize.numbers(cols[, i+pos]) + } else { + if (is.null(sanitize.text.function)) { + cols[, i+pos] <- sanitize(cols[, i+pos]) + } else { + cols[, i+pos] <- sanitize.text.function(cols[, i+pos]) + } + } } - result <- result + ETABULAR - result <- result + ESIZE - if ( floating == TRUE ) { - if ((!is.null(caption)) && (type=="latex" && caption.placement=="bottom")) result <- result + BCAPTION + caption + ECAPTION - if (!is.null(attr(x,"label",exact=TRUE)) && caption.placement=="bottom") result <- result + BLABEL + attr(x,"label",exact=TRUE) + ELABEL + + multiplier <- 5 + full <- matrix("", nrow = nrow(x), ncol = multiplier*(ncol(x)+pos)+2) + full[, 1] <- BROW + full[, multiplier*(0:(ncol(x)+pos-1))+2] <- BTD1 + full[, multiplier*(0:(ncol(x)+pos-1))+3] <- BTD2 + full[, multiplier*(0:(ncol(x)+pos-1))+4] <- BTD3 + full[, multiplier*(0:(ncol(x)+pos-1))+5] <- cols + full[, multiplier*(0:(ncol(x)+pos-1))+6] <- ETD + + full[, multiplier*(ncol(x)+pos)+2] <- paste(EROW, lastcol[-(1:2)], + sep = " ") + + if (type == "latex") full[, 2] <- "" + result <- result + lastcol[2] + paste(t(full), collapse = "") + if (!only.contents) { + if (tabular.environment == "longtable") { + ## booktabs change added the if() - 1 Feb 2012 + if(!booktabs) { + result <- result + PHEADER + } + + ## fix 10-27-09 Liviu Andronic (landronimirc@gmail.com) the following 'if' condition is inserted in order to avoid + ## that bottom caption interferes with a top caption of a longtable + if(caption.placement == "bottom"){ + if ((!is.null(caption)) && (type == "latex")) { + result <- result + BCAPTION + caption + ECAPTION + } + } + if (!is.null(attr(x, "label", exact = TRUE))) { + result <- result + BLABEL + attr(x, "label", exact = TRUE) + + ELABEL + } + ETABULAR <- "\\end{longtable}\n" + } + result <- result + ETABULAR + result <- result + ESIZE + if ( floating == TRUE ) { + if ((!is.null(caption)) && + (type == "latex" && caption.placement == "bottom")) { + result <- result + BCAPTION + caption + ECAPTION + } + if (!is.null(attr(x, "label", exact = TRUE)) && + caption.placement == "bottom") { + result <- result + BLABEL + attr(x, "label", exact = TRUE) + + ELABEL + } + } + result <- result + EENVIRONMENT + result <- result + ETABLE } - result <- result + EENVIRONMENT - result <- result + ETABLE - } - result <- sanitize.final(result) - - if (print.results){ + result <- sanitize.final(result) + + if (print.results){ print(result) - } - - return(invisible(result$text)) -} + } -"+.string" <- function(x,y) { - x$text <- paste(x$text,as.string(y)$text,sep="") - return(x) + return(invisible(result$text)) } -print.string <- function(x,...) { - cat(x$text,file=x$file,append=x$append) - return(invisible()) +"+.string" <- function(x, y) { + x$text <- paste(x$text, as.string(y)$text, sep = "") + return(x) } -string <- function(text,file="",append=FALSE) { - x <- list(text=text,file=file,append=append) - class(x) <- "string" - return(x) +print.string <- function(x, ...) { + cat(x$text, file = x$file, append = x$append) + return(invisible()) } -as.string <- function(x,file="",append=FALSE) { - if (is.null(attr(x,"class",exact=TRUE))) - switch(data.class(x), - character=return(string(x,file,append)), - numeric=return(string(as.character(x),file,append)), - stop("Cannot coerse argument to a string")) - if (class(x)=="string") +string <- function(text, file = "", append = FALSE) { + x <- list(text = text, file = file, append = append) + class(x) <- "string" return(x) - stop("Cannot coerse argument to a string") +} + +as.string <- function(x, file = "", append = FALSE) { + if (is.null(attr(x, "class", exact = TRUE))) + switch(data.class(x), + character = return(string(x, file, append)), + numeric = return(string(as.character(x), file, append)), + stop("Cannot coerse argument to a string")) + if (class(x) == "string") + return(x) + stop("Cannot coerse argument to a string") } is.string <- function(x) { - return(class(x)=="string") + return(class(x) == "string") } diff --git a/pkg/man/print.xtable.Rd b/pkg/man/print.xtable.Rd index 26dfe98..03e3589 100644 --- a/pkg/man/print.xtable.Rd +++ b/pkg/man/print.xtable.Rd @@ -1,98 +1,202 @@ \name{print.xtable} \alias{print.xtable} \title{Print Export Tables} -\description{Function returning and displaying or writing to disk the LaTeX or HTML code associated with the supplied object of class \code{xtable}.} +\description{ + Function returning and displaying or writing to disk the LaTeX or HTML + code associated with the supplied object of class \code{xtable}. +} \usage{ \method{print}{xtable}(x, - type=getOption("xtable.type", "latex"), - file=getOption("xtable.file", ""), - append=getOption("xtable.append", FALSE), - floating=getOption("xtable.floating", TRUE), - floating.environment=getOption("xtable.floating.environment", "table"), - table.placement=getOption("xtable.table.placement", "ht"), - caption.placement=getOption("xtable.caption.placement", "bottom"), - latex.environments=getOption("xtable.latex.environments", c("center")), - tabular.environment=getOption("xtable.tabular.environment", "tabular"), - size=getOption("xtable.size", NULL), - hline.after=getOption("xtable.hline.after", c(-1,0,nrow(x))), - NA.string=getOption("xtable.NA.string", ""), - include.rownames=getOption("xtable.include.rownames", TRUE), - include.colnames=getOption("xtable.include.colnames", TRUE), - only.contents=getOption("xtable.only.contents", FALSE), - add.to.row=getOption("xtable.add.to.row", NULL), - sanitize.text.function=getOption("xtable.sanitize.text.function", NULL), - sanitize.rownames.function=getOption("xtable.sanitize.rownames.function", - sanitize.text.function), - sanitize.colnames.function=getOption("xtable.sanitize.colnames.function", - sanitize.text.function), - math.style.negative=getOption("xtable.math.style.negative", FALSE), - html.table.attributes=getOption("xtable.html.table.attributes", "border=1"), - print.results=getOption("xtable.print.results", TRUE), - format.args=getOption("xtable.format.args", NULL), - rotate.rownames=getOption("xtable.rotate.rownames", FALSE), - rotate.colnames=getOption("xtable.rotate.colnames", FALSE), + type = getOption("xtable.type", "latex"), + file = getOption("xtable.file", ""), + append = getOption("xtable.append", FALSE), + floating = getOption("xtable.floating", TRUE), + floating.environment = getOption("xtable.floating.environment", "table"), + table.placement = getOption("xtable.table.placement", "ht"), + caption.placement = getOption("xtable.caption.placement", "bottom"), + latex.environments = getOption("xtable.latex.environments", c("center")), + tabular.environment = getOption("xtable.tabular.environment", "tabular"), + size = getOption("xtable.size", NULL), + hline.after = getOption("xtable.hline.after", c(-1,0,nrow(x))), + NA.string = getOption("xtable.NA.string", ""), + include.rownames = getOption("xtable.include.rownames", TRUE), + include.colnames = getOption("xtable.include.colnames", TRUE), + only.contents = getOption("xtable.only.contents", FALSE), + add.to.row = getOption("xtable.add.to.row", NULL), + sanitize.text.function = getOption("xtable.sanitize.text.function", NULL), + sanitize.rownames.function = getOption("xtable.sanitize.rownames.function", + sanitize.text.function), + sanitize.colnames.function = getOption("xtable.sanitize.colnames.function", + sanitize.text.function), + math.style.negative = getOption("xtable.math.style.negative", FALSE), + html.table.attributes = getOption("xtable.html.table.attributes", + "border=1"), + print.results = getOption("xtable.print.results", TRUE), + format.args = getOption("xtable.format.args", NULL), + rotate.rownames = getOption("xtable.rotate.rownames", FALSE), + rotate.colnames = getOption("xtable.rotate.colnames", FALSE), booktabs = getOption("xtable.booktabs", FALSE), scalebox = getOption("xtable.scalebox", NULL), width = getOption("xtable.width", 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"}. - Default value is \code{"latex"}.} - \item{file}{Name of file where the resulting code should be saved. If \code{file=""}, output is displayed on screen. Note that - the function also (invisibly) returns a character vector of the results (which can be helpful for post-processing). - Default value is \code{""}.} - \item{append}{If \code{TRUE} and \code{file!=""}, code will be appended to \code{file} instead of overwriting \code{file}. - Default value is \code{FALSE}.} - \item{floating}{If \code{TRUE} and \code{type="latex"}, the resulting table will be a floating table (using, for example, \code{\\begin\{table\}} and \code{\\end\{table\}}). See \code{floating.environment} below. Default value is \code{TRUE}.} - \item{floating.environment}{If \code{floating=TRUE} and \code{type="latex"}, the resulting table uses the specified floating environment. - Possible values are \code{"table"}, \code{"table*"}, or \code{"sidewaystable"} (defined in the LaTeX package 'rotating'). - Default value is \code{"table"}.} - \item{table.placement}{If \code{floating=TRUE} and \code{type="latex"}, the floating table will have placement given by \code{table.placement} where \code{table.placement} must be \code{NULL} or contain only elements of \{"h","t","b","p","!","H"\}. - Default value is \code{"ht"}.} - \item{caption.placement}{The caption will be have placed at the bottom of the table if \code{caption.placement} is \code{"bottom"} and at the top of the table if it equals \code{"top"}. - Default value is \code{"bottom"}.} - \item{latex.environments}{If \code{floating=TRUE} and \code{type="latex"}, the specificed latex environments (provided as a character vector) will enclose the tabuluar environment. - Default value is \code{"center"}.} - \item{tabular.environment}{When \code{type="latex"}, the tabular environment that will be used. Defaults to \code{"tabular"}. When working with tables that extend more than one page, using \code{tabular.environment="longtable"} and the LaTeX package \code{"longtable"} (see Fairbairns, 2005) allows one to typeset them uniformly. Note that \code{"floating"} should be set to \code{"FALSE"} when using the \code{"longtable"} environment.} - \item{size}{An arbitrary character vector intended to be used to set the font size in a LaTeX table. The supplied value (if not \code{NULL}) is inserted just before the tabular environment starts. Default value is \code{NULL}.} - \item{hline.after}{When \code{type="latex"}, a vector of numbers between -1 and \code{"nrow(x)"}, inclusive, indicating the rows after which a horizontal line should appear. If \code{NULL} is used no lines are produced. Default value is \code{c(-1,0,nrow(x))} which means draw a line before and after the columns names and at the end of the table. Repeated values are allowed.} - \item{NA.string}{String to be used for missing values in table entries. Default value is \code{""}.} - \item{include.rownames}{logical. If \code{TRUE} the rows names is printed. Default value is \code{TRUE}.} - \item{include.colnames}{logical. If \code{TRUE} the columns names is printed. Default value is \code{TRUE}.} - \item{only.contents}{logical. If \code{TRUE} only the rows of the table is printed. Default value is \code{FALSE}.} - \item{add.to.row}{a list of two components. The first component (which should be called 'pos') is a list contains the position of rows on which extra commands should be added at the end, The second component (which should be called 'command') is a character vector of the same length of the first component which contains the command that should be added at the end of the specified rows. Default value is \code{NULL}, i.e. do not add commands.} - \item{sanitize.text.function}{All non-numeric enteries (except row and column names) are sanitised in an attempt to remove characters which have special meaning for the output format. If \code{sanitize.text.function} is not NULL (the default), it should be a function taking a character vector and returning one, and will be used for the sanitization instead of the default internal function.} - \item{sanitize.rownames.function}{Like the \code{sanitize.text.function}, but applicable to row names. The default uses the \code{sanitize.text.function}.} - \item{sanitize.colnames.function}{Like the \code{sanitize.text.function}, but applicable to column names. The default uses the \code{sanitize.text.function}.} - \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{rotate.rownames}{If \code{TRUE}, the row names are displayed vertically in LaTeX.} - \item{rotate.colnames}{If \code{TRUE}, the column names are displayed vertically in LaTeX.} - \item{booktabs}{If \code{TRUE}, the \code{toprule}, \code{midrule} and \code{bottomrule} tags from the LaTex "booktabs" package are used rather than \code{hline} for the horizontal line tags.} - \item{scalebox}{If not \code{NULL}, a \code{scalebox} clause will be added around the tabular environment with the specified value used as the scaling factor.} - \item{width}{If not \code{NULL}, the specified value is included in parenthesis between the tabular environment \code{begin} tag and the alignment specification. This allows specification of the table width when using tabular environments such as \code{tabular*} and \code{tabularx}. Note that table width specification is not supported with the \code{tabular} or \code{longtable} environments.} + \item{type}{Type of table to produce. Possible values for \code{type} + are \code{"latex"} or \code{"html"}. + Default value is \code{"latex"}.} + \item{file}{Name of file where the resulting code should be saved. If + \code{file=""}, output is displayed on screen. Note that the + function also (invisibly) returns a character vector of the results + (which can be helpful for post-processing). + Default value is \code{""}.} + \item{append}{If \code{TRUE} and \code{file!=""}, code will be + appended to \code{file} instead of overwriting \code{file}. + Default value is \code{FALSE}.} + \item{floating}{If \code{TRUE} and \code{type="latex"}, the resulting + table will be a floating table (using, for example, + \code{\\begin\{table\}} and \code{\\end\{table\}}). See + \code{floating.environment} below. + Default value is \code{TRUE}. } + \item{floating.environment}{If \code{floating=TRUE} and + \code{type="latex"}, the resulting table uses the specified floating + environment. Possible values are \code{"table"}, \code{"table*"}, or + \code{"sidewaystable"} (defined in the LaTeX package + 'rotating'). + Default value is \code{"table"}.} + \item{table.placement}{If \code{floating=TRUE} and + \code{type="latex"}, the floating table will have placement given by + \code{table.placement} where \code{table.placement} must be + \code{NULL} or contain only elements of + \{"h","t","b","p","!","H"\}. + Default value is \code{"ht"}.} + \item{caption.placement}{The caption will be have placed at the bottom + of the table if \code{caption.placement} is \code{"bottom"} and at + the top of the table if it equals \code{"top"}. + Default value is \code{"bottom"}.} + \item{latex.environments}{If \code{floating=TRUE} and + \code{type="latex"}, the specificed latex environments (provided as + a character vector) will enclose the tabular environment. + Default value is \code{"center"}. } + \item{tabular.environment}{When \code{type="latex"}, the tabular + environment that will be used. + Defaults to \code{"tabular"}. + When working with tables that extend more than one page, using + \code{tabular.environment="longtable"} and the LaTeX package + \code{"longtable"} (see Fairbairns, 2005) allows one to typeset them + uniformly. Note that \code{"floating"} should be set to + \code{"FALSE"} when using the \code{"longtable"} environment.} + \item{size}{An arbitrary character vector intended to be used to set + the font size in a LaTeX table. The supplied value (if not + \code{NULL}) is inserted just before the tabular environment + starts. + Default value is \code{NULL}. } + \item{hline.after}{When \code{type="latex"}, a vector of numbers + between -1 and \code{"nrow(x)"}, inclusive, indicating the rows + after which a horizontal line should appear. If \code{NULL} is used + no lines are produced. + Default value is \code{c(-1,0,nrow(x))} which means draw a line + before and after the columns names and at the end of the + table. Repeated values are allowed.} + \item{NA.string}{String to be used for missing values in table + entries. + Default value is \code{""}.} + \item{include.rownames}{logical. If \code{TRUE} the rows names is + printed. + Default value is \code{TRUE}.} + \item{include.colnames}{logical. If \code{TRUE} the columns names is + printed. + Default value is \code{TRUE}.} + \item{only.contents}{logical. If \code{TRUE} only the rows of the + table is printed. + Default value is \code{FALSE}. } + \item{add.to.row}{a list of two components. The first component (which + should be called 'pos') is a list contains the position of rows on + which extra commands should be added at the end, The second + component (which should be called 'command') is a character vector + of the same length of the first component which contains the command + that should be added at the end of the specified rows. + Default value is \code{NULL}, i.e. do not add commands.} + \item{sanitize.text.function}{All non-numeric enteries (except row and + column names) are sanitised in an attempt to remove characters which + have special meaning for the output format. If + \code{sanitize.text.function} is not NULL (the default), it should + be a function taking a character vector and returning one, and will + be used for the sanitization instead of the default internal + function.} + \item{sanitize.rownames.function}{Like the + \code{sanitize.text.function}, but applicable to row names. + The default uses the \code{sanitize.text.function}. } + \item{sanitize.colnames.function}{Like the + \code{sanitize.text.function}, but applicable to column names. + The default uses the \code{sanitize.text.function}. } + \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{rotate.rownames}{If \code{TRUE}, the row names are displayed + vertically in LaTeX. } + \item{rotate.colnames}{If \code{TRUE}, the column names are displayed + vertically in LaTeX. } + \item{booktabs}{If \code{TRUE}, the \code{toprule}, \code{midrule} and + \code{bottomrule} tags from the LaTex "booktabs" package are used + rather than \code{hline} for the horizontal line tags. } + \item{scalebox}{If not \code{NULL}, a \code{scalebox} clause will be + added around the tabular environment with the specified value used + as the scaling factor. } + \item{width}{If not \code{NULL}, the specified value is included in + parenthesis between the tabular environment \code{begin} tag and the + alignment specification. This allows specification of the table + width when using tabular environments such as \code{tabular*} and + \code{tabularx}. Note that table width specification is not + supported with the \code{tabular} or \code{longtable} environments. } \item{...}{Additional arguments. (Currently ignored.)} } \details{ - This function displays or writes to disk the code to produce a table associated with an object \code{x} of class \code{"xtable"}. - The resulting code is either a LaTeX or HTML table, depending on the value of \code{type}. The function also (invisibly) returns a character vector - of the results (which can be helpful for post-processing). + This function displays or writes to disk the code to produce a table + associated with an object \code{x} of class \code{"xtable"}. + The resulting code is either a LaTeX or HTML table, depending on the + value of \code{type}. The function also (invisibly) returns a + character vector of the results (which can be helpful for + post-processing). - Since version 1.4 the non default behavior of \code{hline.after} is changed. To obtain the same results as the previous versions add to the \code{hline.after} vector the vector \code{c(-1, 0, nrow(x))} where \code{nrow(x)} is the numbers of rows of the object. + Since version 1.4 the non default behavior of \code{hline.after} is + changed. To obtain the same results as the previous versions add to + the \code{hline.after} vector the vector \code{c(-1, 0, nrow(x))} + where \code{nrow(x)} is the numbers of rows of the object. - From version 1.4-3, all non-numeric columns are sanitized, and all LaTeX special characters are sanitised for LaTeX output. See Section 3 of the \code{xtableGallery} vignette for an example of customising the sanitization. - From version 1.4-4, the sanitization also applies to column names. To remove any text sanitization, specify \code{sanitize.text.function=function(x){x}}. + From version 1.4-3, all non-numeric columns are sanitized, and all + LaTeX special characters are sanitised for LaTeX output. See Section + 3 of the \code{xtableGallery} vignette for an example of customising + the sanitization. From version 1.4-4, the sanitization also applies to + column names. To remove any text sanitization, specify + \code{sanitize.text.function=function(x){x}}. - From version 1.6-1 the default values for the arguments other than \code{x} are obtainined using \code{getOption()}. Hence the user can set the values once with \code{options()} rather than setting them in every call to \code{print.xtable()}. + From version 1.6-1 the default values for the arguments other than + \code{x} are obtainined using \code{getOption()}. Hence the user can + set the values once with \code{options()} rather than setting them in + every call to \code{print.xtable()}. +} +\author{ + David Dahl \email{dahl@stat.tamu.edu} with contributions and + suggestions from many others (see source code). } -\author{David Dahl \email{dahl@stat.tamu.edu} with contributions and suggestions from many others (see source code).} \references{ - Fairbairns, Robin (2005) \emph{Tables longer than a single page} The UK List of TeX Frequently Asked Questions on the Web. \url{http://www.tex.ac.uk/cgi-bin/texfaq2html?label=longtab} + Fairbairns, Robin (2005) \emph{Tables longer than a single page} The + UK List of TeX Frequently Asked Questions on the + Web. \url{http://www.tex.ac.uk/cgi-bin/texfaq2html?label=longtab} +} +\seealso{ + \code{\link{xtable}}, \code{\link{caption}}, \code{\link{label}}, + \code{\link{align}}, \code{\link{digits}}, \code{\link{display}}, + \code{\link{formatC}} } -\seealso{\code{\link{xtable}}, \code{\link{caption}}, \code{\link{label}}, - \code{\link{align}}, \code{\link{digits}}, \code{\link{display}}, \code{\link{formatC}}} \keyword{print} diff --git a/pkg/man/xtable.Rd b/pkg/man/xtable.Rd index b8a7f99..52d91b9 100644 --- a/pkg/man/xtable.Rd +++ b/pkg/man/xtable.Rd @@ -18,134 +18,152 @@ \alias{xtable.table} \alias{xtable.zoo} \title{Create Export Tables} -\description{Function converting an R object to an \code{xtable} object, which can then be printed as a LaTeX or HTML table.} +\description{ + Function converting an R object to an \code{xtable} object, which can + then be printed as a LaTeX or HTML table. +} \usage{ -xtable(x, caption=NULL, label=NULL, align=NULL, digits=NULL, - display=NULL, ...) +xtable(x, caption = NULL, label = NULL, align = NULL, digits = NULL, + display = NULL, ...) } \arguments{ - \item{x}{An R object of class found among \code{methods(xtable)}. See below on how to write additional method functions - for \code{xtable}.} - \item{caption}{Character vector of length 1 or 2 containing the table's caption or title. If length 2, the second item - is the "short caption" used when LaTeX generates a "List of Tables". - Set to \code{NULL} to suppress the caption. Default value is \code{NULL}.} - \item{label}{Character vector of length 1 containing the LaTeX label or HTML anchor. - Set to \code{NULL} to suppress the label. Default value is \code{NULL}.} - \item{align}{Character vector of length equal to the number of columns of the resulting - table indicating the alignment of the corresponding columns. Also, \code{"|"} may be used - to produce vertical lines between columns in LaTeX tables, but these are effectively ignored - when considering the required length of the supplied vector. If a character vector of length one - is supplied, it is split as \code{strsplit(align,"")[[1]]} before processing. - Since the row names are printed in the first column, the length of \code{align} - is one greater than \code{ncol(x)} if \code{x} is a \code{data.frame}. - Use \code{"l"}, \code{"r"}, and \code{"c"} to denote left, right, and - center alignment, respectively. Use \code{"p\{3cm\}"} etc for a LaTeX column of the specified width. - For HTML output the \code{"p"} alignment is interpreted as \code{"l"}, ignoring - the width request. Default depends on the class of \code{x}.} - \item{digits}{Numeric vector of length equal to one (in which case it will be replicated as necessary) - or to the number of columns of the resulting - table \bold{or} matrix of the same size as the resulting table - indicating the number of digits to display in the corresponding columns. - Since the row names are printed in the first column, the length of the - vector \code{digits} or the number of columns of the matrix \code{digits} - is one greater than \code{ncol(x)} if \code{x} is a \code{data.frame}. - Default depends of class of \code{x}. - If values of \code{digits} are negative, the corresponding values - of \code{x} are displayed in scientific format with \code{abs(digits)} - digits.} - \item{display}{Character vector of length equal to the number of columns of the resulting - table indicating the format for the corresponding columns. - Since the row names are printed in the first column, the length of \code{display} - is one greater than \code{ncol(x)} if \code{x} is a \code{data.frame}. - These values are passed to the \code{formatC} function. Use \code{"d"} (for integers), - \code{"f"}, \code{"e"}, \code{"E"}, \code{"g"}, \code{"G"}, \code{"fg"} (for - reals), or \code{"s"} (for strings). - \code{"f"} gives numbers in the usual \code{xxx.xxx} format; \code{"e"} and - \code{"E"} give \code{n.ddde+nn} or \code{n.dddE+nn} (scientific format); - \code{"g"} and \code{"G"} put \code{x[i]} into scientific format only if it saves - space to do so. \code{"fg"} uses fixed format as \code{"f"}, 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{x}{An R object of class found among \code{methods(xtable)}. See + below on how to write additional method functions for \code{xtable}.} + \item{caption}{Character vector of length 1 or 2 containing the + table's caption or title. If length 2, the second item is the + "short caption" used when LaTeX generates a "List of Tables". Set to + \code{NULL} to suppress the caption. Default value is \code{NULL}. } + \item{label}{Character vector of length 1 containing the LaTeX label + or HTML anchor. Set to \code{NULL} to suppress the label. Default + value is \code{NULL}. } + \item{align}{Character vector of length equal to the number of columns + of the resulting table indicating the alignment of the corresponding + columns. Also, \code{"|"} may be used to produce vertical lines + between columns in LaTeX tables, but these are effectively ignored + when considering the required length of the supplied vector. If a + character vector of length one is supplied, it is split as + \code{strsplit(align, "")[[1]]} before processing. Since the row + names are printed in the first column, the length of \code{align} is + one greater than \code{ncol(x)} if \code{x} is a + \code{data.frame}. Use \code{"l"}, \code{"r"}, and \code{"c"} to + denote left, right, and center alignment, respectively. Use + \code{"p\{3cm\}"} etc for a LaTeX column of the specified width. For + HTML output the \code{"p"} alignment is interpreted as \code{"l"}, + ignoring the width request. Default depends on the class of + \code{x}. } + \item{digits}{ + Numeric vector of length equal to one (in which case it will be + replicated as necessary) or to the number of columns of the + resulting table \bold{or} matrix of the same size as the resulting + table indicating the number of digits to display in the + corresponding columns. Since the row names are printed in the first + column, the length of the vector \code{digits} or the number of + columns of the matrix \code{digits} is one greater than + \code{ncol(x)} if \code{x} is a \code{data.frame}. Default depends + of class of \code{x}. If values of \code{digits} are negative, the + corresponding values of \code{x} are displayed in scientific format + with \code{abs(digits)} digits.} + \item{display}{ + Character vector of length equal to the number of columns of the + resulting table indicating the format for the corresponding columns. + Since the row names are printed in the first column, the length of + \code{display} is one greater than \code{ncol(x)} if \code{x} is a + \code{data.frame}. These values are passed to the \code{formatC} + function. Use \code{"d"} (for integers), \code{"f"}, \code{"e"}, + \code{"E"}, \code{"g"}, \code{"G"}, \code{"fg"} (for reals), or + \code{"s"} (for strings). \code{"f"} gives numbers in the usual + \code{xxx.xxx} format; \code{"e"} and \code{"E"} give + \code{n.ddde+nn} or \code{n.dddE+nn} (scientific format); \code{"g"} + and \code{"G"} put \code{x[i]} into scientific format only if it + saves space to do so. \code{"fg"} uses fixed format as \code{"f"}, + 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{...}{Additional arguments. (Currently ignored.)} } \details{ - This function extracts tabular information from \code{x} and returns an object of class \code{"xtable"}. - The nature of the table generated depends on the class of \code{x}. - For example, \code{aov} objects produce - ANOVA tables while \code{data.frame} objects produce a table of the entire data.frame. One can optionally provide a - caption (called a title in HTML) or label (called an anchor in HTML), - as well as formatting specifications. Default - values for \code{align}, \code{digits}, and \code{display} are - class dependent. + + This function extracts tabular information from \code{x} and returns + an object of class \code{"xtable"}. The nature of the table generated + depends on the class of \code{x}. For example, \code{aov} objects + produce ANOVA tables while \code{data.frame} objects produce a table + of the entire data.frame. One can optionally provide a caption + (called a title in HTML) or label (called an anchor in HTML), as well + as formatting specifications. Default values for \code{align}, + \code{digits}, and \code{display} are class dependent. - The available method functions for \code{xtable} are given by \code{methods(xtable)}. - Users can extend the list of available classes by writing methods for the generic function \code{xtable}. + The available method functions for \code{xtable} are given by + \code{methods(xtable)}. Users can extend the list of available + classes by writing methods for the generic function \code{xtable}. These methods functions should have \code{x} as their first argument - with additional arguments to - specify \code{caption}, \code{label}, \code{align}, - \code{digits}, and - \code{display}. Optionally, other arguments - may be present to specify how the object \code{x} should be manipulated. - All method functions should return an object whose class if given by \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. + with additional arguments to specify \code{caption}, \code{label}, + \code{align}, \code{digits}, and \code{display}. Optionally, other + arguments may be present to specify how the object \code{x} should be + manipulated. All method functions should return an object whose class + if given by \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. + } + \value{An object of class \code{"xtable"} which inherits the + \code{data.frame} class and contains several additional attributes + specifying the table formatting options. } -\value{ - An object of class \code{"xtable"} which inherits the \code{data.frame} class and contains several additional attributes - specifying the table formatting options. +\author{David Dahl \email{dahl@stat.tamu.edu} with contributions and + suggestions from many others (see source code). +} +\seealso{\code{\link{print.xtable}}, \code{\link{caption}}, + \code{\link{label}}, \code{\link{align}}, \code{\link{digits}}, + \code{\link{display}}, \code{\link{formatC}}, \code{\link{methods}} } -\author{David Dahl \email{dahl@stat.tamu.edu} with contributions and suggestions from many others (see source code).} -\seealso{\code{\link{print.xtable}}, \code{\link{caption}}, \code{\link{label}}, - \code{\link{align}}, \code{\link{digits}}, \code{\link{display}}, \code{\link{formatC}}, \code{\link{methods}}} \examples{ ## Load example dataset data(tli) ## Demonstrate data.frame -tli.table <- xtable(tli[1:20,]) -digits(tli.table)[c(2,6)] <- 0 +tli.table <- xtable(tli[1:20, ]) +digits(tli.table)[c(2, 6)] <- 0 print(tli.table) -print(tli.table,type="html") +print(tli.table, type = "html") ## Demonstrate data.frame with different digits in cells -tli.table <- xtable(tli[1:20,]) +tli.table <- xtable(tli[1:20, ]) digits(tli.table) <- matrix( 0:4, nrow = 20, ncol = ncol(tli)+1 ) print(tli.table) -print(tli.table,type="html") +print(tli.table, type = "html") ## Demonstrate matrix -design.matrix <- model.matrix(~ sex*grade, data=tli[1:20,]) +design.matrix <- model.matrix(~ sex*grade, data = tli[1:20, ]) design.table <- xtable(design.matrix) print(design.table) -print(design.table,type="html") +print(design.table, type = "html") ## Demonstrate aov -fm1 <- aov(tlimth ~ sex + ethnicty + grade + disadvg, data=tli) +fm1 <- aov(tlimth ~ sex + ethnicty + grade + disadvg, data = tli) fm1.table <- xtable(fm1) print(fm1.table) -print(fm1.table,type="html") +print(fm1.table, type = "html") ## Demonstrate lm -fm2 <- lm(tlimth ~ sex*ethnicty, data=tli) +fm2 <- lm(tlimth ~ sex*ethnicty, data = tli) fm2.table <- xtable(fm2) print(fm2.table) -print(fm2.table,type="html") +print(fm2.table, type = "html") print(xtable(anova(fm2))) -print(xtable(anova(fm2)),type="html") -fm2b <- lm(tlimth ~ ethnicty, data=tli) -print(xtable(anova(fm2b,fm2))) -print(xtable(anova(fm2b,fm2)),type="html") +print(xtable(anova(fm2)), type = "html") +fm2b <- lm(tlimth ~ ethnicty, data = tli) +print(xtable(anova(fm2b, fm2))) +print(xtable(anova(fm2b, fm2)), type = "html") ## Demonstrate glm -fm3 <- glm(disadvg ~ ethnicty*grade, data=tli, family=binomial()) +fm3 <- glm(disadvg ~ ethnicty*grade, data = tli, family = binomial()) fm3.table <- xtable(fm3) print(fm3.table) -print(fm3.table,type="html") +print(fm3.table, type = "html") print(xtable(anova(fm3))) -print(xtable(anova(fm3)),type="html") +print(xtable(anova(fm3)), type = "html") ## Demonstrate aov ## Taken from help(aov) in R 1.1.1 @@ -155,9 +173,10 @@ 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) 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) 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, 62.0,48.8,45.5,44.2,52.0,51.5,49.8,48.8,57.2,59.0,53.2,56.0) -npk <- data.frame(block=gl(6,4), N=factor(N), P=factor(P), K=factor(K), yield=yield) +npk <- data.frame(block = gl(6,4), N = factor(N), P = factor(P), + K = factor(K), yield = yield) npk.aov <- aov(yield ~ block + N*P*K, npk) -op <- options(contrasts=c("contr.helmert", "contr.treatment")) +op <- options(contrasts = c("contr.helmert", "contr.treatment")) npk.aovE <- aov(yield ~ N*P*K + Error(block), npk) options(op) @@ -167,8 +186,8 @@ print(xtable(anova(npk.aov))) print(xtable(summary(npk.aov))) summary(npk.aovE) -print(xtable(npk.aovE),type="html") -print(xtable(summary(npk.aovE)),type="html") +print(xtable(npk.aovE), type = "html") +print(xtable(summary(npk.aovE)), type = "html") ## Demonstrate lm ## Taken from help(lm) in R 1.1.1 @@ -176,7 +195,7 @@ print(xtable(summary(npk.aovE)),type="html") ## Page 9: Plant Weight Data. ctl <- c(4.17,5.58,5.18,6.11,4.50,4.61,5.17,4.53,5.33,5.14) trt <- c(4.81,4.17,4.41,3.59,5.87,3.83,6.03,4.89,4.32,4.69) -group <- gl(2,10,20, labels=c("Ctl","Trt")) +group <- gl(2,10,20, labels = c("Ctl","Trt")) weight <- c(ctl, trt) lm.D9 <- lm(weight ~ group) print(xtable(lm.D9)) @@ -190,34 +209,35 @@ counts <- c(18,17,15,20,10,20,25,13,12) outcome <- gl(3,1,9) treatment <- gl(3,3) d.AD <- data.frame(treatment, outcome, counts) -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") +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 = ",")) +print(fm1.table, format.args = list(big.mark = "'", decimal.mark = ",")) ## Demonstration of "short caption" support. -fm1sc <- aov(tlimth ~ sex + ethnicty + grade, data=tli) +fm1sc <- aov(tlimth ~ sex + ethnicty + grade, data = tli) fm1sc.table <- xtable(fm1sc, - caption=c("ANOVA Model with Predictors Sex, Ethnicity, and Grade", + caption = c("ANOVA Model with Predictors Sex, Ethnicity, and Grade", "ANOVA: Sex, Ethnicity, Grade")) print(fm1sc.table) ## Demonstration of longtable support. ## Remember to insert \usepackage{longtable} on your LaTeX preamble x <- matrix(rnorm(1000), ncol = 10) -x.big <- xtable(x,label='tabbig',caption='Example of longtable spanning several pages') -print(x.big,tabular.environment='longtable',floating=FALSE) -x <- x[1:30,] -x.small <- xtable(x,label='tabsmall',caption='regular table env') +x.big <- xtable(x, label = 'tabbig', + caption = 'Example of longtable spanning several pages') +print(x.big, tabular.environment = 'longtable', floating = FALSE) +x <- x[1:30, ] +x.small <- xtable(x, label = 'tabsmall', caption = 'regular table env') print(x.small) # default, no longtable ## Demonstration of sidewaystable support. ## Remember to insert \usepackage{rotating} on your LaTeX preamble -print(x.small,floating.environment='sidewaystable') +print(x.small, floating.environment = 'sidewaystable') -if(require(stats,quietly=TRUE)) { +if(require(stats, quietly = TRUE)) { ## Demonstrate prcomp ## Taken from help(prcomp) in mva package of R 1.1.1 data(USArrests) @@ -234,7 +254,7 @@ if(require(stats,quietly=TRUE)) { ## Demonstrate include.rownames, include.colnames, ## only.contents and add.to.row arguments set.seed(2345) -res <- matrix(sample(0:9, size=6*9, replace=TRUE), ncol=6, nrow=9) +res <- matrix(sample(0:9, size = 6*9, replace = TRUE), ncol = 6, nrow = 9) xres <- xtable(res) digits(xres) <- rep(0, 7) addtorow <- list() @@ -242,28 +262,29 @@ addtorow$pos <- list() addtorow$pos[[1]] <- c(0, 2) addtorow$pos[[2]] <- 4 addtorow$command <- c('\\vspace{2mm} \n', '\\vspace{10mm} \n') -print(xres, add.to.row=addtorow, include.rownames=FALSE, include.colnames=TRUE, - only.contents=TRUE, hline.after=c(0, 0, 9, 9)) +print(xres, add.to.row = addtorow, include.rownames = FALSE, + include.colnames = TRUE, only.contents = TRUE, + hline.after = c(0, 0, 9, 9)) -## Demostrate include.rownames, include.colnames, +## Demonstrate include.rownames, include.colnames, ## only.contents and add.to.row arguments in Rweave files \dontrun{ \begin{small} \setlongtables % For longtable version 3.x or less \begin{longtable}{ - <>= - cat(paste(c('c', rep('cc', 34/2-1), 'c'), collapse='@{\\hspace{2pt}}')) + <>= + cat(paste(c('c', rep('cc', 34/2-1), 'c'), collapse = '@{\\hspace{2pt}}')) @ } \hline \endhead \hline \endfoot - <>= + <>= library(xtable) set.seed(2345) - res <- matrix(sample(0:9, size=34*90, replace=TRUE), ncol=34, nrow=90) + res <- matrix(sample(0:9, size = 34*90, replace = TRUE), ncol = 34, nrow = 90) xres <- xtable(res) digits(xres) <- rep(0, 35) addtorow <- list() @@ -271,8 +292,8 @@ print(xres, add.to.row=addtorow, include.rownames=FALSE, include.colnames=TRUE, addtorow$pos[[1]] <- c(seq(4, 40, 5), seq(49, 85, 5)) addtorow$pos[[2]] <- 45 addtorow$command <- c('\\vspace{2mm} \n', '\\newpage \n') - print(xres, add.to.row=addtorow, include.rownames=FALSE, include.colnames=FALSE, - only.contents=TRUE, hline.after=NULL) + print(xres, add.to.row = addtorow, include.rownames = FALSE, + include.colnames = FALSE, only.contents = TRUE, hline.after = NULL) @ \end{longtable} \end{small} @@ -282,65 +303,80 @@ print(xres, add.to.row=addtorow, include.rownames=FALSE, include.colnames=TRUE, mat <- round(matrix(c(0.9, 0.89, 200, 0.045, 2.0), c(1, 5)), 4) rownames(mat) <- "$y_{t-1}$" colnames(mat) <- c("$R^2$", "$\\\bar{R}^2$", "F-stat", "S.E.E", "DW") -print(xtable(mat), type="latex", sanitize.text.function = function(x){x}) +print(xtable(mat), type = "latex", sanitize.text.function = function(x){x}) ## Demonstrate booktabs print(tli.table) -print(tli.table , hline.after=c(-1,0)) -print(tli.table , hline.after=NULL) -print(tli.table , add.to.row=list(pos=list(2), command=c("\\vspace{2mm} \n"))) +print(tli.table, hline.after = c(-1,0)) +print(tli.table, hline.after = NULL) +print(tli.table, + add.to.row = list(pos = list(2), command = c("\\vspace{2mm} \n"))) -print(tli.table , booktabs=TRUE) -print(tli.table , booktabs=TRUE, hline.after=c(-1,0)) -print(tli.table , booktabs=TRUE, hline.after=NULL) -print(tli.table , booktabs=TRUE, - add.to.row=list(pos=list(2), command=c("\\vspace{2mm} \n"))) -print(tli.table , booktabs=TRUE, add.to.row=list(pos=list(2), - command=c("youhou\n")),tabular.environment = "longtable") +print(tli.table, booktabs = TRUE) +print(tli.table, booktabs = TRUE, hline.after = c(-1,0)) +print(tli.table, booktabs = TRUE, hline.after = NULL) +print(tli.table, booktabs = TRUE, + add.to.row = list(pos = list(2), command = c("\\vspace{2mm} \n"))) +print(tli.table, booktabs = TRUE, add.to.row = list(pos = list(2), + command = c("youhou\n")), tabular.environment = "longtable") \testonly{ for(i in c("latex","html")) { - outFileName <- paste("xtable.",ifelse(i=="latex","tex",i),sep="") - print(tli.table,type=i,file=outFileName,append=FALSE) - print(design.table,type=i,file=outFileName,append=TRUE) - print(fm1.table,type=i,file=outFileName,append=TRUE) - print(fm2.table,type=i,file=outFileName,append=TRUE) - print(fm2.table,type=i,file=outFileName,append=TRUE,math.style.negative=TRUE) - print(xtable(anova(fm2)),type=i,file=outFileName,append=TRUE) - print(xtable(anova(fm2b,fm2)),type=i,file=outFileName,append=TRUE) - print(fm3.table,type=i,file=outFileName,append=TRUE) - print(xtable(anova(fm3)),type=i,file=outFileName,append=TRUE) - print(xtable(npk.aov),type=i,file=outFileName,append=TRUE) - print(xtable(anova(npk.aov)),type=i,file=outFileName,append=TRUE) - print(xtable(summary(npk.aov)),type=i,file=outFileName,append=TRUE) - print(xtable(npk.aovE),type=i,file=outFileName,append=TRUE) - print(xtable(summary(npk.aovE)),type=i,file=outFileName,append=TRUE) - if(i=="latex") cat("\\\clearpage\n",file=outFileName,append=TRUE) - print(xtable(lm.D9),type=i,file=outFileName,append=TRUE,latex.environment=NULL) - print(xtable(lm.D9),type=i,file=outFileName,append=TRUE,latex.environment="") - print(xtable(lm.D9),type=i,file=outFileName,append=TRUE,latex.environment="center") - print(xtable(anova(lm.D9)),type=i,file=outFileName,append=TRUE) - print(xtable(glm.D93),type=i,file=outFileName,append=TRUE) - print(xtable(anova(glm.D93,test="Chisq")),type=i,file=outFileName,append=TRUE) - print(xtable(glm.D93,align="r|llrc"),include.rownames=FALSE,include.colnames=TRUE, - type=i,file=outFileName,append=TRUE) - print(xtable(glm.D93,align="r||llrc"),include.rownames=TRUE,include.colnames=FALSE, - type=i,file=outFileName,append=TRUE) - print(xtable(glm.D93,align="|r||llrc"),include.rownames=FALSE,include.colnames=FALSE, - type=i,file=outFileName,append=TRUE) - print(xtable(glm.D93,align="|r||llrc|"),type=i,file=outFileName,append=TRUE) - print(xtable(anova(glm.D93)),hline.after=c(1),size="small",type=i,file=outFileName, append=TRUE) - if(require(stats,quietly=TRUE)) { - print(xtable(pr1),type=i,file=outFileName,append=TRUE) - print(xtable(summary(pr1)),type=i,file=outFileName,append=TRUE) - # print(xtable(pr2),type=i,file=outFileName,append=TRUE) + outFileName <- paste("xtable.", ifelse(i=="latex", "tex", i), sep = "") + print(tli.table, type = i, file = outFileName, append = FALSE) + print(design.table, type = i, file = outFileName, append = TRUE) + print(fm1.table, type = i, file = outFileName, append = TRUE) + print(fm2.table, type = i, file = outFileName, append = TRUE) + print(fm2.table, type = i, file = outFileName, append = TRUE, + math.style.negative = TRUE) + print(xtable(anova(fm2)), type = i, file = outFileName, append = TRUE) + print(xtable(anova(fm2b, fm2)), type = i, file = outFileName, append = TRUE) + print(fm3.table, type = i, file = outFileName, append = TRUE) + print(xtable(anova(fm3)), type = i, file = outFileName, append = TRUE) + print(xtable(npk.aov), type = i, file = outFileName, append = TRUE) + print(xtable(anova(npk.aov)), type = i, file = outFileName, append = TRUE) + print(xtable(summary(npk.aov)), type = i, file = outFileName, append = TRUE) + print(xtable(npk.aovE), type = i, file = outFileName, append = TRUE) + print(xtable(summary(npk.aovE)), + type = i, file = outFileName, append = TRUE) + if(i=="latex") cat("\\\clearpage\n", file = outFileName, append = TRUE) + print(xtable(lm.D9), + type = i, file = outFileName, append = TRUE, latex.environment = NULL) + print(xtable(lm.D9), + type = i, file = outFileName, append = TRUE, latex.environment = "") + print(xtable(lm.D9), + type = i, file = outFileName, append = TRUE, + latex.environment = "center") + print(xtable(anova(lm.D9)), type = i, file = outFileName, append = TRUE) + print(xtable(glm.D93), type = i, file = outFileName, append = TRUE) + print(xtable(anova(glm.D93, test = "Chisq")), + type = i, file = outFileName, append = TRUE) + print(xtable(glm.D93, align = "r|llrc"), + include.rownames = FALSE, include.colnames = TRUE, + type = i, file = outFileName, append = TRUE) + print(xtable(glm.D93, align = "r||llrc"), + include.rownames = TRUE, include.colnames = FALSE, + type = i, file = outFileName, append = TRUE) + print(xtable(glm.D93, align = "|r||llrc"), + include.rownames = FALSE, include.colnames = FALSE, + type = i, file = outFileName, append = TRUE) + print(xtable(glm.D93, align = "|r||llrc|"), + type = i, file = outFileName, append = TRUE) + print(xtable(anova(glm.D93)), + hline.after = c(1), size = "small", + type = i, file = outFileName, append = TRUE) + if(require(stats, quietly = TRUE)) { + print(xtable(pr1), type = i, file = outFileName, append = TRUE) + print(xtable(summary(pr1)), type = i, file = outFileName, append = TRUE) + # print(xtable(pr2), type = i, file = outFileName, append = TRUE) } - temp.table <- xtable(ts(cumsum(1+round(rnorm(100), 2)), start = c(1954, 7), frequency=12)) + temp.table <- xtable(ts(cumsum(1+round(rnorm(100), 2)), + start = c(1954, 7), frequency = 12)) caption(temp.table) <- "Time series example" - print(temp.table,type=i,file=outFileName,append=TRUE,caption.placement="top", - table.placement="h") - print(temp.table,type=i,file=outFileName,append=TRUE,caption.placement="bottom", - table.placement="htb") + print(temp.table, type = i, file = outFileName, + append = TRUE, caption.placement = "top", table.placement = "h") + print(temp.table, type = i, file = outFileName, + append = TRUE, caption.placement = "bottom", table.placement = "htb") } } -- 2.39.2