From 84179ae4bb7a2cc86b611891c857b1fc0d46f03b Mon Sep 17 00:00:00 2001 From: dscott Date: Fri, 15 Jan 2016 12:23:43 +0000 Subject: [PATCH 1/1] Working version of xtable with support for flat tables git-svn-id: svn://scm.r-forge.r-project.org/svnroot/xtable@96 edb9625f-4e0d-4859-8d74-9fd3b1da38cb --- pkg/R/xtableFtable.R | 24 +++---- pkg/man/xtable.Rd | 4 +- pkg/tests/test.xtable.xtableFtable.R | 12 ++-- pkg/vignettes/xtableGallery.Rnw | 99 ++++++++++++++++++++-------- 4 files changed, 92 insertions(+), 47 deletions(-) diff --git a/pkg/R/xtableFtable.R b/pkg/R/xtableFtable.R index 22d721a..1e9d72a 100644 --- a/pkg/R/xtableFtable.R +++ b/pkg/R/xtableFtable.R @@ -2,7 +2,7 @@ ### Feature request #2248, 2/9/2012 xtableFtable <- function(x, caption = NULL, label = NULL, align = NULL, digits = NULL, display = NULL, - quote = TRUE, + quote = FALSE, method = c("non.compact", "row.compact", "col.compact", "compact"), lsep = " | ", ...) { @@ -26,27 +26,29 @@ xtableFtable <- function(x, caption = NULL, label = NULL, align = NULL, } } if (method == "non.compact"){ - nCharCols <- nRowVars + 1 + nCharCols <- nRowVars + 2 nCharRows <- nColVars + 1 } if (method == "row.compact"){ - nCharCols <- nRowVars + 1 + nCharCols <- nRowVars + 2 nCharRows <- nColVars } if (method == "col.compact"){ - nCharCols <- nRowVars + nCharCols <- nRowVars + 1 nCharRows <- nColVars + 1 } if (method == "compact"){ - nCharCols <- nRowVars + nCharCols <- nRowVars + 1 nCharRows <- nColVars } - - if(is.null(align)) align <- c(rep("l", nCharCols), rep("r", xDim[2])) + + if(is.null(align)) { + align <- c(rep("l", nCharCols - 1), "l |", rep("r", xDim[2])) + } if(is.null(display)) { display <- c(rep("s", nCharCols), rep("d", xDim[2])) } - + attr(x, "ftableCaption") <- caption attr(x, "ftableLabel") <- label attr(x, "ftableAlign") <- align @@ -95,9 +97,9 @@ print.xtableFtable <- function(x, ...) { if (type == "latex"){ caption <- attr(x, "ftableCaption") - label <- attr(x, "ftableLabel") + label <- attr(x, "ftableLabel") align <- attr(x, "ftableAlign") - digits <- attr(x, "ftableDigits") + digits <- attr(x, "ftableDigits") quote <- attr(x, "quote") digits <- attr(x, "ftabelDigits") method <- attr(x, "method") @@ -110,7 +112,7 @@ print.xtableFtable <- function(x, attr(fmtFtbl, "align") <- align attr(fmtFtbl, "digits") <- digits attr(fmtFtbl, "quote") <- quote - attr(fmtFtbl, "display") <- display + attr(fmtFtbl, "display") <- display print.xtable(fmtFtbl, hline.after = c(-1, nCharRows, dim(fmtFtbl)[1]), include.rownames = FALSE, include.colnames = FALSE) } else { diff --git a/pkg/man/xtable.Rd b/pkg/man/xtable.Rd index 36caca2..f632573 100644 --- a/pkg/man/xtable.Rd +++ b/pkg/man/xtable.Rd @@ -136,12 +136,12 @@ xtable(x, caption = NULL, label = NULL, align = NULL, digits = NULL, \author{David Dahl \email{dahl@stat.byu.edu} with contributions and suggestions from many others (see source code). } -\seealso{ +\seealso{ \code{\link{print.xtable}}, \code{\link{caption}}, \code{\link{label}}, \code{\link{align}}, \code{\link{digits}}, \code{\link{display}}, \code{\link{autoformat}}, \code{\link{xalign}}, \code{\link{xdigits}}, \code{\link{xdisplay}}, - \code{\link{xtableMatharray}}, \codel{\link{xtableList}} + \code{\link{xtableMatharray}}, \code{\link{xtableList}} } \examples{ diff --git a/pkg/tests/test.xtable.xtableFtable.R b/pkg/tests/test.xtable.xtableFtable.R index 473f0e7..5ac399f 100644 --- a/pkg/tests/test.xtable.xtableFtable.R +++ b/pkg/tests/test.xtable.xtableFtable.R @@ -6,9 +6,7 @@ library(xtable) tbl <- ftable(mtcars$cyl, mtcars$vs, mtcars$am, mtcars$gear, row.vars = c(2, 4), dnn = c("Cylinders", "V/S", "Transmission", "Gears")) -source("../R/xtableFtable.R") - -debug(xtableFtable) +## debug(xtableFtable) xftbl <- xtableFtable(tbl) str(xftbl) unclass(xftbl) @@ -19,8 +17,8 @@ xftbl <- xtableFtable(tbl, method = "col.compact") print.xtableFtable(xftbl) xftbl <- xtableFtable(tbl, method = "compact") print.xtableFtable(xftbl) -debug(print.xtableFtable) -undebug(print.xtableFtable) -debug(print.xtable) -undebug(print.xtable) +## debug(print.xtableFtable) +## undebug(print.xtableFtable) +## debug(print.xtable) +## undebug(print.xtable) diff --git a/pkg/vignettes/xtableGallery.Rnw b/pkg/vignettes/xtableGallery.Rnw index 1a5f72d..b5aa2a0 100644 --- a/pkg/vignettes/xtableGallery.Rnw +++ b/pkg/vignettes/xtableGallery.Rnw @@ -123,6 +123,47 @@ temp.table <- xtable(temp.ts, digits = 0) temp.table @ +\subsection{Flat tables} +\label{sec:flat-tables} + +See the \textbf{Details} section of the help for \code{ftable} for a +description of these tables, which are flat versions of +multi-dimensional contingency tables. They require special methods to +enable them to be printed using \pkg{xtable} + + +<>= +data(mtcars) +tbl <- ftable(mtcars$cyl, mtcars$vs, mtcars$am, mtcars$gear, + row.vars = c(2, 4), + dnn = c("Cylinders", "V/S", "Transmission", "Gears")) + +@ %def + +\p +<>= +xftbl <- xtableFtable(tbl) +print.xtableFtable(xftbl) +@ %def + +\p +<>= +xftbl <- xtableFtable(tbl, method = "row.compact") +print.xtableFtable(xftbl) +@ %def + +\p +<>= +xftbl <- xtableFtable(tbl, method = "col.compact") +print.xtableFtable(xftbl) +@ %def + +\p +<>= +xftbl <- xtableFtable(tbl, method = "compact") +print.xtableFtable(xftbl) +@ %def + \newpage <>= @@ -147,9 +188,9 @@ temp.table \section{Automatic formatting} \subsection{Suggest alignment, digits, and display} -The functions \code{xalign}, \code{xdigits}, and \code{xdisplay} are useful for -formatting tables in a sensible way. Consider the output produced by the default -formatting. +The functions \code{xalign}, \code{xdigits}, and \code{xdisplay} are +useful for formatting tables in a sensible way. Consider the output +produced by the default formatting. <>= dat <- mtcars[1:3, 1:6] @@ -200,7 +241,7 @@ print(xtable(data.frame(text = c("foo","bar"), row.names = c("A","B")), display = c("s","s","g","g")), math.style.exponents = TRUE) -@ +@ this option also supports the values \code{ensuremath} which uses \code{\char`\\ensuremath} instead of \code{\$\$} and \code{UTF-8} @@ -255,8 +296,8 @@ print(xtable(dat), \newpage \subsection{Markup in tables} -Markup can be included in tables, including in column and row names, by using -a custom \code{sanitize.text.function}. +Markup can be included in tables, including in column and row names, +by using a custom \code{sanitize.text.function}. <>= mat <- round(matrix(c(0.9, 0.89, 200, 0.045, 2.0), c(1, 5)), 4) @@ -379,9 +420,9 @@ print(tli.table, include.colnames = FALSE, include.rownames = FALSE) \newpage \subsection{Rotate row/column names} -The \code{rotate.rownames} and \code{rotate.colnames} arguments can be used to -rotate the row and/or column names. This requires \verb|\usepackage{rotating}| -in the \LaTeX\ preamble. +The \code{rotate.rownames} and \code{rotate.colnames} arguments can be +used to rotate the row and/or column names. This requires +\verb|\usepackage{rotating}| in the \LaTeX\ preamble. <>= print(tli.table, rotate.rownames = TRUE, rotate.colnames = TRUE) @@ -399,16 +440,17 @@ print(xtable(anova(fm3)), hline.after = c(1)) @ \subsubsection{Line styles} -Specifying \code{booktabs = TRUE} will generate three line types. By default, -when no value is given for \code{hline.after}, a \verb|\toprule| will be drawn -above the table, a \verb|\midrule| after the table headings and a -\verb|\bottomrule| below the table. This requires \verb|\usepackage{booktabs}| -in the \LaTeX\ preamble. +Specifying \code{booktabs = TRUE} will generate three line types. By +default, when no value is given for \code{hline.after}, a +\verb|\toprule| will be drawn above the table, a \verb|\midrule| after +the table headings and a \verb|\bottomrule| below the table. This +requires \verb|\usepackage{booktabs}| in the \LaTeX\ preamble. \p -The top and bottom rules are slightly thicker than the mid rule. The thickness -of the lines can be set via the \LaTeX\ lengths \verb|\heavyrulewidth| and -\verb|\lightrulewidth|. + +The top and bottom rules are slightly thicker than the mid rule. The +thickness of the lines can be set via the \LaTeX\ lengths +\verb|\heavyrulewidth| and \verb|\lightrulewidth|. <>= tli.table <- xtable(tli[1:10, ]) @@ -416,6 +458,7 @@ print(tli.table, include.rownames = FALSE, booktabs = TRUE) @ \p + If \code{hline.after} includes \code{-1}, a \verb|\toprule| will be drawn above the table. If \code{hline.after} includes the number of rows in the table, a \verb|\bottomrule| will be drawn below the @@ -460,7 +503,7 @@ command <- paste0("\\hline\n\\endhead\n", "\\multicolumn{", dim(x)[2] + 1, "}{l}", "{\\footnotesize Continued on next page}\n", "\\endfoot\n", - "\\endlastfoot\n") + "\\endlastfoot\n") add.to.row$command <- command print(x.big, hline.after=c(-1), add.to.row = add.to.row, tabular.environment = "longtable") @@ -547,8 +590,8 @@ column formats. These allow for very sophisticated cell formatting, namely left-aligned, centred, or right-aligned text, with recognition of line breaks for the first three new column types. If these lines are -included along with \verb|\usepackage{array}|, then the following -is possible. +included along with \verb|\usepackage{array}|, then the following is +possible. \newcolumntype{L}[1]{>{\raggedright\let\newline\\ \arraybackslash\hspace{0pt}}m{#1}} @@ -568,10 +611,10 @@ print(xtable(df, align = c("l", "|c", "|R{3cm}", "|L{3cm}", "| p{3cm}|")), \newpage \subsection{Table width} -The \code{tabularx} environment is for typesetting tables whose overall width is -fixed. The column alignment code \code{X} denotes columns that will be stretched -to achieve the desired table width. Requires \verb|\usepackage{tabularx}| in the -\LaTeX\ preamble. +The \code{tabularx} environment is for typesetting tables whose +overall width is fixed. The column alignment code \code{X} denotes +columns that will be stretched to achieve the desired table +width. Requires \verb|\usepackage{tabularx}| in the \LaTeX\ preamble. <>= df.width <- data.frame(One = c("item 1", "A"), Two = c("item 2", "B"), @@ -582,9 +625,10 @@ print(x.width, tabular.environment = "tabularx", width = "\\textwidth") @ \section{Suppressing printing} -By default the \code{print} method will print the \LaTeX\ or HTML to standard -output and also return the character strings invisibly. The printing to -standard output can be suppressed by specifying \code{print.results = FALSE}. +By default the \code{print} method will print the \LaTeX\ or HTML to +standard output and also return the character strings invisibly. The +printing to standard output can be suppressed by specifying +\code{print.results = FALSE}. <<>>= x.out <- print(tli.table, print.results = FALSE) @@ -600,6 +644,7 @@ class(x.ltx) x.ltx @ + \newpage \section{Acknowledgements} -- 2.39.2