]> git.donarmstrong.com Git - xtable.git/blobdiff - pkg/vignettes/xtableGallery.Rnw
Vignette: double quotes, fewer LaTeX packages, explain add.to.row detail
[xtable.git] / pkg / vignettes / xtableGallery.Rnw
index 9fccd7be959c3d290f36f8ae0492dfe3ff844b76..88dd00b8c94d94657c3a749d9598049a9db7908c 100644 (file)
 \usepackage{booktabs}\r
 \usepackage{longtable}\r
 \usepackage{rotating}\r
+\usepackage{array}\r
 \usepackage{tabularx}\r
-\newcommand\code[1]{\texttt{#1}}\r
 \newcommand\p{\vspace{2ex}}\r
 \setcounter{tocdepth}{2}\r
+%% Define \code \proglang and \pkg commands\r
+\newcommand\code[1]{\texttt{#1}}\r
+\newcommand\pkg[1]{\textbf{#1}}\r
+\newcommand\R{\textsf{R}}\r
+%% url and hyperref\r
+\usepackage[hyphens]{url}\r
+\usepackage{hyperref}\r
 \begin{document}\r
 \r
-\title{The xtable Gallery}\r
+\title{The \pkg{xtable} Gallery}\r
 \author{Jonathan Swinton and others}\r
 \maketitle\r
 \r
 \r
 \newpage\r
 \r
+\section{Introduction}\r
+\label{sec:introduction}\r
+\r
+This document gives a gallery of tables which can be made using the\r
+\pkg{xtable} package to create \LaTeX\ output. It doubles as a\r
+regression check for the package.\r
+\r
 <<include=FALSE>>=\r
 library(knitr)\r
 opts_chunk$set(fig.path='figdir/fig', debug=TRUE, echo=TRUE)\r
 @\r
 \r
-\section{Gallery}\r
-\subsection{Data frame}\r
+The first step is to load the package and set an option for this document.\r
 <<results='asis'>>=\r
 library(xtable)\r
 options(xtable.floating = FALSE)\r
+@\r
+\r
+\section{Gallery}\r
+\subsection{Data frame}\r
+<<results='asis'>>=\r
 data(tli)\r
 xtable(tli[1:10, ])\r
 @\r
@@ -44,17 +62,16 @@ xtable(tli[1:10, ])
 \subsection{Matrix}\r
 <<results='asis'>>=\r
 design.matrix <- model.matrix(~ sex*grade, data = tli[1:10, ])\r
-xtable(design.matrix)\r
+xtable(design.matrix, digits = 0)\r
 @\r
 \r
+\newpage\r
 \subsection{aov}\r
 <<results='asis'>>=\r
 fm1 <- aov(tlimth ~ sex + ethnicty + grade + disadvg, data = tli)\r
 xtable(fm1)\r
 @\r
 \r
-\newpage\r
-\r
 \subsection{lm}\r
 <<results='asis'>>=\r
 fm2 <- lm(tlimth ~ sex*ethnicty, data = tli)\r
@@ -72,86 +89,18 @@ fm2b <- lm(tlimth ~ ethnicty, data = tli)
 xtable(anova(fm2b, fm2))\r
 @\r
 \r
+\newpage\r
 \subsection{glm}\r
 <<results='asis'>>=\r
 fm3 <- glm(disadvg ~ ethnicty*grade, data = tli, family = binomial)\r
 xtable(fm3)\r
 @\r
 \r
-\p\r
+\subsubsection{Analysis of deviance}\r
 <<results='asis'>>=\r
 xtable(anova(fm3))\r
 @\r
 \r
-\subsection{More aov}\r
-<<results='asis'>>=\r
-## Venables and Ripley (1997, p. 210)\r
-N <- c(0,1,0,1,1,1,0,0,0,1,1,0,1,1,0,0,1,0,1,0,1,1,0,0)\r
-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)\r
-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)\r
-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,\r
-           62.0,48.8,45.5,44.2,52.0,51.5,49.8,48.8,57.2,59.0,53.2,56.0)\r
-npk <- data.frame(block = gl(6,4), N = factor(N), P = factor(P),\r
-                  K = factor(K), yield = yield)\r
-npk.aov <- aov(yield ~ block + N*P*K, npk)\r
-xtable(npk.aov)\r
-@\r
-\r
-\p\textbf{\itshape Anova table}\r
-<<results='asis'>>=\r
-xtable(anova(npk.aov))\r
-@\r
-\r
-\p\textbf{\itshape Summary table}\r
-<<results='asis'>>=\r
-xtable(summary(npk.aov))\r
-@\r
-\r
-\newpage\r
-\r
-\p\r
-<<results='asis'>>=\r
-## Alternative multistratum model\r
-op <- options(contrasts = c("contr.helmert", "contr.treatment"))\r
-npk.aovE <- aov(yield ~  N*P*K + Error(block), npk)\r
-options(op)\r
-xtable(npk.aovE)\r
-@\r
-\r
-\p\r
-<<results='asis'>>=\r
-xtable(summary(npk.aovE))\r
-@\r
-\r
-\subsection{More lm}\r
-<<results='asis'>>=\r
-## Dobson (1990, p. 9) plant weight data\r
-ctl <- c(4.17,5.58,5.18,6.11,4.50,4.61,5.17,4.53,5.33,5.14)\r
-trt <- c(4.81,4.17,4.41,3.59,5.87,3.83,6.03,4.89,4.32,4.69)\r
-group <- gl(2,10,20, labels = c("Ctl","Trt"))\r
-weight <- c(ctl, trt)\r
-lm.D9 <- lm(weight ~ group)\r
-xtable(lm.D9)\r
-@\r
-\r
-\p\r
-<<results='asis'>>=\r
-xtable(anova(lm.D9))\r
-@\r
-\r
-\newpage\r
-\r
-\subsection{More glm}\r
-<<results='asis'>>=\r
-## Dobson (1990, p. 93) randomized controlled trial\r
-counts <- c(18,17,15,20,10,20,25,13,12)\r
-outcome <- gl(3,1,9)\r
-treatment <- gl(3,3)\r
-d.AD <- data.frame(treatment, outcome, counts)\r
-glm.D93 <- glm(counts ~ outcome + treatment, family = poisson)\r
-xtable(glm.D93, align = "r|llrc")\r
-@\r
-\r
 \subsection{prcomp}\r
 <<results='asis'>>=\r
 pr1 <- prcomp(USArrests)\r
@@ -173,10 +122,11 @@ xtable(summary(pr1))
 temp.ts <- ts(cumsum(1 + round(rnorm(100), 0)),\r
               start = c(1954, 7), frequency = 12)\r
 temp.table <- xtable(temp.ts, digits = 0)\r
-caption(temp.table) <- "Time series example"\r
-print(temp.table, floating = TRUE)\r
+temp.table\r
 @\r
 \r
+\newpage\r
+\r
 <<include=FALSE>>=\r
 # ## Demonstrate saving to file\r
 # for(i in c("latex", "html")) {\r
@@ -238,14 +188,12 @@ xtable(insane)
 Sometimes you might want to have your own sanitization function.\r
 \r
 <<results='asis'>>=\r
-wanttex <- xtable(data.frame(label =\r
+wanttex <- xtable(data.frame(Column =\r
                              paste("Value_is $10^{-",1:3,"}$", sep = "")))\r
 print(wanttex, sanitize.text.function =\r
       function(str) gsub("_", "\\_", str, fixed = TRUE))\r
 @\r
 \r
-\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
@@ -253,13 +201,14 @@ a custom \code{sanitize.text.function}.
 <<results='asis'>>=\r
 mat <- round(matrix(c(0.9, 0.89, 200, 0.045, 2.0), c(1, 5)), 4)\r
 rownames(mat) <- "$y_{t-1}$"\r
-colnames(mat) <- c("$R^2$", "$\\bar{R}^2$", "F-stat", "S.E.E", "DW")\r
+colnames(mat) <- c("$R^2$", "$\\bar{x}$", "F-stat", "S.E.E", "DW")\r
 mat <- xtable(mat)\r
 print(mat, sanitize.text.function = function(x) {x})\r
 @\r
 \r
+\newpage\r
 % By David Dahl to demonstrate contribution from David Whitting, 2007-10-09.\r
-\p\r
+\r
 You can also have sanitize functions that are specific to column or\r
 row names.  In the table below, the row name is not sanitized but\r
 column names and table elements are.\r
@@ -275,14 +224,14 @@ print(xtable(money), sanitize.rownames.function = function(x) {x})
 \section{Format examples}\r
 \subsection{Adding a centering environment}\r
 <<results='asis'>>=\r
-print(xtable(lm.D9, caption = "\\tt latex.environments = NULL"),\r
-      floating = TRUE, latex.environments = NULL)\r
-print(xtable(lm.D9, caption = "\\tt latex.environments = \"\""),\r
+print(xtable(anova(fm3), caption = "\\tt latex.environments = \"\""),\r
       floating = TRUE, latex.environments = "")\r
-print(xtable(lm.D9, caption = "\\tt latex.environments = \"center\""),\r
+print(xtable(anova(fm3), caption = "\\tt latex.environments = \"center\""),\r
       floating = TRUE, latex.environments = "center")\r
 @\r
 \r
+\newpage\r
+\r
 \subsection{Column alignment}\r
 <<results='asis'>>=\r
 tli.table <- xtable(tli[1:10, ])\r
@@ -328,7 +277,8 @@ tli.table
 \r
 \newpage\r
 \r
-\subsection{Suppress row names}\r
+\subsection{Suppress row/column names}\r
+\subsubsection{Suppress row names}\r
 <<results='asis'>>=\r
 tli.table <- xtable(tli[1:10, ])\r
 print(tli.table, include.rownames = FALSE)\r
@@ -350,7 +300,7 @@ align(tli.table) <- "|rr|lp{3cm}l|r|"
 \r
 \newpage\r
 \r
-\subsection{Suppress column names}\r
+\subsubsection{Suppress column names}\r
 <<results='asis'>>=\r
 print(tli.table, include.colnames = FALSE)\r
 @\r
@@ -362,14 +312,14 @@ print(tli.table, include.colnames = FALSE,
       hline.after = c(0,nrow(tli.table)))\r
 @\r
 \r
-\subsection{Suppress row and column names}\r
+\subsubsection{Suppress row and column names}\r
 <<results='asis'>>=\r
 print(tli.table, include.colnames = FALSE, include.rownames = FALSE)\r
 @\r
 \r
 \newpage\r
 \r
-\subsection{Rotate row and column names}\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
@@ -386,7 +336,7 @@ Use the \code{hline.after} argument to specify the position of the
 horizontal lines.\r
 \r
 <<results='asis'>>=\r
-print(xtable(anova(glm.D93)), hline.after = c(1))\r
+print(xtable(anova(fm3)), hline.after = c(1))\r
 @\r
 \r
 \subsubsection{Line styles}\r
@@ -422,9 +372,14 @@ hlines <- c(-1, 0, 1, nrow(bktbs))
 print(bktbs, booktabs = TRUE, hline.after = hlines)\r
 @\r
 \r
-\subsection{Table-level LaTeX}\r
+\subsection{Table level commands}\r
+<<results='asis'>>=\r
+print(xtable(anova(fm3)), size = "large")\r
+@\r
+\r
+\p\r
 <<results='asis'>>=\r
-print(xtable(anova(glm.D93)), size = "small")\r
+print(xtable(anova(fm3)), size = "\\setlength{\\tabcolsep}{12pt}")\r
 @\r
 \r
 \subsection{Long tables}\r
@@ -432,36 +387,77 @@ Requires \verb|\usepackage{longtable}| in the \LaTeX\ preamble.
 \r
 <<results='asis'>>=\r
 x <- matrix(rnorm(1000), ncol = 10)\r
-x.big <- xtable(x, caption = 'A \\code{longtable} spanning several pages')\r
-print(x.big, hline.after=c(-1, 0), tabular.environment = 'longtable')\r
+x.big <- xtable(x, caption = "A \\code{longtable} spanning several pages")\r
+print(x.big, hline.after=c(-1, 0), tabular.environment = "longtable")\r
 @\r
 \r
 %% The column name alignment is off in the following example.\r
 %% It needs some revision before exposing it. - CR, 7/2/2012\r
 %\r
-%\subsubsection{Long tables with the header on each page}\r
-%The \code{add.to.row} argument can be used to display the header\r
-%for a long table on each page, and to add a "continued" footer\r
-%on all pages except the last page.\r
-%\r
-%<<results=tex>>=\r
-%library(xtable)\r
-%x<-matrix(rnorm(1000), ncol = 10)\r
-%addtorow<-list()\r
-%addtorow$pos<-list()\r
-%addtorow$pos[[1]]<-c(0)\r
-%addtorow$command<-c(paste(\r
-%    "\\hline \n",\r
-%    "  \\endhead \n",\r
-%    "  \\hline \n",\r
-%    "  {\\footnotesize Continued on next page} \n",\r
-%    "  \\endfoot \n",\r
-%    "  \\endlastfoot \n", sep = ""))\r
-%x.big2 <- xtable(x, label = "tabbig2",\r
-%    caption = "Example of longtable with the header on each page")\r
-%print(x.big2, tabular.environment = "longtable",\r
-%include.rownames = FALSE, add.to.row = addtorow, hline.after = c(-1))\r
-%@\r
+%% Tried to fix this and got some of the way, but \hlines are wrong\r
+%% and first column is too wide. - DJS 4/10/2014\r
+%% \subsubsection{Long tables with the header on each page}\r
+%% The \code{add.to.row} argument can be used to display the header\r
+%% for a long table on each page, and to add a "continued" footer\r
+%% on all pages except the last page.\r
+\r
+%% \newcommand{\head}[1]{\centercell{\bfseries#1}}\r
+\r
+%% <<results='asis'>>=\r
+%% x <- matrix(rnorm(1000), ncol = 10)\r
+%% hdr <-  paste(paste("\\multicolumn{1}{c}{",1:9,"} & ", collapse = ""),\r
+%%               "\\multicolumn{1}{c}{10}\\\\")\r
+%% addtorow <- list()\r
+%% addtorow$pos <- list()\r
+%% addtorow$pos[[1]] <- c(0)\r
+%% addtorow$command <- c(paste(\r
+%%     hdr,\r
+%%     "  \\hline \n",\r
+%%     "  \\endhead \n",\r
+%%     "  \\hline \n",\r
+%%     "  {\\footnotesize Continued on next page} \n",\r
+%%     "  \\endfoot \n",\r
+%%     "  \\endlastfoot \n", sep = ""))\r
+%% x.big2 <- xtable(x, label = "tabbig2", align = "lrrrrrrrrrr",\r
+%%                  caption = "Example of longtable with the header on each page")\r
+%% print(x.big2, tabular.environment = "longtable",\r
+%%       include.rownames = FALSE, include.colnames = FALSE,\r
+%%       add.to.row = addtorow)\r
+%% @\r
+\r
+\newpage\r
+\r
+\subsection{Use of \code{add.to.row} argument}\r
+The following frequency table has outer dimnames: \code{Grade3} and\r
+\code{Grade6}.\r
+\r
+<<>>=\r
+Grade3 <- c("A","B","B","A","B","C","C","D","A","B",\r
+            "C","C","C","D","B","B","D","C","C","D")\r
+Grade6 <- c("A","A","A","B","B","B","B","B","C","C",\r
+            "A","C","C","C","D","D","D","D","D","D")\r
+Cohort <- table(Grade3, Grade6)\r
+Cohort\r
+@\r
+\r
+\p\r
+The default behavior of \code{print.xtable} is to strip outer dimnames.\r
+<<results='asis'>>=\r
+xtable(Cohort)\r
+@\r
+\r
+\p\r
+The desired column labels can be created using \code{add.to.row}, in this case\r
+applying two commands to ``row number zero'' while suppressing the basic column\r
+names.\r
+\r
+<<results='asis'>>=\r
+addtorow <- list()\r
+addtorow$pos <- list(0, 0)\r
+addtorow$command <- c("& \\multicolumn{4}{c}{Grade 6} \\\\\n",\r
+                      "Grade 3 & A & B & C & D \\\\\n")\r
+print(xtable(Cohort), add.to.row = addtorow, include.colnames = FALSE)\r
+@\r
 \r
 \subsection{Sideways tables}\r
 Requires \verb|\usepackage{rotating}| in the LaTeX\r
@@ -471,8 +467,8 @@ fairly nearby.
 \r
 <<results='asis'>>=\r
 x <- x[1:30, ]\r
-x.small <- xtable(x, label = 'tabsmall', caption = 'A sideways table')\r
-print(x.small, floating = TRUE, floating.environment = 'sidewaystable')\r
+x.side <- xtable(x, caption = "A sideways table")\r
+print(x.side, floating = TRUE, floating.environment = "sidewaystable")\r
 @\r
 \clearpage\r
 \r
@@ -484,6 +480,51 @@ x.rescale <- xtable(x)
 print(x.rescale, scalebox = 0.7)\r
 @\r
 \r
+\newpage\r
+\r
+\subsection{Aligning fixed width columns}\r
+Note that using specifications such as \verb|p{2cm}| always\r
+produces a \textbf{left aligned} column. What if some other alignment\r
+is desired?\r
+\r
+This is not really a problem with \pkg{xtable} but with the formatting\r
+of tables with fixed width columns and different alignments using\r
+standard \LaTeX.\r
+\r
+One solution is to use the \verb|array| package, defining new\r
+column formats.\r
+\r
+\begin{verbatim}\r
+\newcolumntype{L}[1]{>{\raggedright\let\newline\\\r
+    \arraybackslash\hspace{0pt}}m{#1}}\r
+\newcolumntype{C}[1]{>{\centering\let\newline\\\r
+    \arraybackslash\hspace{0pt}}m{#1}}\r
+\newcolumntype{R}[1]{>{\raggedleft\let\newline\\\r
+    \arraybackslash\hspace{0pt}}m{#1}}\r
+\newcolumntype{P}[1]{>{\raggedright\tabularxbackslash}p{#1}}\r
+\end{verbatim}\r
+\r
+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
+\r
+\newcolumntype{L}[1]{>{\raggedright\let\newline\\\r
+    \arraybackslash\hspace{0pt}}m{#1}}\r
+\newcolumntype{C}[1]{>{\centering\let\newline\\\r
+    \arraybackslash\hspace{0pt}}m{#1}}\r
+\newcolumntype{R}[1]{>{\raggedleft\let\newline\\\r
+    \arraybackslash\hspace{0pt}}m{#1}}\r
+\newcolumntype{P}[1]{>{\raggedright\tabularxbackslash}p{#1}}\r
+\r
+<<results='asis'>>=\r
+df <- data.frame(name = c("A","B"), right = c(1.4, 34.6),\r
+                 left = c(1.4, 34.6), text = c("txt1","txt2"))\r
+print(xtable(df, align = c("l", "|c", "|R{3cm}", "|L{3cm}", "| p{3cm}|")),\r
+      floating = FALSE, include.rownames = FALSE)\r
+@\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
@@ -520,10 +561,11 @@ x.ltx
 @\r
 \r
 \section{Acknowledgements}\r
-Most of the examples in this gallery are taken from the \code{xtable}\r
-documentation.\r
+Most of the examples in this gallery are taken from the \pkg{xtable}\r
+documentation. Two examples (\code{add.to.row} and `Aligning fixed width\r
+columns') are from Stack Exchange.\r
 \r
-\section{R session information}\r
+\section{\R\ session information}\r
 <<results='asis'>>=\r
 toLatex(sessionInfo())\r
 @\r