Output an HTML table
Source:R/htmlTable.R, R/htmlTable_render_knit_print.R, R/htmlTable_render_print.R
htmlTable.RdGenerates advanced HTML tables with column and row groups
for a dense representation of complex data. Designed for
maximum compatibility with copy-paste into word processors.
For styling, see addHtmlTableStyle() and setHtmlTableTheme().
Note: If you are using tidyverse and dplyr you may
want to check out tidyHtmlTable() that automates many of the arguments
that htmlTable requires.
Usage
htmlTable(
x,
header = NULL,
rnames = NULL,
rowlabel = NULL,
caption = NULL,
tfoot = NULL,
label = NULL,
rgroup = NULL,
n.rgroup = NULL,
cgroup = NULL,
n.cgroup = NULL,
tspanner = NULL,
n.tspanner = NULL,
total = NULL,
ctable = TRUE,
compatibility = getOption("htmlTableCompat", "LibreOffice"),
cspan.rgroup = "all",
escape.html = FALSE,
...
)
# Default S3 method
htmlTable(
x,
header = NULL,
rnames = NULL,
rowlabel = NULL,
caption = NULL,
tfoot = NULL,
label = NULL,
rgroup = NULL,
n.rgroup = NULL,
cgroup = NULL,
n.cgroup = NULL,
tspanner = NULL,
n.tspanner = NULL,
total = NULL,
ctable = TRUE,
compatibility = getOption("htmlTableCompat", "LibreOffice"),
cspan.rgroup = "all",
escape.html = FALSE,
...
)
# S3 method for class 'htmlTable'
knit_print(x, ...)
# S3 method for class 'htmlTable'
print(x, useViewer, ...)Arguments
- x
The matrix/data.frame with the data. For the
printandknit_printit takes a string of the classhtmlTableasxargument.- header
A vector of character strings specifying column header, defaulting to
colnames(x)- rnames
Default row names are generated from
rownames(x). If you provideFALSEthen it will skip the row names. Note: Fordata.framesif you dorownames(my_dataframe) <- NULLit still has row names. Thus you need to useFALSEif you want to supress row names fordata.frames.- rowlabel
If the table has row names or
rnames,rowlabelis a character string containing the column heading for thernames.- caption
Adds a table caption.
- tfoot
Adds a table footer (uses the
<tfoot>HTML element). The output is run throughtxtMergeLines()simplifying the generation of multiple lines.- label
A text string representing a symbolic label for the table for referencing as an anchor. All you need to do is to reference the table, for instance
<a href="#anchor_name">see table 2</a>. 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 thecss.classelement attribute.- rgroup
A vector of character strings containing headings for row groups.
n.rgroupmust be present whenrgroupis given. See detailed description in section below.- n.rgroup
An integer vector giving the number of rows in each grouping. If
rgroupis not specified,n.rgroupis just used to divide off blocks of rows by horizontal lines. Ifrgroupis given butn.rgroupis omitted,n.rgroupwill 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 torgroupthroughattr(rgroup, "add"), see below explaining section.- cgroup
A vector, matrix or list of character strings defining major column header. The default is to have none. These elements are also known as column spanners. If you want a column not to have a spanner then put that column as "". If you pass cgroup and
n.crgroupas matrices you can have column spanners for several rows. See cgroup section below for details.- n.cgroup
An integer vector, matrix or list containing the number of columns for which each element in cgroup is a heading. For example, specify
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.rowlabeldoes not count in the column numbers. You can omitn.cgroupif all groups have the same number of columns. If then.cgroupis one less than the number of columns in the matrix/data.frame then it automatically adds those.- tspanner
The table spanner is somewhat of a table header that you can use when you want to join different tables with the same columns.
- n.tspanner
An integer vector with the number of rows or
rgroups in the original matrix that the table spanner should span. If you have provided one fewer n.tspanner elements the last will be imputed from the number ofrgroups (if you have providedrgroupandsum(n.tspanner) < length(rgroup)) or the number of rows in the table.- total
The last row is sometimes a row total with a border on top and bold fonts. Set this to
TRUEif 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".- ctable
If the table should have a double top border or a single a' la LaTeX ctable style
- compatibility
Is default set to
LibreOfficeas 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 theoptions(htmlTableCompat = "html")if you want a change to apply to the entire document. MS Excel sometimes misinterprets certain cell data when opening HTML-tables (eg. 1/2 becomes 1. February). To avoid this please specify the correct Microsoft Office format for each cell in the table using the css.cell-argument. To make MS Excel interpret everything as text use "mso-number-format:\"\@\"".- cspan.rgroup
The number of columns that an
rgroupshould span. It spans by default all columns but you may want to limit this if you have column colors that you want to retain.- escape.html
logical: should HTML characters be escaped? Defaults to FALSE.
- ...
Passed on to
print.htmlTablefunction and any argument except theuseViewerwill be passed on to thebase::cat()functions arguments. Note: as of version 2.0.0 styling options are still allowed but it is recommended to instead preprocess your object withaddHtmlTableStyle().- useViewer
If you are using RStudio there is a viewer thar can render the table within that is envoced if in
base::interactive()mode. Set this toFALSEif 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::browseURLif you want to override the default RStudio viewer. Another option that does the same is to set theoptions(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 thebase::cat()the set[options][base::options](htmlTable.cat = TRUE).
Multiple rows of column spanners cgroup
If you want to have a column spanner in multiple levels (rows) you can
set the cgroup and n.cgroup arguments to a matrix or list.
For different level elements, set absent ones to NA in a matrix. For example,
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.
Using a list is recommended to avoid handling NAs.
For an empty cgroup, use "".
The rgroup argument
The rgroup groups rows seamlessly. Each row in a group is indented by two
spaces (unless the rgroup is "") and grouped by its rgroup element. The sum(n.rgroup)
should be \(\leq\) matrix rows. If fewer, remaining rows are padded with an empty rgroup (""). If rgroup
has one more element than n.rgroup, the last n.rgroup is computed as nrow(x) - sum(n.rgroup)
for a smoother table generation.
The add attribute to rgroup
To add an extra element at the rgroup level/row, use attr(rgroup, 'add').
The value can either be a vector, a list,
or a matrix. See vignette("general", package = "htmlTable") for examples.
A
vectorof either equal number ofrgroups to the number ofrgroups that aren't empty, i.e.rgroup[rgroup != ""]. Or a named vector where the name must correspond to either anrgroupor to anrgroupnumber.A
listthat 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.A
matrixwith the dimensionnrow(x) x ncol(x)ornrow(x) x 1where the latter is equivalent to a named vector. If you haverownamesthese will resolve similarly to the names to thelist/vectorarguments. The same thing applies tocolnames.
Important knitr-note
This function will only work with knitr outputting HTML, i.e. markdown mode. As the function returns raw HTML-code the compatibility with non-HTML formatting is limited, even with pandoc.
Thanks to the the knitr::knit_print() and the knitr::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 base::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).
RStudio's notebook
RStudio has an interactive notebook that allows output directly into the document.
In order for the output to be properly formatted it needs to have the class
of html. The htmlTable tries to identify if the environment is a
notebook document (uses the rstudioapi and identifies if its a file with and Rmd
file ending or if there is an element with html_notebook). If you don't want this
behavior you can remove it using the options(htmlTable.skip_notebook = TRUE).
Table counter
If you set the option table_counter you will get a Table 1,2,3
etc before each table, just set 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 = "<b>Table %s:</b> ")
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 numerals instead of Arabic.
Empty data frames
An empty data frame will result in a warning and output an empty table, provided that
rgroup and n.rgroup are not specified. All other row layout options will be ignored.
Options
There are multiple options that can be set, here is a set of the perhaps most used
table_counter- logical - activates a counter for each tabletable_counter_roman- logical - if true the counter is in Roman numbers, i.e. I, II, III, IV...table_counter_str- string - the string used for generating the table counter textuseViewer- logical - if viewer should be used fro printing the tablehtmlTable.cat- logical - if the output should be directly sent tocat()htmlTable.skip_notebook- logical - skips the logic for detecting notebookhtmlTable.pretty_indentation- logical - there was some issues in previous Pandoc versions where HTML indentation caused everything to be interpreted as code. This seems to be fixed and if you want to look at the raw HTML code it is nice to have this set toTRUEso that the tags and elements are properly indented.htmlTableCompat- string - see parameter description
Other
Copy-pasting: As you copy-paste results into Word you need to keep the original formatting. Either right click and choose that paste option or click on the icon appearing after a paste. Currently the following compatibilities have been tested with MS Word 2016:
Internet Explorer (v. 11.20.10586.0) Works perfectly when copy-pasting into Word
RStudio (v. 0.99.448) Works perfectly when copy-pasting into Word. Note: can have issues with multi-line
cgroups - see bugChrome (v. 47.0.2526.106) Works perfectly when copy-pasting into Word. Note: can have issues with multi-line
cgroups - see bugFirefox (v. 43.0.3) Works poorly - looses font-styling, lines and general feel
Edge (v. 25.10586.0.0) Works poorly - looses lines and general feel
Direct word processor opening: Opening directly in Libre Office or Word is no longer recommended. You get much prettier results using the cut-and-paste option.
Google docs: Copy-paste directly into a Google docs document is handled rather well. This seems to work especially well when the paste comes directly from a Chrome browser.
Note that when using complex cgroup alignments with multiple levels
not every browser is able to handle this. For instance the RStudio
webkit browser seems to have issues with this and a
bug has been filed.
As the table uses HTML for rendering you need to be aware of that headers,
row names, and cell values should try respect this for optimal display. Browsers
try to compensate and frequently the tables still turn out fine but it is
not advised. Most importantly you should try to use
< instead of < and
> instead of >. You can find a complete list
of HTML characters here.
Lastly, I want to mention that function was inspired by the Hmisc::latex()
that can be an excellent alternative if you wish to switch to PDF-output.
For the sibling function tidyHtmlTable() you can directly switch between
the two using the table_fn argument.
See also
addHtmlTableStyle(),
setHtmlTableTheme(),
tidyHtmlTable().
txtMergeLines(),
Hmisc::latex()
Other table functions:
tblNoLast(),
tblNoNext()
Examples
# Basic example
output <- matrix(1:4,
ncol = 2,
dimnames = list(list("Row 1", "Row 2"),
list("Column 1", "Column 2")))
htmlTable(output)
#> <table class='gmisc_table' style='border-collapse: collapse; margin-top: 1em; margin-bottom: 1em;' >
#> <thead>
#> <tr><th style='border-bottom: 1px solid grey; border-top: 2px solid grey;'></th>
#> <th style='font-weight: 900; border-bottom: 1px solid grey; border-top: 2px solid grey; text-align: center;'>Column 1</th>
#> <th style='font-weight: 900; border-bottom: 1px solid grey; border-top: 2px solid grey; text-align: center;'>Column 2</th>
#> </tr>
#> </thead>
#> <tbody>
#> <tr>
#> <td style='text-align: left;'>Row 1</td>
#> <td style='text-align: center;'>1</td>
#> <td style='text-align: center;'>3</td>
#> </tr>
#> <tr>
#> <td style='border-bottom: 2px solid grey; text-align: left;'>Row 2</td>
#> <td style='border-bottom: 2px solid grey; text-align: center;'>2</td>
#> <td style='border-bottom: 2px solid grey; text-align: center;'>4</td>
#> </tr>
#> </tbody>
#> </table>
invisible(readline(prompt = "Press [enter] to continue"))
#> Press [enter] to continue
# 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)
}
}
output |>
addHtmlTableStyle(
align = "r",
col.columns = c(rep("none", 2), rep("#F5FBFF", 4)),
col.rgroup = c("none", "#F7F7F7"),
css.cell = "padding-left: .5em; padding-right: .2em;"
) |>
htmlTable(
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
)
#> <table class='gmisc_table' style='border-collapse: collapse; margin-top: 1em; margin-bottom: 1em;' >
#> <thead>
#> <tr><td colspan='9' style='text-align: left;'>
#> Basic table with both column spanners (groups) and row groups</td></tr>
#> <tr>
#> <th style='border-top: 2px solid grey;'></th>
#> <th colspan='2' style='font-weight: 900; border-top: 2px solid grey; text-align: center;'></th><th style='border-bottom: none; border-top: 2px solid grey;' colspan=1> </th>
#> <th colspan='5' style='font-weight: 900; border-bottom: 1px solid grey; border-top: 2px solid grey; text-align: center;'>Column spanners</th>
#> </tr>
#> <tr>
#> <th style=''></th>
#> <th colspan='2' style='font-weight: 900; text-align: center;'></th><th style='border-bottom: none;' colspan=1> </th>
#> <th colspan='2' style='font-weight: 900; border-bottom: 1px solid grey; text-align: center;'>Cgroup 1</th><th style='border-bottom: none;' colspan=1> </th>
#> <th colspan='2' style='font-weight: 900; border-bottom: 1px solid grey; text-align: center;'>Cgroup 2†</th>
#> </tr>
#> <tr><th style='border-bottom: 1px solid grey;'></th>
#> <th style='font-weight: 900; border-bottom: 1px solid grey; text-align: center;'>1st hdr</th>
#> <th style='font-weight: 900; border-bottom: 1px solid grey; text-align: center;'>2nd hdr</th>
#> <th style='font-weight: 900; border-bottom: 1px solid grey; text-align: center;' colspan=1> </th>
#> <th style='font-weight: 900; border-bottom: 1px solid grey; background-color: #f5fbff; text-align: center;'>3rd hdr</th>
#> <th style='font-weight: 900; border-bottom: 1px solid grey; background-color: #f5fbff; text-align: center;'>4th hdr</th>
#> <th style='font-weight: 900; border-bottom: 1px solid grey; text-align: center; background-color: #f5fbff;' colspan=1> </th>
#> <th style='font-weight: 900; border-bottom: 1px solid grey; background-color: #f5fbff; text-align: center;'>5th hdr</th>
#> <th style='font-weight: 900; border-bottom: 1px solid grey; background-color: #f5fbff; text-align: center;'>6th hdr</th>
#> </tr>
#> </thead>
#> <tbody>
#> <tr><td colspan='2' style='padding-left: .5em; padding-right: .2em; font-weight: 900;'>Group A</td>
#> <td style='padding-left: .5em; padding-right: .2em; font-weight: 900; text-align: right;'></td>
#> <td style='padding-left: .5em; padding-right: .2em; font-weight: 900; text-align: right;' colspan=1> </td>
#> <td style='padding-left: .5em; padding-right: .2em; font-weight: 900; background-color: #f5fbff; text-align: right;'></td>
#> <td style='padding-left: .5em; padding-right: .2em; font-weight: 900; background-color: #f5fbff; text-align: right;'></td>
#> <td style='padding-left: .5em; padding-right: .2em; font-weight: 900; text-align: right; background-color: #f5fbff;' colspan=1> </td>
#> <td style='padding-left: .5em; padding-right: .2em; font-weight: 900; background-color: #f5fbff; text-align: right;'></td>
#> <td style='padding-left: .5em; padding-right: .2em; font-weight: 900; background-color: #f5fbff; text-align: right;'></td></tr>
#> <tr>
#> <td style='padding-left: .5em; padding-right: .2em; text-align: left;'> 1st row</td>
#> <td style='padding-left: .5em; padding-right: .2em; text-align: right;'>1:1</td>
#> <td style='padding-left: .5em; padding-right: .2em; text-align: right;'>1:2</td>
#> <td style='padding-left: .5em; padding-right: .2em; text-align: right;' colspan=1> </td>
#> <td style='padding-left: .5em; padding-right: .2em; background-color: #f5fbff; text-align: right;'>1:3</td>
#> <td style='padding-left: .5em; padding-right: .2em; background-color: #f5fbff; text-align: right;'>1:4</td>
#> <td style='padding-left: .5em; padding-right: .2em; text-align: right; background-color: #f5fbff;' colspan=1> </td>
#> <td style='padding-left: .5em; padding-right: .2em; background-color: #f5fbff; text-align: right;'>1:5</td>
#> <td style='padding-left: .5em; padding-right: .2em; background-color: #f5fbff; text-align: right;'>1:6</td>
#> </tr>
#> <tr>
#> <td style='padding-left: .5em; padding-right: .2em; text-align: left;'> 2nd row</td>
#> <td style='padding-left: .5em; padding-right: .2em; text-align: right;'>2:1</td>
#> <td style='padding-left: .5em; padding-right: .2em; text-align: right;'>2:2</td>
#> <td style='padding-left: .5em; padding-right: .2em; text-align: right;' colspan=1> </td>
#> <td style='padding-left: .5em; padding-right: .2em; background-color: #f5fbff; text-align: right;'>2:3</td>
#> <td style='padding-left: .5em; padding-right: .2em; background-color: #f5fbff; text-align: right;'>2:4</td>
#> <td style='padding-left: .5em; padding-right: .2em; text-align: right; background-color: #f5fbff;' colspan=1> </td>
#> <td style='padding-left: .5em; padding-right: .2em; background-color: #f5fbff; text-align: right;'>2:5</td>
#> <td style='padding-left: .5em; padding-right: .2em; background-color: #f5fbff; text-align: right;'>2:6</td>
#> </tr>
#> <tr><td colspan='2' style='padding-left: .5em; padding-right: .2em; font-weight: 900; background-color: #f7f7f7;'>Group B</td>
#> <td style='padding-left: .5em; padding-right: .2em; font-weight: 900; background-color: #f7f7f7; text-align: right;'></td>
#> <td style='padding-left: .5em; padding-right: .2em; font-weight: 900; text-align: right; background-color: #F7F7F7;' colspan=1> </td>
#> <td style='padding-left: .5em; padding-right: .2em; font-weight: 900; text-align: right; background-color: #F6F9FB;'></td>
#> <td style='padding-left: .5em; padding-right: .2em; font-weight: 900; text-align: right; background-color: #F6F9FB;'></td>
#> <td style='padding-left: .5em; padding-right: .2em; font-weight: 900; text-align: right; background-color: #F6F8F9;' colspan=1> </td>
#> <td style='padding-left: .5em; padding-right: .2em; font-weight: 900; text-align: right; background-color: #F6F9FB;'></td>
#> <td style='padding-left: .5em; padding-right: .2em; font-weight: 900; text-align: right; background-color: #F6F9FB;'></td></tr>
#> <tr style='background-color: #f7f7f7;'>
#> <td style='padding-left: .5em; padding-right: .2em; background-color: #f7f7f7; text-align: left;'> 3rd row</td>
#> <td style='padding-left: .5em; padding-right: .2em; background-color: #f7f7f7; text-align: right;'>3:1</td>
#> <td style='padding-left: .5em; padding-right: .2em; background-color: #f7f7f7; text-align: right;'>3:2</td>
#> <td style='padding-left: .5em; padding-right: .2em; text-align: right; background-color: #F7F7F7;' colspan=1> </td>
#> <td style='padding-left: .5em; padding-right: .2em; text-align: right; background-color: #F6F9FB;'>3:3</td>
#> <td style='padding-left: .5em; padding-right: .2em; text-align: right; background-color: #F6F9FB;'>3:4</td>
#> <td style='padding-left: .5em; padding-right: .2em; text-align: right; background-color: #F6F8F9;' colspan=1> </td>
#> <td style='padding-left: .5em; padding-right: .2em; text-align: right; background-color: #F6F9FB;'>3:5</td>
#> <td style='padding-left: .5em; padding-right: .2em; text-align: right; background-color: #F6F9FB;'>3:6</td>
#> </tr>
#> <tr style='background-color: #f7f7f7;'>
#> <td style='padding-left: .5em; padding-right: .2em; background-color: #f7f7f7; text-align: left;'> 4th row</td>
#> <td style='padding-left: .5em; padding-right: .2em; background-color: #f7f7f7; text-align: right;'>4:1</td>
#> <td style='padding-left: .5em; padding-right: .2em; background-color: #f7f7f7; text-align: right;'>4:2</td>
#> <td style='padding-left: .5em; padding-right: .2em; text-align: right; background-color: #F7F7F7;' colspan=1> </td>
#> <td style='padding-left: .5em; padding-right: .2em; text-align: right; background-color: #F6F9FB;'>4:3</td>
#> <td style='padding-left: .5em; padding-right: .2em; text-align: right; background-color: #F6F9FB;'>4:4</td>
#> <td style='padding-left: .5em; padding-right: .2em; text-align: right; background-color: #F6F8F9;' colspan=1> </td>
#> <td style='padding-left: .5em; padding-right: .2em; text-align: right; background-color: #F6F9FB;'>4:5</td>
#> <td style='padding-left: .5em; padding-right: .2em; text-align: right; background-color: #F6F9FB;'>4:6</td>
#> </tr>
#> <tr style='background-color: #f7f7f7;'>
#> <td style='padding-left: .5em; padding-right: .2em; background-color: #f7f7f7; text-align: left;'> 5th row</td>
#> <td style='padding-left: .5em; padding-right: .2em; background-color: #f7f7f7; text-align: right;'>5:1</td>
#> <td style='padding-left: .5em; padding-right: .2em; background-color: #f7f7f7; text-align: right;'>5:2</td>
#> <td style='padding-left: .5em; padding-right: .2em; text-align: right; background-color: #F7F7F7;' colspan=1> </td>
#> <td style='padding-left: .5em; padding-right: .2em; text-align: right; background-color: #F6F9FB;'>5:3</td>
#> <td style='padding-left: .5em; padding-right: .2em; text-align: right; background-color: #F6F9FB;'>5:4</td>
#> <td style='padding-left: .5em; padding-right: .2em; text-align: right; background-color: #F6F8F9;' colspan=1> </td>
#> <td style='padding-left: .5em; padding-right: .2em; text-align: right; background-color: #F6F9FB;'>5:5</td>
#> <td style='padding-left: .5em; padding-right: .2em; text-align: right; background-color: #F6F9FB;'>5:6</td>
#> </tr>
#> <tr style='background-color: #f7f7f7;'>
#> <td style='padding-left: .5em; padding-right: .2em; background-color: #f7f7f7; text-align: left;'> 6th row</td>
#> <td style='padding-left: .5em; padding-right: .2em; background-color: #f7f7f7; text-align: right;'>6:1</td>
#> <td style='padding-left: .5em; padding-right: .2em; background-color: #f7f7f7; text-align: right;'>6:2</td>
#> <td style='padding-left: .5em; padding-right: .2em; text-align: right; background-color: #F7F7F7;' colspan=1> </td>
#> <td style='padding-left: .5em; padding-right: .2em; text-align: right; background-color: #F6F9FB;'>6:3</td>
#> <td style='padding-left: .5em; padding-right: .2em; text-align: right; background-color: #F6F9FB;'>6:4</td>
#> <td style='padding-left: .5em; padding-right: .2em; text-align: right; background-color: #F6F8F9;' colspan=1> </td>
#> <td style='padding-left: .5em; padding-right: .2em; text-align: right; background-color: #F6F9FB;'>6:5</td>
#> <td style='padding-left: .5em; padding-right: .2em; text-align: right; background-color: #F6F9FB;'>6:6</td>
#> </tr>
#> <tr><td colspan='2' style='padding-left: .5em; padding-right: .2em; font-weight: 900;'>Group C</td>
#> <td style='padding-left: .5em; padding-right: .2em; font-weight: 900; text-align: right;'></td>
#> <td style='padding-left: .5em; padding-right: .2em; font-weight: 900; text-align: right;' colspan=1> </td>
#> <td style='padding-left: .5em; padding-right: .2em; font-weight: 900; background-color: #f5fbff; text-align: right;'></td>
#> <td style='padding-left: .5em; padding-right: .2em; font-weight: 900; background-color: #f5fbff; text-align: right;'></td>
#> <td style='padding-left: .5em; padding-right: .2em; font-weight: 900; text-align: right; background-color: #f5fbff;' colspan=1> </td>
#> <td style='padding-left: .5em; padding-right: .2em; font-weight: 900; background-color: #f5fbff; text-align: right;'></td>
#> <td style='padding-left: .5em; padding-right: .2em; font-weight: 900; background-color: #f5fbff; text-align: right;'></td></tr>
#> <tr>
#> <td style='padding-left: .5em; padding-right: .2em; text-align: left;'> 7th row</td>
#> <td style='padding-left: .5em; padding-right: .2em; text-align: right;'>7:1</td>
#> <td style='padding-left: .5em; padding-right: .2em; text-align: right;'>7:2</td>
#> <td style='padding-left: .5em; padding-right: .2em; text-align: right;' colspan=1> </td>
#> <td style='padding-left: .5em; padding-right: .2em; background-color: #f5fbff; text-align: right;'>7:3</td>
#> <td style='padding-left: .5em; padding-right: .2em; background-color: #f5fbff; text-align: right;'>7:4</td>
#> <td style='padding-left: .5em; padding-right: .2em; text-align: right; background-color: #f5fbff;' colspan=1> </td>
#> <td style='padding-left: .5em; padding-right: .2em; background-color: #f5fbff; text-align: right;'>7:5</td>
#> <td style='padding-left: .5em; padding-right: .2em; background-color: #f5fbff; text-align: right;'>7:6</td>
#> </tr>
#> <tr>
#> <td style='padding-left: .5em; padding-right: .2em; border-bottom: 2px solid grey; text-align: left;'> 8th row</td>
#> <td style='padding-left: .5em; padding-right: .2em; border-bottom: 2px solid grey; text-align: right;'>8:1</td>
#> <td style='padding-left: .5em; padding-right: .2em; border-bottom: 2px solid grey; text-align: right;'>8:2</td>
#> <td style='padding-left: .5em; padding-right: .2em; border-bottom: 2px solid grey; text-align: right;' colspan=1> </td>
#> <td style='padding-left: .5em; padding-right: .2em; border-bottom: 2px solid grey; background-color: #f5fbff; text-align: right;'>8:3</td>
#> <td style='padding-left: .5em; padding-right: .2em; border-bottom: 2px solid grey; background-color: #f5fbff; text-align: right;'>8:4</td>
#> <td style='padding-left: .5em; padding-right: .2em; border-bottom: 2px solid grey; text-align: right; background-color: #f5fbff;' colspan=1> </td>
#> <td style='padding-left: .5em; padding-right: .2em; border-bottom: 2px solid grey; background-color: #f5fbff; text-align: right;'>8:5</td>
#> <td style='padding-left: .5em; padding-right: .2em; border-bottom: 2px solid grey; background-color: #f5fbff; text-align: right;'>8:6</td>
#> </tr>
#> </tbody>
#> <tfoot><tr><td colspan='9'>
#> † A table footer commment</td></tr></tfoot>
#> </table>
invisible(readline(prompt = "Press [enter] to continue"))
#> Press [enter] to continue
# An advanced empty table
suppressWarnings({
matrix(ncol = 6, nrow = 0) |>
addHtmlTableStyle(
col.columns = c(rep("none", 2), rep("#F5FBFF", 4)),
col.rgroup = c("none", "#F7F7F7"),
css.cell = "padding-left: .5em; padding-right: .2em;"
) |>
htmlTable(
align = "r",
header = paste(c("1st", "2nd", "3rd", "4th", "5th", "6th"), "hdr"),
cgroup = rbind(c("", "Column spanners", NA),
c("", "Cgroup 1", "Cgroup 2†")),
n.cgroup = rbind(c(1, 2, NA),
c(2, 2, 2)),
caption = "Basic empty table with column spanners (groups) and ignored row colors",
tfoot = "† A table footer commment",
cspan.rgroup = 2
)
})
#> <table class='gmisc_table' style='border-collapse: collapse; margin-top: 1em; margin-bottom: 1em;' >
#> <thead>
#> <tr><td colspan='8' style='text-align: left;'>
#> Basic empty table with column spanners (groups) and ignored row colors</td></tr>
#> <tr>
#> <th colspan='2' style='font-weight: 900; border-top: 2px solid grey; text-align: center;'></th><th style='border-bottom: none; border-top: 2px solid grey;' colspan=1> </th>
#> <th colspan='5' style='font-weight: 900; border-bottom: 1px solid grey; border-top: 2px solid grey; text-align: center;'>Column spanners</th>
#> </tr>
#> <tr>
#> <th colspan='2' style='font-weight: 900; text-align: center;'></th><th style='border-bottom: none;' colspan=1> </th>
#> <th colspan='2' style='font-weight: 900; border-bottom: 1px solid grey; text-align: center;'>Cgroup 1</th><th style='border-bottom: none;' colspan=1> </th>
#> <th colspan='2' style='font-weight: 900; border-bottom: 1px solid grey; text-align: center;'>Cgroup 2†</th>
#> </tr>
#> <tr>
#> <th style='font-weight: 900; border-bottom: 1px solid grey; text-align: center;'>1st hdr</th>
#> <th style='font-weight: 900; border-bottom: 1px solid grey; text-align: center;'>2nd hdr</th>
#> <th style='font-weight: 900; border-bottom: 1px solid grey; text-align: center;' colspan=1> </th>
#> <th style='font-weight: 900; border-bottom: 1px solid grey; background-color: #f5fbff; text-align: center;'>3rd hdr</th>
#> <th style='font-weight: 900; border-bottom: 1px solid grey; background-color: #f5fbff; text-align: center;'>4th hdr</th>
#> <th style='font-weight: 900; border-bottom: 1px solid grey; text-align: center; background-color: #f5fbff;' colspan=1> </th>
#> <th style='font-weight: 900; border-bottom: 1px solid grey; background-color: #f5fbff; text-align: center;'>5th hdr</th>
#> <th style='font-weight: 900; border-bottom: 1px solid grey; background-color: #f5fbff; text-align: center;'>6th hdr</th>
#> </tr>
#> </thead>
#> <tbody>
#> </tbody>
#> <tfoot><tr><td colspan='8'>
#> † A table footer commment</td></tr></tfoot>
#> </table>
invisible(readline(prompt = "Press [enter] to continue"))
#> Press [enter] to continue
# An example of how to use the css.cell for header styling
simple_output <- matrix(1:4, ncol = 2)
simple_output |>
addHtmlTableStyle(
css.cell = rbind(
rep("background: lightgrey; font-size: 2em;", times = ncol(simple_output)),
matrix("", ncol = ncol(simple_output), nrow = nrow(simple_output))
)
) |>
htmlTable(header = LETTERS[1:2])
#> <table class='gmisc_table' style='border-collapse: collapse; margin-top: 1em; margin-bottom: 1em;' >
#> <thead>
#> <tr>
#> <th style='font-weight: 900; background: lightgrey; font-size: 2em; border-bottom: 1px solid grey; border-top: 2px solid grey; text-align: center;'>A</th>
#> <th style='font-weight: 900; background: lightgrey; font-size: 2em; border-bottom: 1px solid grey; border-top: 2px solid grey; text-align: center;'>B</th>
#> </tr>
#> </thead>
#> <tbody>
#> <tr>
#> <td style='text-align: center;'>1</td>
#> <td style='text-align: center;'>3</td>
#> </tr>
#> <tr>
#> <td style='border-bottom: 2px solid grey; text-align: center;'>2</td>
#> <td style='border-bottom: 2px solid grey; text-align: center;'>4</td>
#> </tr>
#> </tbody>
#> </table>
invisible(readline(prompt = "Press [enter] to continue"))
#> Press [enter] to continue
# See vignette("tables", package = "htmlTable")
# for more examples, also check out tidyHtmlTable() that manages
# the group arguments for you through tidy-select syntax