File: listOfTablesGallery.Rnw

package info (click to toggle)
r-cran-xtable 1%3A1.8-4-2
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, bullseye, forky, sid, trixie
  • size: 992 kB
  • sloc: sh: 19; makefile: 2
file content (234 lines) | stat: -rw-r--r-- 5,888 bytes parent folder | download | duplicates (4)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
%\VignetteIndexEntry{xtable List of Tables Gallery}
%\VignetteDepends{xtable}
%\VignetteKeywords{LaTeX, HTML, table}
%\VignettePackage{xtable}
% !Rnw weave = knitr
% \VignetteEngine{knitr::knitr}
%**************************************************************************
\documentclass{article}
\usepackage[a4paper,height=24cm]{geometry} % geometry first
\usepackage{array}
\usepackage{booktabs}
\usepackage{longtable}
\usepackage{parskip}
\usepackage{rotating}
\usepackage{tabularx}
\usepackage{titlesec}
\usepackage{hyperref} % hyperref last
\titleformat\subsubsection{\bfseries\itshape}{}{0pt}{}
\newcommand\p{\vspace{2ex}}
\newcommand\code[1]{\texttt{#1}}
\newcommand\pkg[1]{\textbf{#1}}
\setcounter{tocdepth}{2}
\begin{document}

\title{\bfseries\Large The \code{xtableList} Gallery}
\author{\bfseries David J. Scott}
\maketitle

\tableofcontents

\newpage

\section{Introduction}
This document represents a test of the functions in \pkg{xtable} which
deal with lists of dataframes.

<<set, include=FALSE>>=
library(knitr)
opts_chunk$set(fig.path='Figures/list', debug=TRUE, echo=TRUE)
opts_chunk$set(out.width='0.9\\textwidth')
@

The first step is to load the package and set some options for this document.
<<package, results='asis'>>=
library(xtable)
options(xtable.floating = FALSE)
options(xtable.timestamp = "")
options(width = 60)
@


Next we create a list of dataframes with attributes.

<<data>>=
require(xtable)
data(mtcars)
mtcars <- mtcars[, 1:6]
mtcarsList <- split(mtcars, f = mtcars$cyl)
### Reduce the size of the list elements
mtcarsList[[1]] <- mtcarsList[[1]][1,]
mtcarsList[[2]] <- mtcarsList[[2]][1:2,]
mtcarsList[[3]] <- mtcarsList[[3]][1:3,]
attr(mtcarsList, "subheadings") <- paste0("Number of cylinders = ",
                                          names(mtcarsList))
attr(mtcarsList, "message") <- c("Line 1 of Message",
                                 "Line 2 of Message")
str(mtcarsList)
attributes(mtcarsList)
@ %def

Now create a list of \code{xtable} objects.


<<xtablelist>>=
xList <- xtableList(mtcarsList)
str(xList)
@ %def

Create an alternative version where the lists have different values
for \code{digits}.


<<xtablelist1>>=
xList1 <- xtableList(mtcarsList, digits = c(0,2,0,0,0,1,2))
str(xList1)
@ %def

<<xtablelist2>>=
xList2 <- xtableList(mtcarsList, digits = c(0,2,0,0,0,1,2),
                            caption = "Caption to List",
                            label = "tbl:xtableList")
str(xList2)
@ %def

Further versions with no subheadings, and no message

<<xtablelist3>>=
attr(mtcarsList, "subheadings") <- NULL
xList3 <- xtableList(mtcarsList)
str(xList3)
@ %def

<<xtablelist4>>=
attr(mtcarsList, "message") <- NULL
xList4 <- xtableList(mtcarsList)
str(xList4)
@ %def

\newpage

\section{Single Column Names}
\label{sec:single-column-names}

Print the list of \code{xtable} objects with a single header of the
column names.

First the default.


<<singledefault, results='asis'>>=
print.xtableList(xList)
@ %def

Booktabs should work.
<<singlebooktabs, results='asis'>>=
print.xtableList(xList, booktabs = TRUE)
@ %def

With digits being specified.
<<singlebooktabs1, results='asis'>>=
print.xtableList(xList1, booktabs = TRUE)
@ %def

Row and column names, subheadings, and the message can be sanitized.

<<sanitize>>=
large <- function(x){
  paste0('{\\Large{\\bfseries ', x, '}}')
}
italic <- function(x){
  paste0('{\\emph{ ', x, '}}')
}
bold <- function(x){
  paste0('{\\bfseries ', x, '}')
}
red <- function(x){
  paste0('{\\color{red} ', x, '}')
}
@ %def


<<sanitizesingle, results='asis'>>=
print.xtableList(xList,
                 sanitize.rownames.function = italic,
                 sanitize.colnames.function = large,
                 sanitize.subheadings.function = bold,
                 sanitize.message.function = red,
                 booktabs = TRUE)
@ %def

A label and caption can be added.
<<singlecaption, results='asis'>>=
print.xtableList(xList2, floating = TRUE)
@ %def

Rotated column names?
<<singlerotated, results='asis'>>=
print.xtableList(xList, rotate.colnames = TRUE)
@ %def

No subheadings?
<<nosubheadings, results='asis'>>=
print.xtableList(xList3)
@ %def

No message?
<<nomessage, results='asis'>>=
print.xtableList(xList4)
@ %def


\section{Multiple Column Names}
\label{sec:multiple-column-names}

Print the list of \code{xtable} objects with multiple headers of the
column names.

First the default with multiple column name headers.

<<multipledefault, results='asis'>>=
print.xtableList(xList, colnames.format = "multiple")
@ %def

Using booktabs:

<<multiplebooktabs, results='asis'>>=
print.xtableList(xList, colnames.format = "multiple",
                 booktabs = TRUE)
@ %def

With sanitization.
<<sanitizemultiple, results='asis'>>=
print.xtableList(xList, colnames.format = "multiple",
                 sanitize.rownames.function = italic,
                 sanitize.colnames.function = large,
                 sanitize.subheadings.function = bold,
                 sanitize.message.function = red,                 
                 booktabs = TRUE)
@ %def

A label and caption can be added.
<<multiplecaption, results='asis'>>=
print.xtableList(xList2, colnames.format = "multiple",
                 floating = TRUE)
@ %def

Rotated column names?
<<multiplerotated, results='asis'>>=
print.xtableList(xList, colnames.format = "multiple",
                 rotate.colnames = TRUE)
@ %def

No subheadings?
<<multiplenosubheadings, results='asis'>>=
print.xtableList(xList3, colnames.format = "multiple")
@ %def

No message?
<<multiplenomessage, results='asis'>>=
print.xtableList(xList4, colnames.format = "multiple")
@ %def


\end{document}