kable()
allows.
It's aim is to provide the Hmisc latex()
colgroup and rowgroup functions in HTML. The html-output is designed for
maximum compatibility with LibreOffice/OpenOffice.
htmlTable(x, ...)
"htmlTable"(x, header, rnames, rowlabel, caption, tfoot, label, rgroup, n.rgroup, cgroup, n.cgroup, tspanner, n.tspanner, total, align = paste(rep("c", ncol(x)), collapse = ""), align.header = paste(rep("c", ncol(x)), collapse = ""), align.cgroup, css.rgroup = "font-weight: 900;", css.rgroup.sep = "", css.tspanner = "font-weight: 900; text-align: left;", css.tspanner.sep = "border-top: 1px solid #BEBEBE;", css.total = "border-top: 1px solid #BEBEBE; font-weight: 900;", css.cell = "", css.cgroup = "", css.class = "gmisc_table", css.table = "margin-top: 1em; margin-bottom: 1em;", pos.rowlabel = "bottom", pos.caption = "top", col.rgroup = "none", col.columns = "none", padding.rgroup = " ", padding.tspanner = "", ctable = TRUE, compatibility = getOption("htmlTableCompat", "LibreOffice"), cspan.rgroup = "all", ...)
"knit_print"(x, ...)
"print"(x, useViewer, ...)
print
and knit_print
it takes a string of the class htmlTable
as x
argument.print.htmlTable
function and any argument except the
useViewer
will be passed on to the cat
functions arguments.colnames(x)
rnames
,
rowlabel is a character string containing the
column heading for the rnames
.
html element). The
output is run through txtMergeLines
simplifying the generation
of multiple lines.see table 2
. This is
known as the element's id attribute, i.e. table id, in HTML linguo, and should
be unique id for an HTML element in contrast to the css.class
element attribute.n.rgroup
must be present when rgroup
is given. See
detailed description in section below.rgroup
is not specified, n.rgroup
is just used to divide off blocks of rows by horizontal
lines. If rgroup
is given but n.rgroup
is omitted, n.rgroup
will
default so that each row group contains the same number of rows. If you want additional
rgroup column elements to the cells you can sett the "add" attribute to rgroup
through
attr(rgroup, "add")
, see below explaining section.n.crgroup
as
matrices you can have column spanners for several rows. See cgroup section below for details.cgroup=c("Major_1","Major_2")
,
n.cgroup=c(3,3)
if "Major_1"
is to span columns 1-3 and
"Major_2"
is to span columns 4-6.
rowlabel
does not count in the column numbers. You can omit n.cgroup
if all groups have the same number of columns. If the n.cgroup is one less than
the number of columns in the matrix/data.frame then it automatically adds those.TRUE
if you are interested in such a row. If you
want a total row at the end of each table spanner you can set this to "tspanner"
.paste(rep('c',ncol(x)),collapse='')
to center. Valid alignments are
l = left, c = center and r = right. You can also specify align='c|c'
and
other LaTeX tabular formatting. If you want to set the alignment of the
rownames this string needst to be ncol(x) + 1
, otherwise it automatically
pads the string with a left alignment for the rownames.paste(rep('c',ncol(x)),collapse='')
.cgroups
x
argument. If have ncol(x) + 1
the first row will correspond to the
rowname style. Correspondingly if the size is nrow(x) + 1
it is assummed that the
first row is the header row.css.class
but for cgroup formatting."top"
,
"bottom"
, "header"
, or a integer between 1
and nrow(cgroup) + 1
. The options
"bottom"
and "header"
are the same, where the row label is presented at the same level as
the header."bottom"
to position a caption below the table
instead of the default of "top"
.rgroup
; one or two colors
is recommended and will be recycled.
, but some
journals only have a bold face for the rgroup and leaves the subelements unindented.padding.rgroup
and it will be added to all elements, including the rgroup elements.
This allows for a 3-level hierarchy if needed.LibreOffice
as some
settings need to be in old html format as Libre Office can't
handle some commands such as the css caption-alignment. Note: this
option is not yet fully implemented for all details, in the future
I aim to generate a html-correct table and one that is aimed
at Libre Office compatibility. Word-compatibility is difficult as
Word ignores most settings and destroys all layout attempts
(at least that is how my 2010 version behaves). You can additinally use the
options(htmlTableCompat = "html")
if you want a change to apply
to the entire document.rgroup
should span. It spans
by default all columns but you may want to limit this if you have column colors
that you want to retain.interactive
mode.
Set this to FALSE
if you want to remove that functionality. You can
also force the function to call a specific viewer by setting this to a
viewer function, e.g. useViewer = utils::browseUrl
if you want to
override the default RStudio viewer. Another option that does the same is to
set the options(viewer=utils::browseUrl)
and it will default to that
particular viewer (this is how RStudio decides on a viewer).
Note: If you want to force all output to go through the
cat()
the set options(htmlTable.cat = TRUE)
.string
Returns a string of class htmlTable
cgroup
and n.cgroup
arguments to matrices.
If the different levels have different number of elements you
need to set the ones that lack elements to NA. For instance
cgroup = rbind(c("first", "second", NA), c("a", "b", "c"))
.
And the corresponding n,cgroup would be n.cgroup = rbind(c(1, 2, NA), c(2, 1, 2))
.
for a table consisting of 5 columns. The "first" spans the first two columns,
the "second" spans the last three columns, "a" spans the first two, "b"
the middle column, and "c" the last two columns.sum(n.rgroup)
should always
be equal or less than the matrix rows. If less then it will pad the
remaining rows with either an empty rgroup, i.e. an "" or if the
rgroup is one longer than the n.rgroup the last n.rgroup element will
be calculated through nrow(x) - sum(n.rgroup)
in order to make
the table generating smoother.attr(rgroup, 'add')
. The value can either be a vector
, a list
,
or a matrix
. See vignette("general", package = "htmlTable")
for examples.
vector
of either equal number of rgroups to the number
of rgroups that aren't empty, i.e. rgroup[rgroup != ""]
. Or a named vector where
the name must correspond to either an rgroup or to an rgroup number.
list
that has exactly the same requirements as the vector.
In addition to the previous we can also have a list with column numbers within
as names within the list.
matrix
with the dimensiont nrow(x) x ncol(x)
or
nrow(x) x 1
where the latter is equivalent to a named vector.
If you have rownames
these will resolve similarly to the names to the
list
/vector
arguments. The same thing applies to colnames
. knit_print
and the
asis_output
the results='asis'
is no longer needed except within for-loops.
If you have a knitr-chunk with a for loop and use print()
to produce
raw html you must set the chunk option results='asis'
. Note
:
the print-function relies on the interactive()
function
for determining if the output should be sent to a browser or to the terminal.
In vignettes and other directly knitted documents you may need to either set
useViewer = FALSE
alternatively set options(htmlTable.cat = TRUE)
.options(table_counter=TRUE)
. If
you set it to a number then that number will correspond to the start of
the table_counter. The table_counter
option will also contain the number
of the last table, this can be useful when referencing it in text. By
setting the option options(table_counter_str = "Table %s: ")
you can manipulate the counter table text that is added prior to the
actual caption. Note, you should use the sprintf
%s
instead of %d
as the software converts all numbers to characters
for compatibility reasons. If you set options(table_counter_roman = TRUE)
then the table counter will use Roman numumerals instead of Arabic.<
instead of <
and
>
instead of >
. You can find a complete list
of html characters here.txtMergeLines
,
latex
# A simple output
output <- matrix(1:4,
ncol=2,
dimnames = list(list("Row 1", "Row 2"),
list("Column 1", "Column 2")))
htmlTable(output)
# An advanced output
output <-
matrix(ncol=6, nrow=8)
for (nr in 1:nrow(output)){
for (nc in 1:ncol(output)){
output[nr, nc] <-
paste0(nr, ":", nc)
}
}
htmlTable(output, align="r",
header = paste(c("1st", "2nd",
"3rd", "4th",
"5th", "6th"),
"hdr"),
rnames = paste(c("1st", "2nd",
"3rd",
paste0(4:8, "th")),
"row"),
rgroup = paste("Group", LETTERS[1:3]),
n.rgroup = c(2,4,nrow(output) - 6),
cgroup = rbind(c("", "Column spanners", NA),
c("", "Cgroup 1", "Cgroup 2†")),
n.cgroup = rbind(c(1,2,NA),
c(2,2,2)),
caption="Basic table with both column spanners (groups) and row groups",
tfoot="†A table footer commment",
cspan.rgroup = 2,
col.columns = c(rep("none", 2),
rep("#F5FBFF", 4)),
col.rgroup = c("none", "#F7F7F7"),
css.cell = "padding-left: .5em; padding-right: .2em;")
# See vignette("tables", package = "htmlTable")
# for more examples
Run the code above in your browser using DataLab