]> git.donarmstrong.com Git - xtable.git/commitdiff
Working version of xtable with support for flat tables
authordscott <dscott@edb9625f-4e0d-4859-8d74-9fd3b1da38cb>
Fri, 15 Jan 2016 12:23:43 +0000 (12:23 +0000)
committerdscott <dscott@edb9625f-4e0d-4859-8d74-9fd3b1da38cb>
Fri, 15 Jan 2016 12:23:43 +0000 (12:23 +0000)
git-svn-id: svn://scm.r-forge.r-project.org/svnroot/xtable@96 edb9625f-4e0d-4859-8d74-9fd3b1da38cb

pkg/R/xtableFtable.R
pkg/man/xtable.Rd
pkg/tests/test.xtable.xtableFtable.R
pkg/vignettes/xtableGallery.Rnw

index 22d721a03ca5786abaa0b9d96968d640fb40d204..1e9d72a07245de8c6f5d1a082703bae5a4469203 100644 (file)
@@ -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 {
index 36caca2c7bb55dcedbfa5133744262cc40435aa6..f632573b00c3aad659b49744ffebd2504262d3f5 100644 (file)
@@ -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{
 
index 473f0e73e6dfd5fc211e416745f796b1e2d04586..5ac399f01e7a302180bba21c23e85ccfcbc8cf28 100644 (file)
@@ -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)
 
index 1a5f72d46fac454db33df9f9b128aecf0c51446b..b5aa2a01fd10e1d9a0f5221334ed28799b7384de 100644 (file)
@@ -123,6 +123,47 @@ temp.table <- xtable(temp.ts, digits = 0)
 temp.table\r
 @\r
 \r
+\subsection{Flat tables}\r
+\label{sec:flat-tables}\r
+\r
+See the \textbf{Details} section of the help for \code{ftable} for a\r
+description of these tables, which are flat versions of\r
+multi-dimensional contingency tables. They require special methods to\r
+enable them to be printed using \pkg{xtable}\r
+\r
+\r
+<<ftable>>=\r
+data(mtcars)\r
+tbl <- ftable(mtcars$cyl, mtcars$vs, mtcars$am, mtcars$gear,\r
+              row.vars = c(2, 4),\r
+              dnn = c("Cylinders", "V/S", "Transmission", "Gears"))\r
+\r
+@ %def\r
+\r
+\p\r
+<<ftable1, results = 'asis'>>=\r
+xftbl <- xtableFtable(tbl)\r
+print.xtableFtable(xftbl)\r
+@ %def\r
+\r
+\p\r
+<<ftable2, results = 'asis'>>=\r
+xftbl <- xtableFtable(tbl, method = "row.compact")\r
+print.xtableFtable(xftbl)\r
+@ %def\r
+\r
+\p\r
+<<ftable3, results = 'asis'>>=\r
+xftbl <- xtableFtable(tbl, method = "col.compact")\r
+print.xtableFtable(xftbl)\r
+@ %def\r
+\r
+\p\r
+<<ftable4, results = 'asis'>>=\r
+xftbl <- xtableFtable(tbl, method = "compact")\r
+print.xtableFtable(xftbl)\r
+@ %def\r
+\r
 \newpage\r
 \r
 <<include=FALSE>>=\r
@@ -147,9 +188,9 @@ temp.table
 \r
 \section{Automatic formatting}\r
 \subsection{Suggest alignment, digits, and display}\r
-The functions \code{xalign}, \code{xdigits}, and \code{xdisplay} are useful for\r
-formatting tables in a sensible way. Consider the output produced by the default\r
-formatting.\r
+The functions \code{xalign}, \code{xdigits}, and \code{xdisplay} are\r
+useful for formatting tables in a sensible way. Consider the output\r
+produced by the default formatting.\r
 \r
 <<results='asis'>>=\r
 dat <- mtcars[1:3, 1:6]\r
@@ -200,7 +241,7 @@ print(xtable(data.frame(text = c("foo","bar"),
                         row.names = c("A","B")),\r
              display = c("s","s","g","g")),\r
       math.style.exponents = TRUE)\r
-@ \r
+@\r
 \r
 this option also supports the values \code{ensuremath} which uses\r
 \code{\char`\\ensuremath} instead of \code{\$\$} and \code{UTF-8}\r
@@ -255,8 +296,8 @@ print(xtable(dat),
 \newpage\r
 \r
 \subsection{Markup in tables}\r
-Markup can be included in tables, including in column and row names, by using\r
-a custom \code{sanitize.text.function}.\r
+Markup can be included in tables, including in column and row names,\r
+by using a custom \code{sanitize.text.function}.\r
 \r
 <<results='asis'>>=\r
 mat <- round(matrix(c(0.9, 0.89, 200, 0.045, 2.0), c(1, 5)), 4)\r
@@ -379,9 +420,9 @@ print(tli.table, include.colnames = FALSE, include.rownames = FALSE)
 \newpage\r
 \r
 \subsection{Rotate row/column names}\r
-The \code{rotate.rownames} and \code{rotate.colnames} arguments can be used to\r
-rotate the row and/or column names. This requires \verb|\usepackage{rotating}|\r
-in the \LaTeX\ preamble.\r
+The \code{rotate.rownames} and \code{rotate.colnames} arguments can be\r
+used to rotate the row and/or column names. This requires\r
+\verb|\usepackage{rotating}| in the \LaTeX\ preamble.\r
 \r
 <<results='asis'>>=\r
 print(tli.table, rotate.rownames = TRUE, rotate.colnames = TRUE)\r
@@ -399,16 +440,17 @@ print(xtable(anova(fm3)), hline.after = c(1))
 @\r
 \r
 \subsubsection{Line styles}\r
-Specifying \code{booktabs = TRUE} will generate three line types. By default,\r
-when no value is given for \code{hline.after}, a \verb|\toprule| will be drawn\r
-above the table, a \verb|\midrule| after the table headings and a\r
-\verb|\bottomrule| below the table. This requires \verb|\usepackage{booktabs}|\r
-in the \LaTeX\ preamble.\r
+Specifying \code{booktabs = TRUE} will generate three line types. By\r
+default, when no value is given for \code{hline.after}, a\r
+\verb|\toprule| will be drawn above the table, a \verb|\midrule| after\r
+the table headings and a \verb|\bottomrule| below the table. This\r
+requires \verb|\usepackage{booktabs}| in the \LaTeX\ preamble.\r
 \r
 \p\r
-The top and bottom rules are slightly thicker than the mid rule. The thickness\r
-of the lines can be set via the \LaTeX\ lengths \verb|\heavyrulewidth| and\r
-\verb|\lightrulewidth|.\r
+\r
+The top and bottom rules are slightly thicker than the mid rule. The\r
+thickness of the lines can be set via the \LaTeX\ lengths\r
+\verb|\heavyrulewidth| and \verb|\lightrulewidth|.\r
 \r
 <<results='asis'>>=\r
 tli.table <- xtable(tli[1:10, ])\r
@@ -416,6 +458,7 @@ print(tli.table, include.rownames = FALSE, booktabs = TRUE)
 @\r
 \r
 \p\r
+\r
 If \code{hline.after} includes \code{-1}, a \verb|\toprule| will be\r
 drawn above the table. If \code{hline.after} includes the number of\r
 rows in the table, a \verb|\bottomrule| will be drawn below the\r
@@ -460,7 +503,7 @@ command <- paste0("\\hline\n\\endhead\n",
                   "\\multicolumn{", dim(x)[2] + 1, "}{l}",\r
                   "{\\footnotesize Continued on next page}\n",\r
                   "\\endfoot\n",\r
-                  "\\endlastfoot\n") \r
+                  "\\endlastfoot\n")\r
 add.to.row$command <- command\r
 print(x.big, hline.after=c(-1), add.to.row = add.to.row,\r
       tabular.environment = "longtable")\r
@@ -547,8 +590,8 @@ column formats.
 These allow for very sophisticated cell formatting, namely\r
 left-aligned, centred, or right-aligned text, with recognition of line\r
 breaks for the first three new column types. If these lines are\r
-included along with \verb|\usepackage{array}|, then the following\r
-is possible.\r
+included along with \verb|\usepackage{array}|, then the following is\r
+possible.\r
 \r
 \newcolumntype{L}[1]{>{\raggedright\let\newline\\\r
     \arraybackslash\hspace{0pt}}m{#1}}\r
@@ -568,10 +611,10 @@ print(xtable(df, align = c("l", "|c", "|R{3cm}", "|L{3cm}", "| p{3cm}|")),
 \newpage\r
 \r
 \subsection{Table width}\r
-The \code{tabularx} environment is for typesetting tables whose overall width is\r
-fixed. The column alignment code \code{X} denotes columns that will be stretched\r
-to achieve the desired table width. Requires \verb|\usepackage{tabularx}| in the\r
-\LaTeX\ preamble.\r
+The \code{tabularx} environment is for typesetting tables whose\r
+overall width is fixed. The column alignment code \code{X} denotes\r
+columns that will be stretched to achieve the desired table\r
+width. Requires \verb|\usepackage{tabularx}| in the \LaTeX\ preamble.\r
 \r
 <<results='asis'>>=\r
 df.width <- data.frame(One = c("item 1", "A"), Two = c("item 2", "B"),\r
@@ -582,9 +625,10 @@ print(x.width, tabular.environment = "tabularx", width = "\\textwidth")
 @\r
 \r
 \section{Suppressing printing}\r
-By default the \code{print} method will print the \LaTeX\ or HTML to standard\r
-output and also return the character strings invisibly.  The printing to\r
-standard output can be suppressed by specifying \code{print.results = FALSE}.\r
+By default the \code{print} method will print the \LaTeX\ or HTML to\r
+standard output and also return the character strings invisibly.  The\r
+printing to standard output can be suppressed by specifying\r
+\code{print.results = FALSE}.\r
 \r
 <<>>=\r
 x.out <- print(tli.table, print.results = FALSE)\r
@@ -600,6 +644,7 @@ class(x.ltx)
 x.ltx\r
 @\r
 \r
+\r
 \newpage\r
 \r
 \section{Acknowledgements}\r