3 \alias{print.xtableFtable}
6 Create and Export Flat Tables
9 \code{xtableFtable} creates an object which contains information about
10 a flat table which can be used by \code{print.xtableFtable} to produce
11 a character string which when included in a document produces a nicely
15 xtableFtable(x, caption = NULL, label = NULL,
16 align = NULL, digits = 0, display = NULL,
18 method = c("non.compact", "row.compact",
19 "col.compact", "compact"),
20 lsep = " $\\\\vert$ ", ...)
22 \method{print}{xtableFtable}(x,
23 type = getOption("xtable.type", "latex"),
24 file = getOption("xtable.file", ""),
25 append = getOption("xtable.append", FALSE),
26 floating = getOption("xtable.floating", TRUE),
27 floating.environment = getOption("xtable.floating.environment", "table"),
28 table.placement = getOption("xtable.table.placement", "ht"),
29 caption.placement = getOption("xtable.caption.placement", "bottom"),
30 caption.width = getOption("xtable.caption.width", NULL),
31 latex.environments = getOption("xtable.latex.environments", c("center")),
32 tabular.environment = getOption("xtable.tabular.environment", "tabular"),
33 size = getOption("xtable.size", NULL),
34 hline.after = getOption("xtable.hline.after", NULL),
35 NA.string = getOption("xtable.NA.string", ""),
36 only.contents = getOption("xtable.only.contents", FALSE),
37 add.to.row = getOption("xtable.add.to.row", NULL),
38 sanitize.text.function = getOption("xtable.sanitize.text.function",
40 sanitize.rownames.function = getOption("xtable.sanitize.rownames.function",
41 sanitize.text.function),
42 sanitize.colnames.function = getOption("xtable.sanitize.colnames.function",
43 sanitize.text.function),
44 math.style.negative = getOption("xtable.math.style.negative", FALSE),
45 math.style.exponents = getOption("xtable.math.style.exponents", FALSE),
46 html.table.attributes = getOption("xtable.html.table.attributes",
48 print.results = getOption("xtable.print.results", TRUE),
49 format.args = getOption("xtable.format.args", NULL),
50 rotate.rownames = getOption("xtable.rotate.rownames", FALSE),
51 rotate.colnames = getOption("xtable.rotate.colnames", FALSE),
52 booktabs = getOption("xtable.booktabs", FALSE),
53 scalebox = getOption("xtable.scalebox", NULL),
54 width = getOption("xtable.width", NULL),
55 comment = getOption("xtable.comment", TRUE),
56 timestamp = getOption("xtable.timestamp", date()),
61 \item{x}{For \code{xtableFtable}, an object of class
62 \code{"ftable"}. For \code{print.xtableFtable}, an object of class
63 \code{c("xtableFtable", "ftable")}.}
64 \item{caption}{Character vector of length 1 or 2 containing the
65 table's caption or title. If length is 2, the second item is the
66 "short caption" used when LaTeX generates a "List of Tables". Set to
67 \code{NULL} to suppress the caption. Default value is \code{NULL}. }
68 \item{label}{Character vector of length 1 containing the LaTeX label
69 or HTML anchor. Set to \code{NULL} to suppress the label. Default
70 value is \code{NULL}. }
71 \item{align}{Character vector of length equal to the number of columns
72 of the resulting table, indicating the alignment of the corresponding
73 columns. Also, \code{"|"} may be used to produce vertical lines
74 between columns in LaTeX tables, but these are effectively ignored
75 when considering the required length of the supplied vector. If a
76 character vector of length one is supplied, it is split as
77 \code{strsplit(align, "")[[1]]} before processing. For a flat table,
78 the number of columns is the number of columns of data, plus the
79 number of row variables in the table, plus one for the row names,
80 even though row names are not printed.
81 Use \code{"l"}, \code{"r"}, and \code{"c"} to
82 denote left, right, and center alignment, respectively. Use
83 \code{"p{3cm}"} etc. for a LaTeX column of the specified width. For
84 HTML output the \code{"p"} alignment is interpreted as \code{"l"},
85 ignoring the width request.
86 If \code{NULL} all row variable labels will be left aligned,
87 separated from the data columns by a vertical line, and all data
88 columns will be right aligned. The actual length of \code{align}
89 depends on the value of \code{method}.}
91 Numeric vector of length equal to one (in which case it will be
92 replicated as necessary) or to the number of columns in the
93 resulting table. Since data in the table consists of
94 counts, the default is 0. If the value of \code{digits} is negative, the
95 corresponding columns are displayed in scientific format
96 with \code{abs(digits)} digits.}
98 Character vector of length equal to the number of columns of the
99 resulting table, indicating the format for the corresponding columns.
100 These values are passed to the \code{formatC}
101 function. Use \code{"d"} (for integers), \code{"f"}, \code{"e"},
102 \code{"E"}, \code{"g"}, \code{"G"}, \code{"fg"} (for reals), or
103 \code{"s"} (for strings). \code{"f"} gives numbers in the usual
104 \code{xxx.xxx} format; \code{"e"} and \code{"E"} give
105 \code{n.ddde+nn} or \code{n.dddE+nn} (scientific format); \code{"g"}
106 and \code{"G"} put \code{x[i]} into scientific format only if it
107 saves space to do so. \code{"fg"} uses fixed format as \code{"f"},
108 but \code{digits} as number of \emph{significant} digits. Note that
109 this can lead to quite long result strings.
110 If \code{NULL} all row variable names and labels will have format
111 \code{"s"}, and all data columns will have format \code{"d"}. The
112 actual length of \code{display} depends on the value of
114 \item{quote}{a character string giving the set of quoting characters
115 for \code{format.ftable} used in \code{print.xtableFtable}; to
116 disable quoting altogether, use \code{quote=""}.}
118 \item{method}{string specifying how the \code{"xtableFtable"} object is
119 printed in the \code{print} method. Can be abbreviated. Available
120 methods are (see the examples in \code{\link{print.ftable}}):
122 \item{"non.compact"}{the default representation of an
123 \code{"ftable"} object.}
124 \item{"row.compact"}{a row-compact version without empty cells
125 below the column labels.}
126 \item{"col.compact"}{a column-compact version without empty cells
127 to the right of the row labels.}
128 \item{"compact"}{a row- and column-compact version. This may imply
129 a row and a column label sharing the same cell. They are then
130 separated by the string \code{lsep}.}
133 \item{lsep}{only for \code{method = "compact"}, the separation string
134 for row and column labels.}
135 \item{type}{Type of table to produce. Possible values for \code{type}
136 are \code{"latex"} or \code{"html"}.
137 Default value is \code{"latex"} and is the only type implemented so far.}
138 \item{file}{Name of file where the resulting code should be saved. If
139 \code{file=""}, output is displayed on screen. Note that the
140 function also (invisibly) returns a character vector of the results
141 (which can be helpful for post-processing).
142 Default value is \code{""}.}
143 \item{append}{If \code{TRUE} and \code{file!=""}, code will be
144 appended to \code{file} instead of overwriting \code{file}.
145 Default value is \code{FALSE}.}
146 \item{floating}{If \code{TRUE} and \code{type="latex"}, the resulting
147 table will be a floating table (using, for example,
148 \code{\\begin\{table\}} and \code{\\end\{table\}}). See
149 \code{floating.environment} below.
150 Default value is \code{TRUE}. }
151 \item{floating.environment}{If \code{floating=TRUE} and
152 \code{type="latex"}, the resulting table uses the specified floating
153 environment. Possible values include \code{"table"}, \code{"table*"},
154 and other floating environments defined in LaTeX packages.
155 Default value is \code{"table"}.}
156 \item{table.placement}{If \code{floating=TRUE} and
157 \code{type="latex"}, the floating table will have placement given by
158 \code{table.placement} where \code{table.placement} must be
159 \code{NULL} or contain only elements of
160 \{"h","t","b","p","!","H"\}.
161 Default value is \code{"ht"}.}
162 \item{caption.placement}{The caption will be placed at the bottom
163 of the table if \code{caption.placement} is \code{"bottom"} and at
164 the top of the table if it equals \code{"top"}.
165 Default value is \code{"bottom"}.}
166 \item{caption.width}{The caption will be placed in a \code{"parbox"}
167 of the specified width if \code{caption.width} is not \code{NULL} and
168 \code{type="latex"}. Default value is \code{NULL}.}
169 \item{latex.environments}{If \code{floating=TRUE} and
170 \code{type="latex"}, the specified LaTeX environments (provided as
171 a character vector) will enclose the tabular environment.
172 Default value is \code{"center"}. }
173 \item{tabular.environment}{When \code{type="latex"}, the tabular
174 environment that will be used.
175 When working with tables that extend more than one page, using
176 \code{tabular.environment="longtable"} with the corresponding
177 LaTeX package (see Fairbairns, 2005) allows one to typeset them
178 uniformly. Note that \code{floating} should be set to
179 \code{FALSE} when using the \code{longtable} environment.
180 Default value is \code{"tabular"}.}
181 \item{size}{A character vector that is inserted just before the
182 tabular environment starts. This can be used to set the font size
183 and a variety of other table settings. Initial backslashes are
184 automatically prefixed, if not supplied by user.
185 Default value is \code{NULL}. }
186 \item{hline.after}{When \code{type="latex"}, a vector of numbers
187 between -1 and \code{nrow(x)}, inclusive, indicating the rows after
188 which a horizontal line should appear. Repeated values are
189 allowed. If \code{NULL} the default is to draw a line before before
190 starting the table, after the column variable names and labels, and
191 at the end of the table.}
192 \item{NA.string}{String to be used for missing values in table
194 Default value is \code{""}.}
195 \item{only.contents}{If \code{TRUE} only the rows of the
197 Default value is \code{FALSE}. }
198 \item{add.to.row}{A list of two components. The first component (which
199 should be called 'pos') is a list that contains the position of rows on
200 which extra commands should be added at the end. The second
201 component (which should be called 'command') is a character vector
202 of the same length as the first component, which contains the command
203 that should be added at the end of the specified rows.
204 Default value is \code{NULL}, i.e. do not add commands.}
205 \item{sanitize.text.function}{All non-numeric entries (except row and
206 column names) are sanitized in an attempt to remove characters which
207 have special meaning for the output format. If
208 \code{sanitize.text.function} is not \code{NULL}, it should
209 be a function taking a character vector and returning one, and will
210 be used for the sanitization instead of the default internal
212 Default value is \code{NULL}.}
213 \item{sanitize.rownames.function}{Like the
214 \code{sanitize.text.function}, but applicable to row names.
215 The default uses the \code{sanitize.text.function}. }
216 \item{sanitize.colnames.function}{Like the
217 \code{sanitize.text.function}, but applicable to column names.
218 The default uses the \code{sanitize.text.function}. }
219 \item{math.style.negative}{In a LaTeX table, if \code{TRUE}, then use
220 $-$ for the negative sign (as was the behavior prior to version 1.5-3).
221 Default value is \code{FALSE}.}
222 \item{math.style.exponents}{In a LaTeX table, if \code{TRUE} or
223 \code{"$$"}, then use \verb{$5 \times 10^{5}$} for 5e5. If
224 \code{"ensuremath"}, then use \verb{\\ensuremath{5 \times 10^{5}}}
225 for 5e5. If \code{"UTF-8"} or \code{"UTF-8"}, then use UTF-8 to
226 approximate the LaTeX typsetting for 5e5.
227 Default value is \code{FALSE}.}
228 \item{html.table.attributes}{In an HTML table, attributes associated
229 with the \code{<TABLE>} tag.
230 Default value is \code{"border=1"}.}
231 \item{print.results}{If \code{TRUE}, the generated table is printed to
232 standard output. Set this to \code{FALSE} if you will just be using
233 the character vector that is returned invisibly.
234 Default value is \code{TRUE}.}
235 \item{format.args}{List of arguments for the \code{formatC} function.
236 For example, standard German number separators can be specified as
237 \code{format.args=list(big.mark = "'", decimal.mark =
238 ","))}. The arguments \code{digits} and \code{format} should not be
239 included in this list.
240 Default value is \code{NULL}.}
241 \item{rotate.rownames}{If \code{TRUE}, the row names and labels, and
242 column variable names are displayed vertically in LaTeX.
243 Default value is \code{FALSE}.}
244 \item{rotate.colnames}{If \code{TRUE}, the column names and labels,
245 and row variable names are displayed vertically in LaTeX.
246 Default value is \code{FALSE}.}
247 \item{booktabs}{If \code{TRUE}, the \code{toprule}, \code{midrule} and
248 \code{bottomrule} commands from the LaTeX "booktabs" package are used
249 rather than \code{hline} for the horizontal line tags. }
250 \item{scalebox}{If not \code{NULL}, a \code{scalebox} clause will be
251 added around the tabular environment with the specified value used
252 as the scaling factor.
253 Default value is \code{NULL}.}
254 \item{width}{If not \code{NULL}, the specified value is included in
255 parentheses between the tabular environment \code{begin} tag and the
256 alignment specification. This allows specification of the table
257 width when using tabular environments such as \code{tabular*} and
258 \code{tabularx}. Note that table width specification is not
259 supported with the \code{tabular} or \code{longtable} environments.
260 Default value is \code{NULL}.}
261 \item{comment}{If \code{TRUE}, the version and timestamp comment is
262 included. Default value is \code{TRUE}. }
263 \item{timestamp}{Timestamp to include in LaTeX comment. Set this
264 to \code{NULL} to exclude the timestamp. Default value is
266 \item{...}{Additional arguments. (Currently ignored.) }
269 \code{xtableFtable} carries out some calculations to determine the
270 number of rows and columns of names and labels which will be in the
271 table when formatted as a flat table, which depends on the value of
272 \code{method}. It uses the results of those calculations to set
273 sensible values for \code{align} and \code{display} if these have
274 not been supplied. It attaches attributes to the resulting object
275 which specify details of the function call which are needed when
276 printing the resulting object which is of class
277 \code{c("xtableFtable", "ftable")}.
279 \code{print.xtableFtable} uses the attributes attached to an object
280 of class \code{c("xtableFtable", "ftable")} to create a suitable
281 character matrix object for subsequent printing via a call to
282 \code{stats:::format.ftable}. This matrix object is then printed via
283 a call to \code{print.xtable}.
285 Note that at present there is no code for \code{type = "html"}.
288 For \code{xtableFtable} an object of class \code{c("xtableFtable",
289 "ftable")}, with attributes
290 \item{ftableCaption}{the value of the \code{caption} argument}
291 \item{ftableLabel}{the value of the \code{label} argument}
292 \item{ftableAlign}{the value of the \code{label} argument}
293 \item{ftableDigits}{the value of the \code{digits} argument or the
294 default value if \code{digits = NULL}}
295 \item{quote}{the value of the \code{quote} argument}
296 \item{ftableDisplay}{the value of the \code{display} argument or the
297 default value if \code{align = NULL}}
298 \item{method}{the value of the \code{method} argument}
299 \item{lsep}{the value of the \code{lsep} argument}
300 \item{nChars}{a vector of length 2 giving the number of character rows
301 and the number of character columns}
303 For \code{print.xtableFtable} a character string which will produce a
304 formatted table when included in a LaTeX document.
308 Fairbairns, Robin (2005) \emph{Tables longer than a single page.} The
309 UK List of TeX Frequently Asked Questions on the
310 Web. \url{http://www.tex.ac.uk/cgi-bin/texfaq2html?label=longtab}
313 David Scott \email{d.scott@auckland.ac.nz}.
316 The functions \code{xtableFtable} and \code{print.xtableFtable} are
317 new and their behaviour may change in the future based on user
318 experience and recommendations.
320 It is not recommended that users change the values of \code{align},
321 \code{digits} or \code{align}. First of all, alternative values have
322 not been tested. Secondly, it is most likely that to determine
323 appropriate values for these arguments, users will have to investigate
324 the code for \code{xtableFtable} and/or \code{print.xtableFtable}.
328 \code{\link{ftable}}, \code{\link{print.ftable}},
329 \code{\link{xtable}}, \code{\link{caption}}, \code{\link{label}},
330 \code{\link{align}}, \code{\link{digits}}, \code{\link{display}},
331 \code{\link{formatC}}
335 mtcars$cyl <- factor(mtcars$cyl, levels = c("4","6","8"),
336 labels = c("four","six","eight"))
337 tbl <- ftable(mtcars$cyl, mtcars$vs, mtcars$am, mtcars$gear,
339 dnn = c("Cylinders", "V/S", "Transmission", "Gears"))
340 xftbl <- xtableFtable(tbl, method = "compact")
341 print.xtableFtable(xftbl, booktabs = TRUE)
342 xftbl <- xtableFtable(tbl, method = "row.compact")
343 print.xtableFtable(xftbl, rotate.colnames = TRUE,
344 rotate.rownames = TRUE)