% \VignetteEngine{knitr::knitr}\r
%**************************************************************************\r
\documentclass[article, nojss]{jss}\r
-\usepackage{amsthm}\r
-\usepackage{amsmath}\r
-\usepackage{graphicx}\r
-\usepackage{color}\r
-\usepackage{afterpage}\r
-\usepackage{hyperref}\r
-\r
-\title{The \pkg{xtable} Gallery}\r
+\usepackage{booktabs}\r
+\usepackage{longtable}\r
+\usepackage{rotating}\r
+\usepackage{tabularx}\r
+\title{The xtable Gallery}\r
\author{Jonathan Swinton and others\\<jonathan@swintons.net>}\r
-\Plainauthor{Jonathan Swinton, and others} %% comma-separated\r
-\Plaintitle{The xtable Gallery} %% without formatting\r
-\Shorttitle{\pkg{xtable} Gallery} %% a short title (if necessary)\r
+\Abstract{This document gives a gallery of tables which can be made by using the\r
+ \texttt{xtable} package to create \LaTeX\ output. It doubles as a regression\r
+ check for the package.}\r
+\Keywords{Reproducible research, \LaTeX}\r
+\Shorttitle{\pkg{xtable} Gallery} %% JSS header\r
+\Address{Jonathan Swinton\\E-mail: \email{jonathan@swintons.net}} %% JSS last pg\r
+\newcommand\p{\vspace{2ex}}\r
+\begin{document}\r
\r
<<include=FALSE>>=\r
library(knitr)\r
opts_chunk$set(fig.path='figdir/fig', debug=TRUE, echo=TRUE)\r
-options(replace.assign = TRUE, width = 60,\r
- tidy.opts = list(width.cutoff = 60))\r
@\r
\r
-\usepackage{rotating}\r
-\usepackage{longtable}\r
-\usepackage{booktabs}\r
-\usepackage{tabularx}\r
-%\usepackage{hyperref}\r
-\r
-\Abstract{\r
- This document gives a gallery of tables which can be made by using\r
- the \texttt{xtable} package to create \LaTeX\ output. It doubles as\r
- a regression check for the package.\r
-}\r
-\r
-\Keywords{Reproducible research, \LaTeX}\r
-\Address{Jonathan Swinton\\E-mail: \email{jonathan@swintons.net}}\r
-\r
-\begin{document}\r
-\r
\section{Gallery}\r
-\r
\subsection{Data frame}\r
-Load example dataset\r
-<<>>=\r
+<<results='asis'>>=\r
library(xtable)\r
options(xtable.floating = FALSE)\r
data(tli)\r
-\r
-## Demonstrate data.frame\r
-tli.table <- xtable(tli[1:10, ])\r
-digits(tli.table)[c(2,6)] <- 0\r
-@\r
-<<results='asis'>>=\r
-tli.table\r
+xtable(tli[1:10, ])\r
@\r
\r
\newpage\r
\r
\subsection{Matrix}\r
-<<>>=\r
-design.matrix <- model.matrix(~ sex*grade, data = tli[1:10, ])\r
-design.table <- xtable(design.matrix)\r
-@\r
<<results='asis'>>=\r
-design.table\r
+design.matrix <- model.matrix(~ sex*grade, data = tli[1:10, ])\r
+xtable(design.matrix)\r
@\r
\r
\subsection{aov}\r
-<<>>=\r
-fm1 <- aov(tlimth ~ sex + ethnicty + grade + disadvg, data = tli)\r
-fm1.table <- xtable(fm1)\r
-@\r
<<results='asis'>>=\r
-fm1.table\r
+fm1 <- aov(tlimth ~ sex + ethnicty + grade + disadvg, data = tli)\r
+xtable(fm1)\r
@\r
\r
\subsection{lm}\r
-<<>>=\r
-fm2 <- lm(tlimth ~ sex*ethnicty, data = tli)\r
-fm2.table <- xtable(fm2)\r
-@\r
<<results='asis'>>=\r
-fm2.table\r
+fm2 <- lm(tlimth ~ sex*ethnicty, data = tli)\r
+xtable(fm2)\r
@\r
\r
-\vspace{12pt}\textbf{\itshape An anova object}\r
-\r
+\p\textbf{\itshape Anova table (one model)}\r
<<results='asis'>>=\r
xtable(anova(fm2))\r
@\r
-\vspace{12pt}\textbf{\itshape Another anova object}\r
-<<>>=\r
-fm2b <- lm(tlimth ~ ethnicty, data = tli)\r
-@\r
+\r
+\p\textbf{\itshape Anova table (two models)}\r
<<results='asis'>>=\r
+fm2b <- lm(tlimth ~ ethnicty, data = tli)\r
xtable(anova(fm2b, fm2))\r
@\r
\r
\subsection{glm}\r
-<<>>=\r
-## Demonstrate glm\r
-fm3 <- glm(disadvg ~ ethnicty*grade, data = tli, family = binomial())\r
-fm3.table <- xtable(fm3)\r
-@\r
<<results='asis'>>=\r
-fm3.table\r
+fm3 <- glm(disadvg ~ ethnicty*grade, data = tli, family = binomial)\r
+xtable(fm3)\r
@\r
\r
-\vspace{12pt}\textbf{\itshape An anova object}\r
+\p\textbf{\itshape Anova table}\r
<<results='asis'>>=\r
xtable(anova(fm3))\r
@\r
\r
\subsection{More aov}\r
-<<>>=\r
-## Demonstrate aov\r
-## Taken from help(aov) in R 1.1.1\r
-## From Venables and Ripley (1997) p.210.\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
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
-op <- options(contrasts = c("contr.helmert", "contr.treatment"))\r
-npk.aovE <- aov(yield ~ N*P*K + Error(block), npk)\r
-options(op)\r
-@\r
-<<results='asis'>>=\r
xtable(npk.aov)\r
@\r
\r
-\vspace{12pt}\textbf{\itshape An anova object}\r
+\p\textbf{\itshape Anova table}\r
<<results='asis'>>=\r
xtable(anova(npk.aov))\r
@\r
\r
-\vspace{12pt}\textbf{\itshape Another anova object}\r
+\p\textbf{\itshape Summary table}\r
<<results='asis'>>=\r
xtable(summary(npk.aov))\r
@\r
\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
@\r
\r
\subsection{More lm}\r
-<<>>=\r
-## Demonstrate lm\r
-## Taken from help(lm) in R 1.1.1\r
-## Annette Dobson (1990) "An Introduction to Generalized Linear Models".\r
-## Page 9: Plant Weight Data.\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
-@\r
-<<results='asis'>>=\r
xtable(lm.D9)\r
@\r
\r
@\r
\r
\subsection{More glm}\r
-<<>>=\r
-## Demonstrate glm\r
-## Taken from help(glm) in R 1.1.1\r
-## Annette Dobson (1990) "An Introduction to Generalized Linear Models".\r
-## Page 93: Randomized Controlled Trial :\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
-@\r
-<<results='asis'>>=\r
+glm.D93 <- glm(counts ~ outcome + treatment, family = poisson)\r
xtable(glm.D93, align = "r|llrc")\r
@\r
\r
\subsection{prcomp}\r
-<<prcomp>>=\r
-if(require(stats, quietly = TRUE)) {\r
- ## Demonstrate prcomp\r
- ## Taken from help(prcomp) in mva package of R 1.1.1\r
- data(USArrests)\r
- pr1 <- prcomp(USArrests)\r
-}\r
-@\r
<<results='asis'>>=\r
+pr1 <- prcomp(USArrests)\r
xtable(pr1)\r
@\r
\r
xtable(summary(pr1))\r
@\r
\r
-<<echo = FALSE, results = 'Hide'>>=\r
-# ## Demonstrate princomp\r
-# ## Taken from help(princomp) in mva package of R 1.1.1\r
-# pr2 <- princomp(USArrests)\r
-# xtable(pr2)\r
+<<include=FALSE>>=\r
+# pr2 <- princomp(USArrests)\r
+# xtable(pr2)\r
@\r
\r
\subsection{Time series}\r
-<<>>=\r
+<<results='asis'>>=\r
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
@\r
-<<results='asis'>>=\r
-temp.table\r
-@\r
-<<savetofile,echo=FALSE>>=\r
-if (FALSE) {\r
- for(i in c("latex", "html")) {\r
- outFileName <- paste("xtable.", ifelse(i=="latex", "tex", i), sep = "")\r
- print(xtable(lm.D9), type = i, file = outFileName, append = TRUE,\r
- latex.environments = NULL)\r
- print(xtable(lm.D9), type = i, file = outFileName, append = TRUE,\r
- latex.environments = "")\r
- print(xtable(lm.D9), type = i, file = outFileName, append = TRUE,\r
- latex.environments = "center")\r
- print(xtable(anova(glm.D93, test = "Chisq")),\r
- type = i, file = outFileName,\r
- append = TRUE)\r
- print(xtable(anova(glm.D93)), hline.after = c(1),\r
- size = "small", type = i,\r
- file = outFileName, append = TRUE)\r
- # print(xtable(pr2), type = i, file = outFileName, append = TRUE)\r
- }\r
-}\r
+\r
+<<include=FALSE>>=\r
+# ## Demonstrate saving to file\r
+# for(i in c("latex", "html")) {\r
+# outFileName <- paste("xtable.", ifelse(i=="latex", "tex", i), sep = "")\r
+# print(xtable(lm.D9), type = i, file = outFileName, append = TRUE,\r
+# latex.environments = NULL)\r
+# print(xtable(lm.D9), type = i, file = outFileName, append = TRUE,\r
+# latex.environments = "")\r
+# print(xtable(lm.D9), type = i, file = outFileName, append = TRUE,\r
+# latex.environments = "center")\r
+# print(xtable(anova(glm.D93, test = "Chisq")),\r
+# type = i, file = outFileName,\r
+# append = TRUE)\r
+# print(xtable(anova(glm.D93)), hline.after = c(1),\r
+# size = "small", type = i,\r
+# file = outFileName, append = TRUE)\r
+# # print(xtable(pr2), type = i, file = outFileName, append = TRUE)\r
+# }\r
@\r
\r
\section{Helper functions for formatting}\r
-\label{sec:helperfns}\r
-The functions \code{xalign}, \code{xdigits}, and \code{xdisplay} are\r
-useful for formatting tables in a sensible way.\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
\r
-<<preliminary>>=\r
+<<results='asis'>>=\r
dat <- mtcars[1:3, 1:6]\r
-@ %def\r
-\r
-Consider the output produced by the default formatting.\r
-\r
-<<current>>=\r
x <- xtable(dat)\r
x\r
-@ %def\r
-\r
-In a \LaTeX\ document this appears as follows.\r
-\r
-<<currentresult, echo = FALSE, results = 'asis'>>=\r
-x\r
-@ %def\r
+@\r
\r
+\p\r
Now change the default alignment, digits and display using helper functions\r
-\texttt{xalign}, \texttt{xdigits}, and \texttt{xdisplay}.\r
+\code{xalign}, \code{xdigits}, and \code{xdisplay}. This produces a better\r
+format as shown below.\r
\r
-<<proposed>>=\r
+<<results='asis'>>=\r
align(x) <- xalign(x)\r
digits(x) <- xdigits(x)\r
display(x) <- xdisplay(x)\r
x\r
-@ %def\r
-\r
-This produces a better format as shown below.\r
-\r
-<<proposedresult, echo = FALSE, results = 'asis'>>=\r
-x\r
-@ %def\r
+@\r
\r
\section{Sanitization}\r
-<<>>=\r
+<<results='asis'>>=\r
insane <- data.frame(Name = c("Ampersand","Greater than","Less than",\r
"Underscore","Per cent","Dollar",\r
"Backslash","Hash","Caret","Tilde",\r
Character = I(c("&",">","<","_","%","$",\r
"\\","#","^","~","{","}")))\r
colnames(insane)[2] <- paste(insane[, 2], collapse = "")\r
-@\r
-\r
-<<pxti,results='asis'>>=\r
xtable(insane)\r
@\r
\r
-\vspace{12pt}\r
-Sometimes you might want to have your own sanitization function\r
-<<>>=\r
+\p\r
+Sometimes you might want to have your own sanitization function.\r
+\r
+<<results='asis'>>=\r
wanttex <- xtable(data.frame(label =\r
paste("Value_is $10^{-",1:3,"}$", sep = "")))\r
-@\r
-<<results='asis'>>=\r
-print(wanttex,\r
- sanitize.text.function =\r
- function(str)gsub("_", "\\_", str, fixed = TRUE))\r
+print(wanttex, sanitize.text.function =\r
+ function(str) gsub("_", "\\_", str, fixed = TRUE))\r
@\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
+a custom \code{sanitize.text.function}.\r
\r
-<<>>=\r
+<<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
mat <- xtable(mat)\r
-@\r
-<<results='asis'>>=\r
-print(mat, sanitize.text.function = function(x){x})\r
+print(mat, sanitize.text.function = function(x) {x})\r
@\r
\r
% By David Dahl to demonstrate contribution from David Whitting, 2007-10-09.\r
+\p\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
-<<>>=\r
+column names and table elements are.\r
+\r
+<<results='asis'>>=\r
money <- matrix(c("$1,000","$900","$100"), ncol = 3,\r
dimnames = list("$\\alpha$",\r
- c("Income (US$)","Expenses (US$)",\r
- "Profit (US$)")))\r
-@\r
-<<results='asis'>>=\r
+ c("Income (US$)","Expenses (US$)",\r
+ "Profit (US$)")))\r
print(xtable(money), sanitize.rownames.function = function(x) {x})\r
@\r
\r
\section{Format examples}\r
-\r
-\subsection{Adding a centering environment }\r
+\subsection{Adding a centering environment}\r
<<results='asis'>>=\r
- print(xtable(lm.D9, caption = "\\tt latex.environments = NULL"),\r
- latex.environments = NULL)\r
- print(xtable(lm.D9, caption = "\\tt latex.environments = \"\""),\r
- latex.environments = "")\r
- print(xtable(lm.D9, caption = "\\tt latex.environments = \"center\""),\r
- latex.environments = "center")\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
+ floating = TRUE, latex.environments = "")\r
+print(xtable(lm.D9, caption = "\\tt latex.environments = \"center\""),\r
+ floating = TRUE, latex.environments = "center")\r
@\r
\r
\subsection{Column alignment}\r
-<<>>=\r
+<<results='asis'>>=\r
tli.table <- xtable(tli[1:10, ])\r
-@\r
-<<>>=\r
align(tli.table) <- rep("r", 6)\r
-@\r
-<<results='asis'>>=\r
tli.table\r
@\r
\r
-\vspace{12pt}\r
-\textbf{\itshape Single string and column lines}\r
-<<>>=\r
-align(tli.table) <- "|rrl|l|lr|"\r
-@\r
+\p\textbf{\itshape Left aligned strings with column lines}\r
<<results='asis'>>=\r
+align(tli.table) <- "|rrl|l|lr|"\r
tli.table\r
@\r
-\vspace{12pt}\textbf{\itshape Fixed width columns}\r
-<<>>=\r
-align(tli.table) <- "|rr|lp{3cm}l|r|"\r
-@\r
+\r
+\p\textbf{\itshape Fixed width columns}\r
<<results='asis'>>=\r
+align(tli.table) <- "|rr|lp{3cm}l|r|"\r
tli.table\r
@\r
\r
-\subsection{Significant digits}\r
-Specify with a single argument\r
-<<>>=\r
-digits(tli.table) <- 3\r
-@\r
+\subsection{Number of digits}\r
+One number for all columns,\r
<<results='asis'>>=\r
+display(tli.table)[c(2,6)] <- "f"\r
+digits(tli.table) <- 3\r
tli.table\r
@\r
\r
-\vspace{12pt}\r
-or one for each column, counting the row names,\r
-<<>>=\r
-digits(tli.table) <- 1:(ncol(tli)+1)\r
-@\r
+\p\r
+or one for each column, including the row names,\r
<<results='asis'>>=\r
+digits(tli.table) <- 1:(ncol(tli)+1)\r
tli.table\r
@\r
\r
-\vspace{12pt}\r
-or as a full matrix\r
-<<>>=\r
-digits(tli.table) <- matrix( 0:4, nrow = 10, ncol = ncol(tli)+1 )\r
-@\r
+\p\r
+or as a full matrix.\r
<<results='asis'>>=\r
+digits(tli.table) <- matrix(0:4, nrow = 10, ncol = ncol(tli)+1)\r
tli.table\r
@\r
\r
\subsection{Suppress row names}\r
<<results='asis'>>=\r
+tli.table <- xtable(tli[1:10, ])\r
print(tli.table, include.rownames = FALSE)\r
@\r
\r
-\vspace{12pt} If you want a vertical line on the left, you need to\r
-change the \code{align} attribute.\r
-<<>>=\r
-align(tli.table) <- "|r|r|lp{3cm}l|r|"\r
-@\r
+\p\r
+If you want a vertical line on the left, you need to change the \code{align}\r
+attribute.\r
<<results='asis'>>=\r
+align(tli.table) <- "|r|r|lp{3cm}l|r|"\r
print(tli.table, include.rownames = FALSE)\r
@\r
\r
-\vspace{12pt} Revert the alignment to what is was before.\r
+\p\r
+Revert the alignment to what is was before.\r
<<>>=\r
align(tli.table) <- "|rr|lp{3cm}l|r|"\r
@\r
print(tli.table, include.colnames = FALSE)\r
@\r
\r
-\vspace{12pt}\r
-Note the doubled header lines which can be suppressed with, eg,\r
+\p\r
+Note the doubled header lines which can be suppressed.\r
<<results='asis'>>=\r
print(tli.table, include.colnames = FALSE,\r
hline.after = c(0,nrow(tli.table)))\r
@\r
\r
\subsection{Rotate row and column names}\r
-The \texttt{rotate.rownames } and \texttt{rotate.colnames} arguments can be\r
-used to rotate the row and/or 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
\r
<<results='asis'>>=\r
print(tli.table, rotate.rownames = TRUE, rotate.colnames = TRUE)\r
@\r
\r
\subsection{Horizontal lines}\r
-\vspace{12pt}\textbf{\itshape Line locations}\r
+\p\textbf{\itshape Line locations}\r
\r
-Use the \texttt{hline.after} argument to specify the position of the\r
+Use the \code{hline.after} argument to specify the position of the\r
horizontal lines.\r
\r
<<results='asis'>>=\r
print(xtable(anova(glm.D93)), hline.after = c(1))\r
@\r
\r
-\vspace{12pt}\textbf{\itshape Line styles}\r
-\r
-The \LaTeX package \pkg{ booktabs} can be used to specify different\r
-line style tags for top, middle, and bottom lines. Specifying\r
-\code{ booktabs = TRUE} will lead to separate tags being generated\r
-for the three line types.\r
-\r
-Insert \verb|\usepackage{booktabs}| in your \LaTeX preamble and define\r
-the \texttt{toprule}, \texttt{midrule}, and \texttt{bottomrule}\r
-tags to specify the line styles. By default, when no value is given\r
-for \texttt{hline.after}, a \texttt{toprule} will be drawn above the\r
-table, a \texttt{midrule} after the table headings and a\r
-\texttt{bottomrule} below the table. The width of the top and bottom\r
-rules can be set by supplying a value to \verb+\heavyrulewidth+. The\r
-width of the midrules can be set by supplying a value to\r
-\verb+\lightrulewidth+. The following tables have\r
-\verb+\heavyrulewidth = 2pt+ and \verb+\lightrulewidth = 0.5pt+, to\r
-ensure the difference in weight is noticeable.\r
+\p\textbf{\itshape Line styles}\r
\r
-There is no support for \verb+\cmidrule+ or \verb+\specialrule+\r
-although they are part of the \texttt{booktabs} package.\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
\r
-\heavyrulewidth = 2pt\r
-\lightrulewidth = 0.5pt\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
<<results='asis'>>=\r
-print(tli.table, booktabs = TRUE)\r
+tli.table <- xtable(tli[1:10, ])\r
+print(tli.table, include.rownames = FALSE, booktabs = TRUE)\r
@\r
\r
-\vspace{12pt}\r
-If \texttt{hline.after} includes $-1$, a \texttt{toprule} will be\r
-drawn above the table. If \texttt{hline.after} includes the number of\r
-rows in the table, a \texttt{bottomrule} will be drawn below the\r
-table. For any other values specified in \texttt{hline.after}, a\r
-\texttt{midrule} will be drawn after that line of the table.\r
+\p\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
+table. For any other values specified in \code{hline.after}, a\r
+\verb|\midrule| will be drawn after that line of the table.\r
\r
-The next table has more than one \texttt{midrule}.\r
+\p\r
+The following table has more than one \verb|\midrule|.\r
\r
-<<>>=\r
-bktbs <- xtable(matrix(1:10, ncol = 2))\r
-hlines <- c(-1,0,1,nrow(bktbs))\r
-@\r
-This command produces the required table.\r
<<results='asis'>>=\r
+bktbs <- xtable(matrix(1:10, ncol = 2))\r
+hlines <- c(-1, 0, 1, nrow(bktbs))\r
print(bktbs, booktabs = TRUE, hline.after = hlines)\r
@\r
\r
@\r
\r
\subsection{Long tables}\r
-Remember to insert \verb|\usepackage{longtable}| in your \LaTeX preamble.\r
+Requires \verb|\usepackage{longtable}| in the \LaTeX\ preamble.\r
\r
-<<longtable>>=\r
-## Demonstration of longtable support.\r
-x <- matrix(rnorm(1000), ncol = 10)\r
-x.big <- xtable(x, label = 'tabbig',\r
- caption = 'Example of \\code{longtable} spanning several pages')\r
-@\r
<<results='asis'>>=\r
-print(x.big, tabular.environment = 'longtable')\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
@\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
%\r
-%\vspace{12pt}\textbf{\itshape Long tables with the header on each page}\r
+%\p\textbf{\itshape Long tables with the header on each page}\r
%\r
-%The \texttt{add.to.row} argument can be used to display the header\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
%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
+%include.rownames = FALSE, add.to.row = addtorow, hline.after = c(-1))\r
%@\r
\r
\subsection{Sideways tables}\r
-Remember to insert \verb|\usepackage{rotating}| in your LaTeX\r
-preamble. Sideways tables can't be forced in place with the `H'\r
+Requires \verb|\usepackage{rotating}| in the LaTeX\r
+preamble. Sideways tables can't be forced in place with the \code{[H]}\r
specifier, but you can use the \verb|\clearpage| command to get them\r
fairly nearby.\r
\r
-<<>>=\r
+<<results='asis'>>=\r
x <- x[1:30, ]\r
x.small <- xtable(x, label = 'tabsmall', caption = 'A sideways table')\r
-@\r
-\r
-<<results='asis'>>=\r
print(x.small, floating = TRUE, floating.environment = 'sidewaystable')\r
@\r
\clearpage\r
\r
\subsection{Rescaled tables}\r
-Specify a \texttt{scalebox} value to rescale the table.\r
-\r
-<<>>=\r
-x <- x[1:20, ]\r
-x.rescale <- xtable(x, label = 'tabrescaled', caption = 'A rescaled table')\r
-@\r
-\r
+Specify a \code{scalebox} value to rescale the table.\r
<<results='asis'>>=\r
+x <- x[1:20, ]\r
+x.rescale <- xtable(x)\r
print(x.rescale, scalebox = 0.7)\r
@\r
\r
-\subsection{Table Width}\r
-The \texttt{tabularx} tabular environment provides more alignment options,\r
-and has a \texttt{width} argument to specify the table width.\r
-\r
-Remember to insert \verb|\usepackage{tabularx}| in your \LaTeX\ preamble.\r
-\r
-<<>>=\r
-df.width <- data.frame(\r
- "label 1 with much more text than is needed" = c("item 1", "A"),\r
- "label 2 is also very long" = c("item 2","B"),\r
- "label 3" = c("item 3","C"),\r
- "label 4" = c("item 4 but again with too much text","D"),\r
- check.names = FALSE)\r
-\r
-x.width <- xtable(df.width,\r
- caption = "Using the 'tabularx' environment")\r
-align(x.width) <- "|l|X|X|l|X|"\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
\r
<<results='asis'>>=\r
-print(x.width, tabular.environment = "tabularx",\r
- width = "\\textwidth")\r
+df.width <- data.frame(One = c("item 1", "A"), Two = c("item 2", "B"),\r
+ Three = c("item 3", "C"), Four = c("item 4", "D"))\r
+x.width <- xtable(df.width)\r
+align(x.width) <- "|l|X|l|l|l|"\r
+print(x.width, tabular.environment = "tabularx", width = "\\textwidth")\r
@\r
\r
-\section{Suppressing Printing}\r
-By default the \texttt{print} method will print the LaTeX or HTML to standard\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 \texttt{print.results = FALSE}.\r
+standard output can be suppressed by specifying \code{print.results = FALSE}.\r
\r
<<>>=\r
x.out <- print(tli.table, print.results = FALSE)\r
@\r
\r
Formatted output can also be captured without printing with the\r
-\texttt{toLatex} method. This function returns an object of class\r
-\texttt{"Latex"}.\r
+\code{toLatex} method. This function returns an object of class\r
+\code{"Latex"}.\r
\r
<<>>=\r
x.ltx <- toLatex(tli.table)\r
@\r
\r
\section{Acknowledgements}\r
-Most of the examples in this gallery are taken from the \texttt{xtable}\r
+Most of the examples in this gallery are taken from the \code{xtable}\r
documentation.\r
\r
-\section{R Session information}\r
+\section{R session information}\r
<<results='asis'>>=\r
toLatex(sessionInfo())\r
@\r