Converts a matrix (or vector) to a <table> HTML fragment, with
optional row/column headers, caption, per-column alignment and widths.
The result has class c("html", "character") (see
asHtml()) and prints as a formatted text table via
preview.html().
Usage
toHtmlTable(
m,
sepCol = FALSE,
caption = "",
bodyAlign = "center",
vAlign = "top",
width = NULL,
cellPadding = 3,
border = 0,
tableWidth = NA,
captionAlign = "center",
frame = TRUE,
rowNames = TRUE,
colNames = TRUE
)Arguments
- m
a matrix or vector
- sepCol
logical; if
TRUE, insert a narrow empty separator column between each pair of columns- caption
table caption text
- bodyAlign
horizontal alignment of body cells (
"left","center","right"), recycled to the number of columns- vAlign
vertical alignment of body cells (HTML
valignattribute:"top","middle","bottom"), recycled to the number of columns- width
column width(s) (HTML
widthattribute), recycled to the number of columns including an optional rowname column; useNAfor columns without an explicit width- cellPadding
HTML
cellpaddingattribute- border
HTML
borderattribute- tableWidth
overall table width (HTML
widthattribute on<table>), orNAfor none- captionAlign
horizontal alignment of the header row cells
- frame
logical; if
TRUE, draw outer frame and group rules (frame="hsides" rules="groups")- rowNames
logical; render the row names as a leading header column. Ignored when
mhas none- colNames
logical; render the column names as a header row. Ignored when
mhas none
Examples
m <- matrix(c(12.3, 4.56, 7.8, 91.2), nrow = 2,
dimnames = list(c("a", "b"), c("mean", "sd")))
# the HTML fragment ...
(h <- toHtmlTable(m, caption = "Summary"))
#> <table border="0" cellpadding="3" data-mce-style="background-color: #e1e7e9;" frame="hsides" rules="groups"><caption>Summary</caption>
#> <tbody><thead> <tr> <td style="text-align: center;"><b></b></td>
#> <td style="text-align: center;"><b>mean</b></td>
#> <td style="text-align: center;"><b>sd</b></td> </tr> </thead> <tr><td style="text-align: left;"><b>a</b></td><td style="text-align: center;" valign="top" >12.3</td><td style="text-align: center;" valign="top" >7.8</td></tr>
#> <tr><td style="text-align: left;"><b>b</b></td><td style="text-align: center;" valign="top" >4.56</td><td style="text-align: center;" valign="top" >91.2</td></tr></tbody></table>
# ... and its rendering as text
preview(h)
#> | mean | sd
#> --+------+-----
#> a | 12.3 | 7.8
#> b | 4.56 | 91.2
# formatted cells, right aligned, without the row names
preview(toHtmlTable(fm(m, digits = 1), rowNames = FALSE, bodyAlign = "right"))
#> mean | sd
#> -----+-----
#> 12.3 | 7.8
#> 4.6 | 91.2
